資料庫上機實驗報告

/*表級完整性約束條件,jno是外碼,被參照表是j*/ 

); 

2、數據表的更改 

在s表中添加一個concat 列

alter table s add concat varchar(20) 

在s表中刪除concat 列 

alter table s drop column concat

更改s表 concat列的屬性 把長度由20改為30 

alter table s alter column concat varchar(30) 

聯繫方式 名字為concat 修改屬性為唯一的 屬性名為con_concat 

alter table s add constraint con_concat unique(concat) 

刪除約束關係con_concat 

alter table s drop constraint con_concat 

/*插入一個元組*/ 

insert into s valus(‘s1’,’精益’,20,’天津’) /*20不能寫成’20’*/ 

試驗中的問題的排除與總結: 

1、在創建spj時

有三個實體所以從3個實體中取主碼,還有一個數量屬性也要寫上 

主碼由那3個主碼確定 

2、更改一個資料庫中數據表時一定要先使該資料庫處於正在使用狀態 

3、constraint 

是可選關鍵字,表示 primary key、not null、unique、foreign key 或 check 約束定義的開始。約束是特殊屬性,用於強制數據完整性並可以為表及其列創建索引。 

4、--go可以不加但是要注意順序 註:go --注釋 提示錯誤 

5、注意添加一個空元素用 null 

附 sql備份 

--創建一個資料庫 student 

create database student 

go 

--在資料庫student中創建表student course sc 注意順序 

use student 

---------------------------------------------------------------- 

create table student 

sno char(9) primary key, /*sno是主碼 列級完整性約束條件*/ 

sname char(10) unique, /*sname取唯一值*/ 

ssex char(2), 

sage smallint, /*類型為smallint*/ 

sdept char(20) /*所在系*/ 

); /*;要加*/ 

-----------