1. 創建表
創建表:
- create table t1 (id int,name char(4));
- create table t2 (id int,name char(4)) engine=myisam; # 使用MyISAM存儲引擎
- create table t3 (id int,name char(4)) engine=memory; # 使用MEMORY存儲引擎
查看表的結構:
-
show create table 表名; — 能夠看到和這張表相關的所有信息
-
desc 表名; — 只能查看表的字段的基礎信息
desc 表名; = describe 表名;
2. 表的約束
1.unsigned
- 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) );
-
聯合唯一
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
)
-
聯合主鍵(不常用)
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 :
- on delete cascade 寫成 on delete set 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
3. 修改表
1.什么時候會用到修改表?(一般不會常見)
- 創建項目之前已創建了表
- 項目開發、運行過程中
2.修改表語句
alter table 表名 add —— 添加字段
- alter table 表名 add 字段名 數據類型(寬度) 約束 first/after name
alter table 表名 drop —— 刪除字段
- alter table 表名 drop 字段名;
alter table 表名 modify —— 修改已經存在的字段 的類型 寬度 約束
- alter table 表名 modify name(字段名后面是修改的內容) varchar(12) not null
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 —— 修改已經存在的字段 的類型 寬度 約束 和 字段名字
- alter table 表名 change name new_name varchar(12) not null
4. 表關系
兩張表中的數據之間的關系:
-
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(主鍵)
例:表一:一本書有多個作者
? 表二:一個作者又寫了多本書
5. 表數據的操作
1.增加 insert
- 1.insert into 表名 values (值....) —— 所有的在這個表中的字段都需要按照順序被填寫在這里
- 2.insert into 表名(字段名,字段名。。。) values (值....) —— 所有在字段位置填寫了名字的字段和后面的值必須是一 一對應
- 3.insert into 表名(字段名,字段名。。。) values (值....),(值....),(值....) —— 所有在字段位置填寫了名字的字段和后面的值必須是一 一對應
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
表查詢分為:單表查詢 、多表查詢
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
