MySQL的約束條件和SQL修改
- 約束條件主鍵
- 約束條件自增
- 約束條件外鍵
- 表關系的種類
- 修改表相關SQL語句
約束條件主鍵
1.單從約束層面上來講 相當於not null + unique = 非空且唯一
primary key # 主鍵 語法: create table 名 (名 類型 primary key);
eg:
create table ta1(id int primary key);
驗證:
insert into ta1 values(null);
insert into ta1 values(2);
2.但是主鍵還是innoDB存儲引擎組織數據表的依據
''' 1.innoDB中表必須由且只有一個主鍵 2.如果不指定主鍵,會采用采用隱藏字段作主鍵 3.當表中沒有主題但是有非空且唯一的字段會自動成為主鍵 4.主鍵可以加快查詢速度 ''' 驗證3: eg: create table ta2( id int, name char(16), age int not null unique, addr char(16) not null unique);
# 結論:創建表都需要一個id字段,並且該字段應設置為表的主鍵字段 語法: create table 表名( id int primary key, name char(16)
約束條件自增
當有大量數據要寫入數據庫時,id量會非常巨大難以手寫輸入,自增就可以解決這問題
''' 功能: 數據的值以自然數的自動增加 ''' 語法: create table 名( id int pimary key auto_increment, name char(16)); eg: create table ta3(
id int primary key auto_increment,
user varchar(16)
);
insert into ta3 (user) values('gg'),('kk');
''' 創建表時 主鍵字段的默認寫法 id int primary key auto_increment '''
自增補充
1.自增不會因為delete刪除操作而重置
eg: delete from ta3 where id =2; insert into ta3(user) values('kk');
2.重置自增數據 truncate # 刪除數據和重置主鍵 eg: truncate table ta3; insert into ta3(user) values('kk'),('jk');
約束條件外鍵
為什么要有外鍵
該表問題:
1.表結構不清晰 2.表數據重復 3.數據擴展性極差
''' 為解決上訴問題,進行了拆表 但是員工與部門之間缺少了關系 '''
在員工表里面添加一個部門id(即外鍵),以此來確定員工與部門的關系 ''' 外鍵:記錄表與表之間數據關系的字段 '''
語法:
# 用於員工表內
foreign key(員工表的部門id) renferences 部門表(id)
表關系的種類
- 一對多關系
- 多對多關系
- 一對一關系
一對多關系
"""判斷表關系遵循 換位思考 的原則"""
員工表: 一個員工不可以對應多個部門 部門表: 一個部門可以對應多個員工 ''' 結論:員工表與部門表的關系為”一對多“ '''
一對多關系的使用規范:
- "一對多"表關系外鍵字段建在多的一方
- 先寫基本字段類型與約束條件,后寫外鍵
eg: create table dep( id int primary key auto_increment, name varchar(16), fc varchar(16)); create table emp( id int primary key auto_increment, name varchar(16), age int, job_id int, foreign key(job_id) references dep(id)); insert into dep(name,fc) values('財務','存錢'),('保安','守錢'); insert into emp(name,age,job_id) values('小王',23,1),('小李',43,1),('小明',12,2),('小張',32,2);
外鍵字段的特性
1.再創建表的時候一定要先創建被關聯表(自身沒有外鍵字段的表) 2.在插入數據的時候也是先插入被關聯表再插入關聯表 3.關聯表中只能輸入被關聯字段中出現的值 問題: 被關聯表的數據無法自由刪改 解決: on update cascade # 級聯更新,可以自由更新數據 on delete cascade # 級聯刪除,可以刪除更新數據 eg: create table dep( id int primary key auto_increment, name varchar(16), fc varchar(16)); create table emp( id int primary key auto_increment, name varchar(16), age int, job_id int, foreign key(job_id) references dep(id) on update cascade # 級聯更新 on delete cascade # 級聯刪除 ); insert into dep(name,fc) values('財務','存錢'),('保安','守錢'); insert into emp(name,age,job_id) values('小王',23,1),('小李',43,1),('小明',12,2),('小張',32,2);
# 修改數據,把dep中id=2修改為id=4 update dep set id=4 where id=1;
# 刪除dep中id=2的數據 delete from dep where id=2;
多對多關系
書名表: 一本書可以對應多個作者 作者名: 一個作者可以有多本書 ''' 結論:書名表與作者名的'多對多' ''' # 針對多對多的表關系 外鍵字段需要建在第三張關系表中 eg: create table bk( id int primary key auto_increment, name varchar(16)); create table author( id int primary key auto_increment, name varchar(16) ); create table rel( id int primary key auto_increment, authorid int, bookid int, foreign key(authorid) references author(id) on update cascade on delete cascade, foreign key(bookid) references bk(id) on update cascade on delete cascade );
# 插入書名數據 insert into bk(name) values('a書'),('b書'),('c書');
# 出入作者名 insert into author(name) values('小明'),('小王');
# 插入關系表
insert into rel(authorid,bookid) values(1,1),(1,2),(2,3),(2,1);
一對一
# 以夫妻關系為例 丈夫: 只有一個妻子 妻子: 只有一個丈夫 ''' 結論:丈夫和妻子是“一對一”關系 ''' ''' 在一對一關系中,外鍵建在任意一方都可以 但是推薦建在查詢頻率較高的表中 ''' eg: create table hus( id int primary key auto_increment, name varchar(16), wife_id int unique, foreign key(wife_id) references wife(id) on update cascade on delete cascade ); create table wife( id int primary key auto_increment, name varchar(16) );
# 插入妻子信息
insert into wife(name) values('麗麗'),('小倩');
insert into hus(name,wife_id) values('老王',2),('老丁',1);
alter table 名 rename 新名;
eg:
alter table wife rename ex_wife; # 把表名wife改成ex_wife
2.增加字段
語法:
alter table 表名
add 字段名 數據類型 [完整性約束條件…],
add 字段名 數據類型 [完整性約束條件…];
eg:
alter table hus add bobby char(16);
語法: # 將數據放在第一位 alter table 表名 add 字段名 數據類型 [完整性約束條件…] first; eg: alter table hus add age int first;
語法: # 把字段加在某數據之后 alter table 表名 add 字段名 數據類型 [完整性約束條件…] after 字段名; eg: alter table hus add car varchar(16) after name;
3.刪除字段
語法:
alter table 表名 drop 字段名;
eg:
alter table hus drop car;
4.修改字段
# modify只改字段數據類型完整約束,不改字段名,但change可以 語法: alter table 表名 modify 字段名 數據類型 [完整性約束條件]; eg: alter table hus modify wife_id int(8) unique;
語法: alter table 表名 change 舊字段名 新字段名 舊/新數據類型 [完整性約束條件…]; eg: alter table hus change bobby hobby int(11) unique;