1. 程式人生 > >資料庫建表基本操作

資料庫建表基本操作

資料庫的基本操作練習:

/*要求在本地磁碟D建立一個學生-課程資料庫(名稱為student),
只有一個數據檔案和日誌檔案,檔名稱分別為stu和stu_log,
物理名稱為stu_data.mdf 和stu_log.ldf,初始大小分別為5MB和3MB,
增長方式分別為10%和1MB,資料檔案最大為500MB,日誌檔案大小不受限制。*/

CREATE DATABASE  student         	 /* 資料庫名 */
ON
PRIMARY                            /* 主檔案組 */
( NAME = 'stu',           /* 主資料檔案邏輯名 */
FILENAME='D:\stu_data.mdf', 
SIZE = 5MB,
 MAXSIZE = 500MB,
FILEGROWTH = 10%)
LOG ON
( NAME = 'stu_log', 
FILENAME='D:\stu_log.ldf',                                             
SIZE = 3MB,
MAXSIZE = UNLIMITED, 
FILEGROWTH = 1MB)
GO

/* 3)建立一個SPJ資料庫,該資料庫的主資料檔案邏輯名稱為SPJ_data,
物理檔案為SPJ.mdf,初始大小為10MB,最大尺寸為無限大,
增長速度為10%;資料庫的日誌檔案邏輯名稱為SPJ_log,物理檔名為SPJ.ldf,
初始大小為1MB,最大尺寸為50MB,增長速度為1MB。 */
CREATE DATABASE  SPJ        	 /* 資料庫名 */
ON
PRIMARY                            /* 主檔案組 */
( NAME = 'SPJ_data',           /* 主資料檔案邏輯名 */
FILENAME='D:\201700800358\SPJ.mdf', 
SIZE = 10MB,
 MAXSIZE = UNLIMITED,
FILEGROWTH = 10%)
LOG ON
( NAME = 'SPJ_log', 
FILENAME='D:\201700800358\SPJ.ldf',                                             
SIZE = 1MB,
MAXSIZE = 50MB, 
FILEGROWTH = 1MB)
GO


/*在student資料庫中建立架構(schema)teacher指定給使用者teacher(也就是要先建立一個teacher使用者)*/
sp_addlogin 'teacher','111'
/*為資料庫test新增(對映)使用者*/
use student
go
sp_adduser 'teacher','teacehr'  /*--這一行需要單獨執行*/
exec sp_addrolemember 'db_owner','teacher'


use student
go
exec sp_addlogin 'teacher','123456'

use student
go
exec sp_addrolemember 'db_owner','teacher'



create schema "teacher" authorization teacher

/*在已建立的teacher架構中建立“tea”表,表結構為(tno(編號), tname(姓名),
 tsd(專業),tphone, te_mail)(資料型別和長度自己定義)*/
create table "teacher".tea
(tno char(9),
tname char(20),
tsd char(20),
tphone char(20),
te_mail char(20)
);

/*修改student表結構,為該表增加phone和e_mail兩個屬性列*/
alter table student add phone char(20);
alter table student add e_mail char(20);

/*將student表的專業屬性列資料型別改為變長字串型別,長度為20*/
alter table student alter column Sdept char(20);

/*刪除tea表的電話屬性列*/
alter table tea drop column tphone;

資料庫基本操作