標籤:添加 use unique strong for HERE 語句 就是 資料庫
一、基礎操作
1、 資料庫表的建立
create table student(
int primary key auto_increment,
name varchar(20) unique,
age int
)
2、表的相關約束
primary key (非空並且唯一):能夠唯一區分目前記錄的欄位稱為主鍵
unique: 唯一,就是資料不能有重複的,一旦有重複就會報錯
not null: 該列資料不可為空,否則報錯
auto_increment: 主鍵欄位必須是數字類型
foreign key:外鍵約束 (外碼)
3、查看錶 (僅用student為代表)
desc student 查看錶結構
show columns from student 查看錶結構
show tables 查看當前資料的所有表
show create table student 查看錶的建立語句
4、修改表結構
①增
alter table student add [列名] 類型 [約束性條件] [first|after 列名]
alter table student add abd int after name; 添加abd列,指定它在 name 列的後面
alter table student add aba int first ; 添加ada列,指定該列在 最前面
alter table student add abs int, 添加多列
②刪 (列)
alter table 表 drop [column] 列名
alter table student drop aBa;
alter table student drop column abd,drop column abs;
表:
drop table 表名;
③改
列的類型:
alter table student modify 列名 類型 [約束條件] [first|after 欄位名]
alter table student modify aba varchar(20) unique; 將 aba 列修飾成 唯一 欄位
列名:
alter table 表 change 列名 新列名 類型 [約束條件] [first|after 欄位名]
alter table student change aba bcd varchar(10); 將aba 修改成 aBa 緊跟在原來的名字後面
表名:
rename table 表名 to 新表名;
rename student to Student;
④查
基本結構:
select
from
where
二、建立索引
索引:
普通索引:
alter table student add (index|key) [索引名](欄位.....)
1.alter table student add index (name) #為name建立索引,索引名預設為欄位名
2.alter table student add index username(name) #為name建立索引,索引名為username
注意:key和index的使用方法一樣
唯一索引:
alter table studnet add unique (index|key) [索引名](欄位名....)
1.alter table studnet add unique index username(name) #為欄位name建立唯一索引,索引名為username
聯合索引:
1.alter table student add index user_age(name, age)
2.alter table student add unique index user_age(name, age)
刪除索引:
alter table student drop (index|key) 欄位名
1.alter table student drop index username
今天的分享就結束啦,在針對相關的增刪改查的訓練和記憶的時候,還是要針對相應的習題開始,這樣才有利於加深自己的理解。
資料庫 SQL語句