DDL
DDL語句:定義語言的縮寫,也就是數(shù)據(jù)庫內(nèi)部的對象進(jìn)行創(chuàng)建、刪除、修改等操作的語言。和DML語句的最大區(qū)別是DML只是對表內(nèi)部數(shù)據(jù)操作,而不涉及表的定義,結(jié)構(gòu)的修改,更不會(huì)涉及到其他對象。
數(shù)據(jù)庫:
create database bookstore;//創(chuàng)建數(shù)據(jù)庫 drop database bookstore;//刪除數(shù)據(jù)庫
在命令行創(chuàng)建數(shù)據(jù)庫指定編碼:
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;//刪除表
修改表:
//向表中添加一項(xiàng) ALTER TABLE table_name ADD(column_name datatype [constraint_condition]) //修改表的某一項(xiàng): ALTER TABLE table_name MODIFY column_name datatype... ALTER TABLE table_name CHANGE column_name column_name datatype... //刪除表中某一項(xiàng): 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,且數(shù)據(jù)類型更改為int(4)*/ ALTER TABLE T_student MODIFY sex CHAR(2)/*修改數(shù)據(jù)類型例子*/ alter table emp rename emp1;/*更改表名為emp1*/
change和modify的區(qū)別:
/*change 和modify都可以修改表的定義,不同的是change后面需要寫兩次列名,不方便。但是change的有點(diǎn)是可以修改列名稱,modify則不能*/
after和first的使用:
直接使用add column默認(rèn)增加到最后一列
使用first關(guān)鍵字可以讓指定列在最前面
使用after關(guān)鍵字可以讓指定列在某列后面
alter table emp modify age int(3) first;/*修改age列放到最前面*/
alter table emp add birth date after ename;/*增加birth列在ename后面*/
約束:
[constraint_condition1]: 指定列完整性約束條件 唯一、主、外、檢查、非空
完整性約束條件可以定義在列級上,也可以定義在表級上。列級完整性約束指的是在定義列和指定列中數(shù)據(jù)類型之后就定義完整性約束條件;表級的完整性約束指的是在定義了所有咧之后在定義完整性的約束條件。
/*非空約束
  not null 不允許插入空值,只能用來約束列
      在建表的時(shí)候創(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);-- 報(bào)錯(cuò)不能插入空值
drop table tabA;
/*唯一約束
unique  保證某列的值唯一,允許為空,這里注意:是允許多個(gè)空值。就是空值的不唯一
      在建表的時(shí)候創(chuàng)建:
               create table tab(name varchar(50) unique);     
*/
create table tabA
(
 name varchar(50) unique       
);
insert into tabA values('sag'); -- 不能重復(fù)插入了
/*
主鍵約束
primary key 保證使用主鍵約束的某一列或者一組列中的值唯一,不能為空,不能包含空,在表中只能有一個(gè)主鍵約束
        在建表的時(shí)候創(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 用來限制列的取值范圍或者取值條件
      在建表的時(shí)候創(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 建立主從表關(guān)聯(lián)
        語法格式:
        foreign key[表名](列名1) references 表名2(列名2)
        [on update [cascade]|[ser null]|[restrict]]
        [on delete [cascade]|[set null]|[restrict]]
        on update和ondelete分別制定了在對表中的數(shù)據(jù)做修改和刪除時(shí),主從表要采取的操作方式,可選操作
           以刪除為例子,三種操作方式講解下:
           cascade:級聯(lián)刪除。如果主表中一條數(shù)據(jù)被刪除,那么從表中與之對應(yīng)的數(shù)據(jù)也將被刪除
           set null:置空刪除。如果主表中的一條數(shù)據(jù)被刪除,那么從表中與之對應(yīng)的數(shù)據(jù)將被設(shè)置為空值
           restrict:受限刪除。如果主表中的一條數(shù)據(jù)被刪除,則在執(zhí)行delete命令時(shí)數(shù)據(jù)庫管理會(huì)報(bào)錯(cuò),通知用戶與主表對應(yīng)的從表中的相對應(yīng)的數(shù)據(jù)仍然存在,報(bào)錯(cuò),刪除失敗。這是默認(rèn)的方式
           update的時(shí)候也是這樣子的。
       在建表的時(shí)候創(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); -- 直接插入會(huì)受到外鍵約束限制而報(bào)錯(cuò)
insert into teacher values(1,'xionggong');
drop table teacher; -- 先刪從表再刪主表,否則可能報(bào)錯(cuò)
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);
--非空約束好像這樣玩會(huì)出問題,只能在建表的時(shí)候添加或者修改表項(xiàng)添加
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);
--注意:有些時(shí)候我們誤以為只有主鍵才可以作為其他表的外鍵,這個(gè)想法是錯(cuò)誤的,其實(shí),是只有該鍵有索引才可以作為其他表的外鍵
alter table taba add constraint fk_a foreign key (name) references tabb(name);--這個(gè)不會(huì)成功
alter table tabb add index tabb_index_name(name);--增加索引,增加成功索引之后就可以用來做外鍵了
--基本上所有約束都會(huì)自動(dòng)生成索引,默認(rèn)的索引名是你的約束名
刪除一個(gè)約束條件:
ALTER TABLE table_name DROP constraint_type
ALTER TABLE T_dept DROP PRIMARY KEY//例子
alter table taba drop foreign key fk_a;//例子
索引:
索引
唯一標(biāo)識(shí),類似于數(shù)組中的下標(biāo),方便查詢數(shù)據(jù)。
索引的分類:
唯一索引、主索引、復(fù)合索引、聚簇索引等等。
unique關(guān)鍵字創(chuàng)建唯一索引。
primary key關(guān)鍵字創(chuàng)建主索引
單列索引:
    定義在一個(gè)數(shù)據(jù)列上的索引就是單列索引。
復(fù)合索引:
    創(chuàng)建在多個(gè)列上的索引叫做復(fù)合索引
聚簇索引:
    為了提高SQL語言對數(shù)據(jù)表的查詢效率,可以為數(shù)據(jù)表創(chuàng)建一個(gè)聚簇索引。聚簇索引中索引項(xiàng)的順序與數(shù)據(jù)表中數(shù)據(jù)集的物理順序保持一致。一個(gè)聚簇索引在一個(gè)數(shù)據(jù)表中只能創(chuàng)建一個(gè)。
一般數(shù)據(jù)庫中對數(shù)據(jù)表中的索引數(shù)量給予限制。
創(chuàng)建與刪除索引:
創(chuàng)建索引:
CREATE [UNIQUE]|[CLUSTRE] INDEX索引名
ON 表名(列名[排序方式]...)
其中UNIQUE表示創(chuàng)建的索引是唯一索引;關(guān)鍵字CLUSTER表示創(chuàng)建的索引是聚簇索引,這兩個(gè)索引都是可選的 ASC表示升序DESC表示降序
CREATE INDEX i_profession ON T_teacher(profession)//單列索引
CREATE INDEX i_dept_profession ON T_teacher(dept,profession)//復(fù)合索引
CREATE INDEX i_salary ONT_teacher(salary ASC)//使用排序
刪除索引:
DROP INDEX 索引名
DROP INDEX i_profession
                            總結(jié)
 
                            
                        - 上一篇: 电圆锯倒装
- 下一篇: S5PV210之GPIO模拟I2c时序之
