1. 創(chuàng)建表
創(chuàng)建表:
- 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存儲引擎
查看表的結(jié)構(gòu):
-
show create table 表名; — 能夠看到和這張表相關(guān)的所有信息
-
desc 表名; — 只能查看表的字段的基礎(chǔ)信息
desc 表名; = describe 表名;
2. 表的約束
1.unsigned
- unsigned —— 設(shè)置某一個數(shù)字無字符
2.not null
-
not null —— 某一個字段不能為空
-
嚴(yán)格模式會影響非空設(shè)置的效果
3.default
-
default 給某個字段設(shè)置默認(rèn)值
create table t2( id int not null, name char(12) not null, age int default 18, # 設(shè)置默認(rèn)值為18,但不會自動填充 gender enum('male','female') not null default 'male' # 不填充gender的值時,會自動默認(rèn)填充'male' )
4.unique
-
unique 設(shè)置某一個字段不能重復(fù)
create table t3( id int unique, username char(12) unique, password char(18) );
-
聯(lián)合唯一
5.auto_increment
-
auto_increment 設(shè)置某一個int類型的字段 自動增加
-
字段設(shè)置條件 :必須是數(shù)字 且 必須是唯一的 —— 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字段設(shè)置為自增字段,增加數(shù)據(jù)時不輸入id字段的值,它會自動增加
6.primary key 主鍵
-
primary key 設(shè)置某一個字段非空且不能重復(fù)
-
約束這個字段 :非空(not null) 且 唯一(unique) 相當(dāng)于 not null + unique
-
一張表只能設(shè)置一個主鍵
-
一張表可以沒有主鍵,但最好設(shè)置一個主鍵(這已變成一條規(guī)范)
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
)
-
聯(lián)合主鍵(不常用)
7.foreign key 外鍵 涉及到兩張表
-
references
-
外鍵關(guān)聯(lián)的字段至少必須是唯一unique的,所以會直接將被關(guān)聯(lián)的這個字段設(shè)置成主鍵。
-
先創(chuàng)建被關(guān)聯(lián)的外表,再創(chuàng)建本表。
部門表 : 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;
級聯(lián)刪除和級聯(lián)更新:
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
)
如果級聯(lián)刪除外表中關(guān)聯(lián)的數(shù)據(jù)后,讓本表中被關(guān)聯(lián)的外鍵列數(shù)據(jù)仍然存在,需要將外鍵列設(shè)置為空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記錄時,將子表上匹配記錄的列設(shè)為null
注意:子表的外鍵列不能為not null
3. 修改表
1.什么時候會用到修改表?(一般不會常見)
- 創(chuàng)建項目之前已創(chuàng)建了表
- 項目開發(fā)、運行過程中
2.修改表語句
alter table 表名 add —— 添加字段
- alter table 表名 add 字段名 數(shù)據(jù)類型(寬度) 約束 first/after name
alter table 表名 drop —— 刪除字段
- alter table 表名 drop 字段名;
alter table 表名 modify —— 修改已經(jīng)存在的字段 的類型 寬度 約束
- alter table 表名 modify name(字段名后面是修改的內(nèi)容) 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 —— 修改已經(jīng)存在的字段 的類型 寬度 約束 和 字段名字
- alter table 表名 change name new_name varchar(12) not null
4. 表關(guān)系
兩張表中的數(shù)據(jù)之間的關(guān)系:
-
1.多對一 :foreign key 永遠是在多的那張表中設(shè)置外鍵
foreign key(多) references 表(一)
例:多個學(xué)生都是同一個班級的
? 學(xué)生表 關(guān)聯(lián) 班級表
? 學(xué)生是多,班級是一
-
2.一對一 :foreign key +unique —— 后出現(xiàn)的后一張表中的數(shù)據(jù)作為外鍵,并且要約束這個外鍵類型是唯一的 unique
foreign key(后一) references 表(先一)
例:一個客戶對應(yīng)一個學(xué)生, 在學(xué)生表中創(chuàng)建外鍵
? 一個商品 有一個商品詳情 ,詳情頁中有外鍵
-
3.多對多 :產(chǎn)生第三張表,把兩個關(guān)聯(lián)關(guān)系的字段作為第三張表的外鍵
foreign key(外鍵名1) references 表1(主鍵)
foreign key(外鍵名2) references 表2(主鍵)
例:表一:一本書有多個作者
? 表二:一個作者又寫了多本書
5. 表數(shù)據(jù)的操作
1.增加 insert
- 1.insert into 表名 values (值....) —— 所有的在這個表中的字段都需要按照順序被填寫在這里
- 2.insert into 表名(字段名,字段名。。。) values (值....) —— 所有在字段位置填寫了名字的字段和后面的值必須是一 一對應(yīng)
- 3.insert into 表名(字段名,字段名。。。) values (值....),(值....),(值....) —— 所有在字段位置填寫了名字的字段和后面的值必須是一 一對應(yīng)
value單數(shù) :一次性寫入一行數(shù)據(jù)
values復(fù)數(shù) :一次性寫入多行數(shù)據(jù)
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)
數(shù)據(jù)寫入的角度:
-
第一個角度:
-
寫入一行內(nèi)容還是寫入多行
insert into 表名 values (值....)
insert into 表名 values (值....),(值....),(值....)
-
-
第二個角度:
-
是把這一行所有的內(nèi)容都寫入
insert into 表名 values (值....)
-
指定字段寫入
insert into 表名(字段1,字段2) values (值1,值2)
-
2.刪除 delete
delete from 表 where 條件;
3.更新 update
update 表 set 字段=新的值 where 條件;
4.查詢 select
表查詢分為:單表查詢 、多表查詢
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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