課程網(wǎng)站怎么做關(guān)鍵詞優(yōu)化app
1,前言
SQL是計(jì)算機(jī)的一門基礎(chǔ)語言,無論在開發(fā)還是數(shù)據(jù)庫(kù)管理上都是非常重要,最近總結(jié)歸納了一下相關(guān)知識(shí),記錄如下。
2,歸納
SQL是結(jié)構(gòu)化查詢語言。
關(guān)系數(shù)據(jù)庫(kù)有三級(jí)模式結(jié)構(gòu)。
基本表和視圖一樣都是關(guān)系。
舉例:student(sno,sname,ssex,sage,sdept);
? ? ?course(cno,cname,cpno,ccredit)
? ? ?sc(sno,cno,grade)
2.1 模式
簡(jiǎn)單說,模式實(shí)際上是個(gè)命名空間,在空間中可以定義數(shù)據(jù)庫(kù)對(duì)象,比如,基本表、視圖等等。
語句如下:
? create schema <模式名> authorization <用戶名>
示例:
create schema "s-t" autherization designlab
在定義模式的時(shí)候可以順便定義表等等。
create schma test authorizaton desingla
create table test1(
col1 INT,
col2 char(10)
);
模式刪除:
drop schema <模式名> <cascade|restrict>
這個(gè)cascade和restrict必選一個(gè)。
casecade級(jí)聯(lián)刪除,相當(dāng)把這個(gè)模式里面的所有東西全部刪除,在生產(chǎn)環(huán)境應(yīng)該避免使用。
restrict限制,當(dāng)模式中有數(shù)據(jù)庫(kù)對(duì)象時(shí)候,是無法刪除的。
2.2 基本表
定義:
create table student
( ?sno char(9) primary key,
? ?sname char(20) unique,
? ?ssex char(2),
? ?sage smallint,
? ?sdept char(20)
);
?? 這里面有碼,主鍵,外鍵的關(guān)系,這里面有數(shù)據(jù)庫(kù)設(shè)計(jì)方法的概念,以后再說。
create table course
?( cno char(4) primary key,
? ? cname char(20),
? ?cpno char(4), /*cpno是先修課*/
? ? ccredit smallint,
? ?foreign key cpno references course(cno)
);
我們看見,這里面外鍵是參照了自己。
create talbe sc
( sno char(7),
? cno char(4),
? grade smallint,
? primary key (sno,cno),
? foreign key (sno) references student(sno),
? foreign key(cno) references course(cno)
?);
修改表:
alter table student add entrance DATE;
? ? ? alter table student alter column sage INT;
alter table course add unique(cname);
? ? ? ? 刪除表:
drop table <表名> [restrict|cascade]
默認(rèn)是restrict
2.3 索引
索引主要是為了加快查詢速度。
create [unique][cluster] index <索引名>
ON <表名> (<列名>[次序])
示例:
create unique index courcno ON course(cno);
2.4 查詢