表格
- 创建表命令:create table 表名称;
- 表中插入数据命令:insert into 表名 (字段名1, 字段名2, 字段名3, …) values (字段1, 字段2, 字段3, …);
或insert into 表名 values (字段1, 字段2, 字段3, …); - 查询表中的数据命令:select 字段名 from 表名称 [查询条件];
- 查询表中所有的数据命令:select * from 表名称;
- 修改表中的数据命令:update 表名称 set 新字段名=新值 where 更新条件(在哪条数据上修改,如:旧数据名=旧数据);
- 删除表中的数据命令:delete from 表名称 where 删除条件(在哪条数据上删除,如:数据名=数据);
- 添加字段名命令:alter table 表名 add 字段名 列数据类型 约束条件(可省) [first/after 插入位置];
- 修改字段名命令:alter table 表名 change 字段名 字段新名称 新数据类型;
- 删除字段命令:alter table 表名 drop 字段名;
- 重命表名命令:alter table 表名 rename to 新表名;
- 删除整张表命令:drop table 表名;
- 查看表结构命令:describe 表名;
- 查看表中的字段命令:show columns from 表名;
- 将表中记录清空命令:delete from 表名;
或 truncate 表名; - 查看字段、字段类型、主键、默认值:desc/describe <表名>;
- 显示创建表时的详细语句: show create table <表名>;
- 修改字段数据类型: alter table <表名> modify <字段名> <新数据类型>;
- 添加字段排列位置:
1.将字段修改为表的第一个字段:
alter table <表名> modify<字段名> <数据类型> first;
2.将字段放到指定的字段之后:
alter table <表名> modify<字段名> <数据类型> after <字段名>; - 更改表的存储引擎:alter table <表名> engine= 引擎 (InnoDB)
- 添加表约束:
1.添加主键约束:alter table <表名> add constraint 主键约束名 primary key<字段名>;
2.添加外键约束:alter table <表名> add constraint 外键约束名 foreign key<字段名> references <主表表名>(关联主表字段);
3.添加唯一约束: alter table <表名> add constraint 约束名 unique(字段名);
4.添加非空约束:alter table <表名> modify <字段名> <数据类型> [约束条件];
5.删除外键约束:alter table <表名> drop foreign key 外键约束名;
6.删除没有关联的表:drop table [if exists] 表1,表2……表n;
(注意:If exists关键字不是必须的,用来判断表是否存在,如果存在执行删除,如果不存在,不执行删除)