DDL
DDL語句:定義語言的縮寫,也就是數據庫內部的對象進行創(chuàng)建、刪除、修改等操作的語言。和DML語句的最大區(qū)別是DML只是對表內部數據操作,而不涉及表的定義,結構的修改,更不會涉及到其他對象。
數據庫:
create database bookstore;//創(chuàng)建數據庫 drop database bookstore;//刪除數據庫
在命令行創(chuàng)建數據庫指定編碼:
CREATE DATABASE db_gz DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
表:
語法:
CREATE TABLE table_name( column_name1 datatype1 [constraint_condition1], column_name2 datatype2 [constraint_condition2], ..... )
create table books (/*建表*/ book_id int, title varchar(50), author varchar(50) );
drop table tablename;//刪除表
修改表:
//向表中添加一項 ALTER TABLE table_name ADD(column_name datatype [constraint_condition]) //修改表的某一項: ALTER TABLE table_name MODIFY column_name datatype... ALTER TABLE table_name CHANGE column_name column_name datatype... //刪除表中某一項: ALTER TABLE table_name DROP column_name //改表名 ALTER TABLE table_name rename newtable_name alter table emp add column age int(3);/*增加age字段*/ alter table emp drop column age;/*刪除age字段*/ alter table emp change age age1 int(4);/*將age改名為age1,且數據類型更改為int(4)*/ ALTER TABLE T_student MODIFY sex CHAR(2)/*修改數據類型例子*/ alter table emp rename emp1;/*更改表名為emp1*/
change和modify的區(qū)別:
/*change 和modify都可以修改表的定義,不同的是change后面需要寫兩次列名,不方便。但是change的有點是可以修改列名稱,modify則不能*/
after和first的使用:
直接使用add column默認增加到最后一列
使用first關鍵字可以讓指定列在最前面
使用after關鍵字可以讓指定列在某列后面
alter table emp modify age int(3) first;/*修改age列放到最前面*/
alter table emp add birth date after ename;/*增加birth列在ename后面*/
約束:
[constraint_condition1]: 指定列完整性約束條件 唯一、主、外、檢查、非空
完整性約束條件可以定義在列級上,也可以定義在表級上。列級完整性約束指的是在定義列和指定列中數據類型之后就定義完整性約束條件;表級的完整性約束指的是在定義了所有咧之后在定義完整性的約束條件。
/*非空約束
not null 不允許插入空值,只能用來約束列
在建表的時候創(chuàng)建:
create table tab(id int not null,name varchar(20) not null);
*/
create table tabA
(
id int not null
)
insert into tabA values(null);-- 報錯不能插入空值
drop table tabA;
/*唯一約束
unique 保證某列的值唯一,允許為空,這里注意:是允許多個空值。就是空值的不唯一
在建表的時候創(chuàng)建:
create table tab(name varchar(50) unique);
*/
create table tabA
(
name varchar(50) unique
);
insert into tabA values('sag'); -- 不能重復插入了
/*
主鍵約束
primary key 保證使用主鍵約束的某一列或者一組列中的值唯一,不能為空,不能包含空,在表中只能有一個主鍵約束
在建表的時候創(chuàng)建
create table tab(id int primary key);
create table tab(id int,name varchar(20), primary key(id,name));
*/
create table tabA
(
id int primary key
)
create table tabB
(
id int,
name varchar(20),
primary key(id,name)
)
insert into tabb (id,name) values (1,'sa');
insert into tabb values(null,'sb'); -- 可以看到不能插入null值
insert into tabb values(1,'sa'); --也不能插入相同的值
drop table tabb;
/*檢查約束 注意:mysql檢查約束無效
check 用來限制列的取值范圍或者取值條件
在建表的時候創(chuàng)建
create table tabA(id int,name varchar(20),sex varchar(2),check(sex in ('男','女'));
create table tabA(id int,name varchar(20),sex varchar(2) check(sex in ('男','女'));
*/
create table tabA
(
id int check(id>3 and id<5)
);
insert into tabA values(1);
create table tabB
(
sex varchar(2),
check(sex in ('男','女'))
);
/*外鍵約束
foreign key 建立主從表關聯
語法格式:
foreign key[表名](列名1) references 表名2(列名2)
[on update [cascade]|[ser null]|[restrict]]
[on delete [cascade]|[set null]|[restrict]]
on update和ondelete分別制定了在對表中的數據做修改和刪除時,主從表要采取的操作方式,可選操作
以刪除為例子,三種操作方式講解下:
cascade:級聯刪除。如果主表中一條數據被刪除,那么從表中與之對應的數據也將被刪除
set null:置空刪除。如果主表中的一條數據被刪除,那么從表中與之對應的數據將被設置為空值
restrict:受限刪除。如果主表中的一條數據被刪除,則在執(zhí)行delete命令時數據庫管理會報錯,通知用戶與主表對應的從表中的相對應的數據仍然存在,報錯,刪除失敗。這是默認的方式
update的時候也是這樣子的。
在建表的時候創(chuàng)建
create table tabA(stuid int,name varchar(20),foreign key(stuid) references student(stuid) on delete casade) ;
*/
create table student
(
id int primary key,
name varchar(50),
tea_id int,
foreign key(tea_id) references teacher(id)
)
create table teacher
(
id int primary key,
name varchar(20)
)
insert into student values(1,'guozhen',1); -- 直接插入會受到外鍵約束限制而報錯
insert into teacher values(1,'xionggong');
drop table teacher; -- 先刪從表再刪主表,否則可能報錯
drop table student;
--- 另一種方式添加約束,約束效果就不演示了
create table tabA
(
id int,
name varchar(20)
)
create table tabB
(
id int,
name varchar(20),
a_id int
)
select * from taba;
select * from tabb;
alter table tabA add constraint primary key(id);
--非空約束好像這樣玩會出問題,只能在建表的時候添加或者修改表項添加
alter table tabA add constraint is not null(name);
alter table taba modify name varchar(20) not null; -- 這樣可以
--外鍵約束這樣
alter table tabb add constraint fk_s foreign key (a_id) references taba(id);
--注意:有些時候我們誤以為只有主鍵才可以作為其他表的外鍵,這個想法是錯誤的,其實,是只有該鍵有索引才可以作為其他表的外鍵
alter table taba add constraint fk_a foreign key (name) references tabb(name);--這個不會成功
alter table tabb add index tabb_index_name(name);--增加索引,增加成功索引之后就可以用來做外鍵了
--基本上所有約束都會自動生成索引,默認的索引名是你的約束名
刪除一個約束條件:
ALTER TABLE table_name DROP constraint_type
ALTER TABLE T_dept DROP PRIMARY KEY//例子
alter table taba drop foreign key fk_a;//例子
索引:
索引
唯一標識,類似于數組中的下標,方便查詢數據。
索引的分類:
唯一索引、主索引、復合索引、聚簇索引等等。
unique關鍵字創(chuàng)建唯一索引。
primary key關鍵字創(chuàng)建主索引
單列索引:
定義在一個數據列上的索引就是單列索引。
復合索引:
創(chuàng)建在多個列上的索引叫做復合索引
聚簇索引:
為了提高SQL語言對數據表的查詢效率,可以為數據表創(chuàng)建一個聚簇索引。聚簇索引中索引項的順序與數據表中數據集的物理順序保持一致。一個聚簇索引在一個數據表中只能創(chuàng)建一個。
一般數據庫中對數據表中的索引數量給予限制。
創(chuàng)建與刪除索引:
創(chuàng)建索引:
CREATE [UNIQUE]|[CLUSTRE] INDEX索引名
ON 表名(列名[排序方式]...)
其中UNIQUE表示創(chuàng)建的索引是唯一索引;關鍵字CLUSTER表示創(chuàng)建的索引是聚簇索引,這兩個索引都是可選的 ASC表示升序DESC表示降序
CREATE INDEX i_profession ON T_teacher(profession)//單列索引
CREATE INDEX i_dept_profession ON T_teacher(dept,profession)//復合索引
CREATE INDEX i_salary ONT_teacher(salary ASC)//使用排序
刪除索引:
DROP INDEX 索引名
DROP INDEX i_profession
總結
- 上一篇: 电圆锯倒装
- 下一篇: 魔兽世界怎么获得沙色夜刃豹 沙色夜刃豹获