1.创建表
create table <表名>
{
字段名1 数据类型 [字段级别约束] [默认值],
字段名2 数据类型 [字段级别约束] [默认值],
.......
[表级别约束]
};
2.查看表
desc 表名;
3.插入数值
insert into 表名 (字段名1,[字段名2],...) values (值1,...);
4. 查看表
select * from 表名;
5. 复制表 只是复制的表的结构,表的记录是不会被克隆的。
create table 新表名 like 旧表名;
6.新增表字段
alter table 表名 add 字段名 字段类型;
在表的指定位置添加字段
alter table 表名 add 新增字段名 字段类型 after 字段名;
7.修改表字段
change 修改名字和属性
modify 修改属性
altet table 表名 change 修改前字段名 修改后字段名 字段类型;
alter table 表名 modify 字段名 字段属性;
8.小结一下
- 默认值约束:就是给字段设置一个默认值
- 非空约束:就是声明字段不能为空值
- 唯一性约束:就是声明字段不能重复
- 自增约束:就是声明字段值能够自动加1,且不会重复。
- create table 字段名 字段类型 primary key;
- create table 字段名 字段类型 not null;
- create table 字段名 字段类型 unique;
- create table 字段名 字段类型 default 值;
- create table 字段名 字段类型 auto_increment;
- create 新表 like 旧表;
- alter table 表名 change 旧字段名 新字段名 数据类型;
- alter table 表名 add column 字段名 字段类型 first|after 字段名;
- alter table 表名 modify 字段名 字段类型 first|after 字段名;