通用SQL基礎

通用SQL基礎

DML數據操作語言:insert delete update
DDL數據定義語言:create alter drop truncate
DCL數據控制語言:grant revoke --通常無需程序員 操作
事物控制語句:commit rollback savepoint

1. 五類完整性約束

NOT NULL:非空約束,指定某列不能爲空。
UNIQUE:唯一約束,指定某列或者幾列組合不能重複。
PRIMARY KEY:主鍵約束,指定該列的值可以唯一標識該條記錄。
FOREIGN KEY:外鍵約束,指定該行記錄從屬於主表中的一條記錄,主要用於保證參照完整性。
CHECK:檢查約束,指定一個布爾表達式,用於指定對應列的值必須滿足該表達式。(MYSQL對check約束不起任何作用)

2. 五種約束的建立和刪除

2.1 非空約束

1.建表時指定非空約束:
create table user(id int not null, name varchar(255) not null, age int null);
2.建表後添加或者刪除非空約束
–增加非空約束
alter table user modify age int not null;
–刪除非空約束
alter table user modify age int null;
–取消非空並指定默認值
alter table user modify age int default 15 null;

2.2 unique約束

唯一性約束用於保證指定列或指定的列組合不允許出現重複值。同一張表內可建多個唯一約束,唯一約束可由多列組合而成。當爲某列創建唯一約束時,mysql會爲該列相應地創建唯一索引,如果不給唯一約束起名字,該唯一約束默認與列名相同。

–使用列級約束語法建立唯一約束–
create table User (
id int not null,
name varchar(255) unique
);
–爲多列組合建立唯一約束----使用表級約束語法–
語法格式: constraint 約束名 約束定義;
create table User(
id int not null,
name varchar(255),
pass varchar(255),
unique(name),–使用列級約束
constraint User_uk unique(pass) --使用表級約束,此時可以起約束名
);
add關鍵字增加唯一約束
alter table User add unique(
name,pass
);

modify關鍵字增加唯一約束
alter table User modify name varchar(255) unique;

刪除唯一約束:
alter table User drop constraint 約束名;

drop刪除唯一約束(適用於mysql)
alter table User drop index User_uk; – User_uk爲約束名

2.3 PRIMARY KEY 約束

主鍵約束相當於非空約束和唯一約束。
–使用列級語法創建主鍵約束
create table User (
id int primary key,
name varchar(255)
);

–使用表級語法創建主鍵約束
create table User (
id int not null,
name varchar(255),
pass varchar(255),
–指定主鍵約束名爲User_pk,對大部分數據庫有效,但對mysql無效。
–mysql中該主鍵約束名依然是primary
constraint User_pk primary key(id)
);

–多列建立組合主鍵
create table User (
name varchar(255),
pass varchar(255),
primary key(name,pass)
);
–刪除指定表的主鍵約束
alter table User drop primary key;–alter table語句後使用drop primary key子句。
–add爲指定表增加主鍵
alter table User add primary key(name ,pass);

–modify爲單獨的數據列增加主鍵
alter table User modify name varchar(255) primary key;

–mysql設置主鍵自動增長
create table User (
id int auto_increment primary key,
name varchar(255),
pass varchar(255)
–一旦指定了某列具有自動增長特性,插入記錄時不能爲該
–列指定值。
);

2.4 FOREIGN KEY約束

–外鍵約束主要用於保證一個或者兩個數據表之間的參照完整性。
外鍵是構建於一個表的兩個字段或者兩個表的兩個字段之間的
參照關係。

–從表外鍵參照的是主表主鍵或者唯一鍵列,這樣保證從表記錄
可以準確定位到被參照的主表記錄。同一個表內可以擁有多個外鍵。
–對於一對多的關聯關係,通常在多的一端來增加外鍵列。對於一對一
的關聯關係,則可選擇任意一方來增加外鍵列。
–只要將外鍵列增加唯一約束就可表示一對一關聯了。
–對於多對多關聯,則需要額外增加一個連接表來記錄他們的關聯關係。
==關鍵字references,references指定該列參照的那個主表,以及參照主表的哪一列。
eg:
–先建立主表
create table teacher(
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);

–從表
create table student (
student_id int auto_increment primary key,
student_name varchar(255),
teacher_id int references teacher(teacher_id)
);
–雖然mysql支持列級約束的語法來建立外鍵約束,但這種列級約束語法
建立的外鍵約束不會生效。此時應該用表級外鍵約束語法
–先建立主表
create table teacher(
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);
–從表–用表級語法建立
creata table student(
student_id int auto_increment primary key,
student_name varchar(255),
teacher_id int,
–不指定外鍵約束名時的寫法(兩者二選一)
foreign key(teacher_id) references teacher(teacher_id)
–指定外鍵約束名時的寫法
constraint student_teacker_fk foreign key(teacher_id) references teacher(teacher_id)
);

