最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

Oracle 建表、刪除表,數據,約束,創(chuàng)建視圖、索引,表設計的三范式

2023-03-10 14:06 作者:阿彥絕地反擊  | 我要投稿

day04?

1. 建表、刪除表

2. 數據:插入、修改、刪除

3. 約束:非空約束、主鍵約束、外鍵約束【難】、檢查約束、唯一約束

4. 創(chuàng)建視圖、索引*

5. 表設計的三范式

一、建表、刪表

建表語法>>> | 刪表的語法>>>

create table 表名( | dropt table 表名;

列名1 列類型 列級約束,

... ...

列名N 列類型 列級約束,

表級約束1...,

表級約束2...

);

1、創(chuàng)建學生信息表:學號、姓名、地址、電話、郵箱、年齡

學號:唯一、必填不可為null 【主鍵】

姓名:必填,但可重復

地址:可填、可不填

郵箱:包含@

電話: 非必填字段

年齡:>=15歲

drop table? studentTB;

create table studentTB(

id number(5) ,

name varchar2(50) not null, ###姓名?

email varchar2(50), ###郵箱?

age number(3), ###年齡

constraint PK_studentTB_id primary key(id), ###設id是當前表的主鍵

constraint CK_studentTB_age check(age>=15 ), ###檢查年齡填寫時必須>=15

constraint CK_studentTB_email check(email like '%@%') ###檢查郵箱填寫時必須包含@

);

--插入1000號學生蛋總

insert into studentTB(id,name,age,email) values(1000,'蛋總',28,'dan@qq.com');? ?commit;

2.創(chuàng)建課程表:課程編號、課程名?

create table classTB( ###創(chuàng)建課程表

id number(4), ###課程編號

name varchar2(30), ###課程名稱

constraint PK_classTB_id primary key(id) ###設id是當前課程表的主鍵

);

--插入2門課程

insert into classTB(id,name) values(1,'數學');

insert into classTB(id,name) values(2,'體育');

commit;

3.考試信息表:考試編號id、學號sid (關聯(lián)學生表的id)、課程號cid (關聯(lián)課程表的id)、成績

create table scoreTB( ###考試表

id number(5), ###考號

myCode varchar2(20), ###考場編碼

sId number(5), ###學號

cId number(4), ###課程號

score number(5,2), ###成績

constraint PK_scoreTB_id primary key(id), ###設id是課程表的主鍵

constraint FK_scoreTB_studentTB_sId foreign key(sid) references studentTB(id),##設sid外鍵關聯(lián)學生表的主鍵id

constraint FK_scoreTB_classTB_cId foreign key(cid) references classTB(id)###設cid外鍵關聯(lián)課程表的主鍵id

);

--插入1000學生(蛋總)的1號課程(數學)考試成績

insert into scoreTB(id,mycode,sid,cid,score) values(101,'xdl184901',1000,1,59.5);

insert into scoreTB(id,mycode,sid,cid,score) values(102,'xdl184901',1000,1,62);

commit;


二、數據增、刪、改

1.向學生表插入一行數據

插入語法:insert into 表名(列名1,...,列名n) values(值,....,值);

? commit;

insert into studentTB(id,name,age,email) values(1000,'蛋總',28,'dan@qq.com');

insert into studentTB(id,name,tel,age,email,address) values(1001,'張紹剛','12345678901',38,'hei@qq.com','魔都');

commit;


2.修改當前蛋總第一次考試成績

修改數據的語法:update 表名 set 列名1=值,列名2=值 ,.... where 更新條件;

update scoreTB set score=48 where id=101;



3.刪除

刪除數據的語法:delect from 表名 where 刪除條件;

delete from scoreTB where id=101 ;



三、約束【默寫】

1.主鍵約束,表級語法:constraint 自定義名 primary key(主鍵列名).

主鍵的特點:值唯一,值必填。?

? ?一個表只能有一個主鍵,但一個主鍵可以包含多列。*包含多列的主鍵我們稱聯(lián)合主鍵


2.檢查約束,表級語法:constraint 自定義名 check(條件)


3.唯一約束,表級語法:constraint 自定義名 unique(列)

唯一的特點:值唯一,可空,

唯一約束與主鍵約束的區(qū)別是:一個表只能有1個主鍵,但一個表可以有多個唯一約束

? ?唯一約束里值可以是null,但主鍵約束中值不能為null

4.外鍵約束,表級語法:constraint 自定義名 foreign key(外鍵列名) references 主表名(主鍵名)?



四、視圖

什么是視圖?

答:用來存儲查詢語句

什么時候用視圖呢?

答:當查詢業(yè)務復雜且頻繁執(zhí)行時,這個業(yè)務還具有較為廣闊的應用型,可以將此查詢保存到視圖。


1.先用system/123456系統(tǒng)管理身份登錄,給scott這個用戶授予視圖創(chuàng)建的權利后,重新scott/tiger登錄

c:\...>sqlplus system/123456

SQL> grant create view to scott;

SQL> conn score/tiger

2.創(chuàng)建視圖:create view 視圖名 as 查詢語句;

例如:查詢學生成績顯示姓名、課程名及考試相關信息

create view v_score

as

select sc.id scId, sc.mycode scCode, s.name stuName, c.name className,sc.score score

from studentTB s inner join scoreTB sc on s.id=sc.sid

? ? ? ? ? ? ? ? ? inner join classTB c on c.id = sc.cid;

3.視圖使用:select * from 視圖名;

select * from v_score;

4.刪除視圖:drop view 視圖名;


五、創(chuàng)建索引

1.先系統(tǒng)管理員授權:grant create any index to scott;

2.在用scott登錄創(chuàng)建索引:create index 自定義索引名稱 on 表(列)

? ? ? ? create index index_score on scoreTB(sid);


六、作業(yè)

1.查每個學生每門課程的考試成績

2.查沒有參加考試的學生信息(學號、姓名)

3.查成績不及格的信息(考號、學號、姓名、課程名、成績)



Oracle 建表、刪除表,數據,約束,創(chuàng)建視圖、索引,表設計的三范式的評論 (共 條)

分享到微博請遵守國家法律
井冈山市| 体育| 济源市| 霍林郭勒市| 奉贤区| 逊克县| 鄂尔多斯市| 安吉县| 泰兴市| 蛟河市| 丰台区| 昭苏县| 改则县| 石楼县| 正宁县| 扶绥县| 武义县| 军事| 宁南县| 公安县| 临江市| 乐都县| 北海市| 伊金霍洛旗| 雷波县| 共和县| 随州市| 奉贤区| 涡阳县| 丽江市| 古田县| 江达县| 佳木斯市| 马边| 旌德县| 南平市| 凤冈县| 车致| 桐庐县| 龙门县| 玛沁县|