目录
一,数据库相关
1,登录数据库
mysql -u 用户名 -p密码
2,显示所有数据库
show databases;
3,创建数据库
create database 数据库名;
4,删除数据库
drop database 数据库名;
5,选择数据库
use 数据库名;
二,约束
unique key 唯一约束
not null 非空约束
default 默认约束
primary key 主键约束
foreign key 外键约束
auto_increment 自动增长
unsigned 无符号约束
三,表相关
1,查看所有表
show tables;
2,创建表
create table [if not exists] 表名(
字段名 类型(长度) 约束,字段名 类型(长度) 约束...
)
#[if not exists] 表示创建的表如果之前没有则创建, #若有了则不创建
3,查看字段
desc 表名;
4,删除表
drop table 表名;
5,修改表名
alter table 旧表名 rename 新表名;
6,增加字段
alter table 表名 add 字段名 类型(长度) 约束;
7,修改字段
alter table 表名 change 旧字段 新字段 类型(长度) 约束;
8,删除字段
alter table 表名 drop 字段名;
9,增加主键
alter table 表名 add constraint 主键名称 primary key 表名(字段名);
10,增加外键
alter table 表名 add constraint 外键名 foreign key(字段名) references 关联的表名(字段名);