–表級語法建立多列組合的外鍵約束
–先建立主表
create table teacher(
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);

–從表–用表級語法建立
creata table student(
student_id int auto_increment primary key,
student_name varchar(255),
teacher_id int,
–不指定外鍵約束名時的寫法(兩者二選一)
foreign key(teacher_id,teacher_pass) references teacher(teacher_id,teacher_pass)
);
–刪除外鍵約束
alter table student drop foreign key student_teacher_fk; --student_teacher_fk爲約束名
–add增加外鍵約束
alter table student add foreign key(
teacher_name,teacher_pass
) references teacher(teacher_name,teacher_pass);
–自關聯:例如使用一個表保存某個公司所有員工記錄,而部門經理
和普通員工之間是1:N的關係,但是他們保存在同一張表中,這就是典型的
自關聯。
create table dept(
id int auto_increment primary key,
name varchar(255),
refer_id int,
foreign key(id) references dept(id);
);
–刪除主表記錄時,從表記錄也刪除
–先建立主表
create table teacher(
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);

–從表–用表級語法建立–需要在建立外鍵約束後添加on delete cascade 或
添加 on delete set null
creata table student(
student_id int auto_increment primary key,
student_name varchar(255),
teacher_id int,
–使用表級約束語法建立外鍵刪除,第一級聯刪除
foreign key(teacher_id) references teacher(teacher_id) on delete cascade;

);

2.5 CHECK約束

–建表時增加check約束–需要在建表的列定義後增加check(邏輯表達式)
create table teacher(
teacher_id int auto_increment,
teacher_name varchar(255),
teacher_salary decimal,
primary key(teacher_id),
–建立check約束
check(teacher_salary >0)
);

常見數據庫對象

數據字典:就是系統表,存放數據庫相關信息的表。系統表裏面的數據通常由數據庫系統維護。程序員通常不用手動修改系統表及內部數據,只可查看系統表數據。

約束:constraint 是在表上強制執行數據校驗的規則,用於保證了數據完整性的規則。

視圖:view 一個或者多個數據表裏數據的邏輯顯示。視圖並不存儲數據。

索引:index 用於提高查詢的性能 相當於書籍目錄。

函數:function 用於完成一次特定的計算,具有一個返回值。

存儲過程:procedure 用於完成一次完整的業務處理,沒有返回值,但可通過傳出參數將多個值傳給調用環境。

觸發器: trigger 相當於一個事件監聽器,當數據庫發生特定事件後,觸發器被觸發,完成響應的處理。

基礎建表語句

create table [模式名.]表名(
//定義多個列
columnName datatype,
);
eg:
create table user(id int,username varchar(50), userpwd varchar(50),userinfo varchar(255));

子查詢建表

使用子查詢建表的同時可以插入數據。子查詢建表:

create table [模式名.]表名 as subquery;

eg: //所建立的表User與table_userinfo表結構和表內容一樣。
create table User as select * from table_userinfo; //此處的subquery爲子查詢語句:select * from table_userinfo

修改表結構

1. 添加列:alter … add

alter table 表名 add (
//多個列
columnName datatype,
);

eg:
alter table User add(
userinfo varchar(100) default ‘xxx’,
usertype int
); //增加列userinfo和usertype

2. 修改列屬性:modify

只修改字段類型而不能修改字段名本身,修改字段本身需要用change–>change適用於mysql(見下文)
alter table 表名 modify columnName datatype;
eg:alter table User modify uage varchar(255); //將uage的類型修改爲varchar(255)

3. 刪除現有的列

刪除現有的列:drop
alter table 表名 drop columnName;
eg:alter table User drop uname; //刪除列uname

4. 刪除表

drop table 表名;
eg:drop table User; //刪除User表 --表結構被刪除、表數據被刪除、該表相關索引和約束一併刪除。

5. 一次性刪除表中所有的記錄

truncate --truncate 比delete速度要快
truncate 表名;
eg: truncate User; //刪除User所有記錄,User表結構還在。

以上適用於所有數據庫。

mysql 數據庫

1.重新命名數據表

alter table 表名 rename (to) 新表名; //to可以省略

eg:
alter table User rename User1; //將表User重命名爲User1;或者alter table User rename to User3;

2.修改表字段並修改該字段屬性

alter table 表名 change old_columnName new_columnName datatype;

eg:alter table User3 change uage uage1 varchar(255);//將列uage重命名爲uage1 字段類型設置爲varchar(255)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章