資料庫實驗 T SQL語言建立及管理資料表

2021-09-25 09:52:07 字數 2188 閱讀 1056

1、建立資料表

1)在資料庫student中建立模式xskc

create schema xskc;

2)在student資料庫中建立基於xskc模式的資料表,表結構如下所示;

student(學生資訊表)

欄位名稱

字段型別及長度

說明備註

snochar(9)

學生學號

主關鍵字

sname

nvarchar(6)

學生姓名

非空s***

nchar(1)

學生性別

可為空sage

int學生年齡

可為空sdept

nvarchar(8)

學生所在院系

可為空course(課程資訊表)

欄位名稱

字段型別及長度

說明備註

cnochar(4)

課程編號

主關鍵字

cname

nvarchar(20)

課程名稱

非空cpno

char(4)

先行課號

可為空ccredit

int學分

可為空sc(選課資訊表)

欄位名稱

字段型別及長度

說明備註

snochar(9)

學生學號

主關鍵字

cnochar(4)

課程編號

主關鍵字

grade

int成績

可為空use student

create table xskc.student

(sno char(9) primary key,

sname nvarchar(6) not null,

s*** nchar(1),

sage int,

sdept nvarchar(8),)

use student

create table xskc.course

(cno char(4) primary key,

cname nvarchar(20) not null,

cpno char(4),

ccredit int,)

use student

create table xskc.sc

(sno char(9),

cno char(4),

grade int,

primary key(sno,cno))

2、修改表結構

1)在shouke表裡新增乙個授課類別字段,列名為type,型別為char(4);

alter table shouke

add type char(4)

2)將shouke表的hours的型別改為smallint;

alter table shouke

alter column hours smallint

3)刪除lessons表中的property列;

alter table lessons

drop column property

4)在表shouke中刪除欄位type;

alter table shouke

drop column type

5)修改表student中欄位名為「sname」的字段長度由原來的6改為8;

alter table xskc.student

alter column sname nvarchar(8)

6)刪除資料表lessons;

drop table dbo.lessons

實驗步驟

management介面方式下的操作步驟

開啟已經建立的資料庫名稱前方的小加號,在【表】節點上右擊,選擇【新建表】命令,開啟表設計器視窗。在表設計器視窗中輸入列名。選擇資料型別及是否允許為空的情況,並在主鍵欄位的前方單擊滑鼠右鍵,選擇【設定主鍵】選項。也可以在列屬性的說明中標出每個字段代表的含義。設計完成後按ctrl+s組合鍵儲存,在彈出的對話方塊中輸入表名,單擊【確定】按鈕。

t-sql語句方式下的操作步驟:

在【sql server management studio】視窗左上方選擇【新建查詢】按鈕,啟動sql編輯器視窗,在游標處輸入t-sql語句,單擊【執行】按鈕。

使用T SQL語言建立資料庫

use master if exists select from sysdatabases where name score 檢查score資料庫是否存在 drop database score 存在就刪除 create database score 建立score資料庫 on primary na...

T sql之建立資料庫

建立乙個資料庫,資料庫名為testdb 在sql2005中看到的名字 create database testdbon 檔案在電腦上顯示的檔名 物理名 name testdb data,資料檔案儲存的位置注意碟符下的資料夾必須事先存在 filename d test testdb data.mdf ...

使用T sql建立資料庫

使用t sql語句建立 myshool 資料庫及刪除資料庫。要求 myshool 資料庫的主資料檔案名為 myshool.mdf,初始大小為3mb,最大為50mb,增長方式為10 日誌檔案的初始大小為1mb,最大為5mb,增長方式為1mb。資料檔案和日誌檔案均存放在d盤根目錄下。create dat...