刑美美,野蛮王妃逍遥王,天国的树演员表
创建表:
查看表的结构:
show create table 表名; — 能够看到和这张表相关的所有信息
desc 表名; — 只能查看表的字段的基础信息
desc 表名; = describe 表名;
1.unsigned
2.not null
not null —— 某一个字段不能为空
严格模式会影响非空设置的效果
3.default
default 给某个字段设置默认值
create table t2( id int not null, name char(12) not null, age int default 18, # 设置默认值为18,但不会自动填充 gender enum('male','female') not null default 'male' # 不填充gender的值时,会自动默认填充'male' )
4.unique
unique 设置某一个字段不能重复
create table t3( id int unique, username char(12) unique, password char(18) );
联合唯一
需要联合的两个字段都不唯一,但是两个字段联合在一起时就要是唯一的。
将需要联合的两个字段写道unique()里 —— unique(字段名1,字段名2)
create table t4( id int, ip char(15), server char(10), port int, unique(ip,port) );
5.auto_increment
auto_increment 设置某一个int类型的字段 自动增加
字段设置条件 :必须是数字 且 必须是唯一的 —— int + unique
auto_increment自带非空not null 、自增的效果
create table t5( id int unique auto_increment, username char(10), password char(18) ) insert into t5(username,password) values('alex','alex3714') # id字段设置为自增字段,增加数据时不输入id字段的值,它会自动增加
6.primary key 主键
primary key 设置某一个字段非空且不能重复
约束这个字段 :非空(not null) 且 唯一(unique) 相当于 not null + unique
一张表只能设置一个主键
一张表可以没有主键,但最好设置一个主键(这已变成一条规范)
create table t6( id int not null unique, # 你指定的第一个非空且唯一的字段会被定义成主键 name char(12) not null unique ) create table t7( id int primary key, # 主键 name char(12) not null unique )
联合主键(不常用)
联合主键:设置每一个字段不能为空,并且这两个字段联合起来成为不能重复的主键的元素
create table t4( id int, ip char(15), server char(10), port int, primary key(ip,port) );
7.foreign key 外键 涉及到两张表
references
外键关联的字段至少必须是唯一unique的,所以会直接将被关联的这个字段设置成主键。
先创建被关联的外表,再创建本表。
部门表 : pid postname post_comment post_phone create table post( pid int primary key, postname char(10) not null unique, comment varchar(255), phone_num char(11) ) 员工表 create table staff( id int primary key auto_increment, age int, gender enum('male','female'), salary float(8,2), hire_date date, post_id int, foreign key(post_id) references post(pid) ) insert into post / staff values ………… update post set pid=2 where pid = 1; delete from post where pid = 1;
级联删除和级联更新:
create table staff2( id int primary key auto_increment, age int, gender enum('male','female'), salary float(8,2), hire_date date, post_id int, foreign key(post_id) references post(pid) on update cascade on delete cascade )
如果级联删除外表中关联的数据后,让本表中被关联的外键列数据仍然存在,需要将外键列设置为空null :
create table staff2( id int primary key auto_increment, age int, gender enum('male','female'), salary float(8,2), hire_date date, post_id int, foreign key(post_id) references post(pid) on update cascade on delete set null )
on delete:
cascade方式:
在父表上update/delete记录时,同步update/delete掉子表的匹配记录
set null方式:
在父表上update/delete记录时,将子表上匹配记录的列设为null
注意:子表的外键列不能为not null
1.什么时候会用到修改表?(一般不会常见)
2.修改表语句
alter table 表名 add —— 添加字段
alter table 表名 drop —— 删除字段
alter table 表名 modify —— 修改已经存在的字段 的类型 宽度 约束
id name age alter table 表名 modify age int not null after id; # 将age的位置修改到id后面 alter table 表名 modify age int not null first; # 将age的位置放在第一个
alter table 表名 change —— 修改已经存在的字段 的类型 宽度 约束 和 字段名字
两张表中的数据之间的关系:
1.多对一 :foreign key 永远是在多的那张表中设置外键
foreign key(多) references 表(一)
例:多个学生都是同一个班级的
学生表 关联 班级表
学生是多,班级是一
2.一对一 :foreign key +unique —— 后出现的后一张表中的数据作为外键,并且要约束这个外键类型是唯一的 unique
foreign key(后一) references 表(先一)
例:一个客户对应一个学生, 在学生表中创建外键
一个商品 有一个商品详情 ,详情页中有外键
3.多对多 :产生第三张表,把两个关联关系的字段作为第三张表的外键
foreign key(外键名1) references 表1(主键)
foreign key(外键名2) references 表2(主键)
例:表一:一本书有多个作者
表二:一个作者又写了多本书
1.增加 insert
value单数 :一次性写入一行数据
values复数 :一次性写入多行数据
t1 :id,name,age insert into t1 value (1,'alex',83) insert into t1 values (1,'alex',83),(2,'wusir',74) insert into t1(name,age) value ('alex',83) insert into t1(name,age) values ('alex',83),('wusir',74)
数据写入的角度:
第一个角度:
写入一行内容还是写入多行
insert into 表名 values (值....)
insert into 表名 values (值....),(值....),(值....)
第二个角度:
是把这一行所有的内容都写入
insert into 表名 values (值....)
指定字段写入
insert into 表名(字段1,字段2) values (值1,值2)
2.删除 delete
delete from 表 where 条件;
3.更新 update
update 表 set 字段=新的值 where 条件;
4.查询 select
表查询分为:单表查询 、多表查询
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论