博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(2.7)Mysql之SQL基础——表的操作与查看
阅读量:4627 次
发布时间:2019-06-09

本文共 2707 字,大约阅读时间需要 9 分钟。

(2.7)Mysql之SQL基础——表的操作与查看

搜索关键字:mysql表操作,comment注释操作,mysql临时表

 

0、临时表 create temporary table 

1、创建表(在innodb下)

  1.1、create table table_name(column type) ENGINE=innodb default CHARSET=UTF8  COLLATE=utf8_general_ci COMMENT='this is a test~';  --常规默认方式 create table table_name(column type)  选项

  1.2、create table table_name like old_table_name;  --like:包括旧表的  表结构+索引 (不含数据信息)

  1.3、create table table_name  as select * from old_table_name;  --create table as select :包括旧表的 表结构+表数据 (不含索引信息),as 可以省略

2、查询表(show tables;)(必须切换到数据库)

  2.1、show tables;

  2.2、show tables from db_name;

  2.3、show tables like '%table_name%';

3、切换数据库(use)

  3.1 、use db_name;

4、查看表结构与表定义(desc/show)

  4.1 查看表结构:desc table_name;           or       desc db_name.table_name;

  4.2 查看表定义:show create table table_name;  or  show create table db_name.table_name;

  4.3 查看列定义:SHOW FULL COLUMNS FROM tbl_name;  

5、删除表(drop)

  5.1 drop table test101;

  5.2 drop table test101,test102;

6、重命名表(rename)

  6.1 rename table test101 to test1011;

  6.2 rename table test101 to test1011,test102 to test1022;

7、截断表数据  

  7.1 删除表中所有数据 truncate:truncate table table_name;

8、插入表数据

  8.1  单条插入insert into values:insert into tab_name(column_1,column_2……) value(val1,val2……)

  8.2  单条插入insert into set:INSERT INTO products SET column1=val1,column2=val2           --只能单条插入

  8.3  多条插入insert into select:insert into table_name(column_1.......) value select val1.....from tab2_name

  8.4  多条插入insert into values(),():insert into tab_name(column_1,column_2……) value(val1,val2……),(val11,val12……)

9、更新表数据

  9.1 单表更新update table_name set column1=value1,column2=value2....

  9.2 多表关联更新/多表更新 update table_1 t1 join table_2 t2 on t1.id=t2.id set t1.column1=value1,t2.column1=value2

10、删除表数据

  10.1 单表删除delete from table_name    or  delete t1 from table_name t1

  10.2 多表关联删除delete t1 from table_1 t1 join table_2 t2 on t1.id = t2.id where t1.id=1 

11、列操作(add/drop/modify/change)

  alter table table_name add/drop/modify/change

  11.1 添加一个列 add: 【1】alter table test101 add n1 varchar(20);  【2】alter table test101 add(n1 varchar(20),n2 varchar(30));

  11.2 删除一个列 drop :【1】 alter table test101 drop n1;

  11.3 重命名列 change:【1】alter table test101 change n1 n11 varchar(20); 【2】alter table test101 change n1 n1 varchar(50);

  11.4 修改列属性 modify:【1】修改列字段类型:alter table test101 modify n1 varchar(100);  【2】修改字符集:alter table test101 character set utf8;

 

 

-- 查看表的 commentSELECT table_name, table_commentFROM information_schema.tablesWHERE table_schema = 'schema_name' AND table_name = 'table_name';-- 查看表中 columns 的 commentSELECT column_name, column_commentFROM information_schema.columnsWHERE table_schema = 'schema_name' AND table_name = 'table_name';---------------------

 

 

  

 

转载于:https://www.cnblogs.com/gered/p/10363599.html

你可能感兴趣的文章
Java分布式锁的三种实现方案(redis)
查看>>
运行客户端程序报读取配置文件出错的解决方案
查看>>
day 5 - 2 字典(dict)练习
查看>>
微引擎的自定义菜单40063错误解决
查看>>
JAVA wait(), notify(),sleep具体解释
查看>>
数据挖掘十大经典算法
查看>>
WebService原理
查看>>
【Unity 3D】学习笔记三十七:物理引擎——碰撞与休眠
查看>>
js动态删除div元素
查看>>
计算机网络中的TCP/IP模型
查看>>
spring mvc 自定义Handlermapping
查看>>
JS验证密码安全级别
查看>>
Cookie是可以覆盖的,如果重复写入同名的Cookie,那么将会覆盖之前的Cookie。
查看>>
高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程
查看>>
高并发 Nginx+Lua OpenResty系列(8)——Lua模版渲染
查看>>
跟我学SpringCloud | 第三篇:服务的提供与Feign调用
查看>>
高并发 Nginx+Lua OpenResty系列(9)——HTTP服务
查看>>
跟我学SpringCloud | 第五篇:熔断监控Hystrix Dashboard和Turbine
查看>>
高并发 Nginx+Lua OpenResty系列(10)——商品详情页
查看>>
跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
查看>>