創建數據庫
-- 指定數據庫名稱
-- (注:如果數據庫名中包含空格可以使用[]將其標示)
create database [Super WC]
-- 關於數據文件的定義
on
(
name = Super_WC_Data, -- 邏輯名
filename = 'C:/Super_WC_Data.MDF', -- 物理路徑以及物理名
size = 2MB, -- 初始大小
maxsize = 4MB, -- 最大限制
filegrowth = 1MB -- 增長大小
)
-- 關於日誌文件的定義
log on
(
name = Super_WC_Log,
filename = 'C:/Super_WC_Log.LDF',
size = 3MB,
maxsize = 7MB,
filegrowth = 20% -- 增長比例
)
-- 附加數據庫
execute sp_attach_db '[Super WC]', 'C:/Super_WC_Data.MDF','C:/Super_WC_Log.LDF'
-- 分離數據庫
execute sp_detach_db '[Super WC]'
-- 複製數據庫
execute master.dbo.xp_cmdshell 'copy C:/Super_WC_Data.MDF D:/Super_WC_Data.MDF'
execute master.dbo.xp_cmdshell 'copy C:/Super_WC_Log.LDF D:/Super_WC_Log.LDF'
(1)創建數據表
創建一個數據表:學生(students)
結構如下:
字段 類型 是否允許爲空 約束 備註
no char(4) No 主鍵 學號
name nvarchar(8) No 唯一 姓名
birthday datetime No 檢查(至少18年前) 生日
age tinyint No 缺省(默認等於當前時間減去生日) 年齡
sex nchar(1) No 缺省(默認'女') 性別
phone char(11) Yes 檢查(要麼沒有,要麼長度等於11) 電話
address nvarchar(24)No 地址
沒有特別約束的情況:
create table student
(
no char(4) not null,
name nvarchar(8) not null,
birthday datetime not null,
phone char(11) null,
address nvarchar(24) null
)
注意:沒有特別約束的情況下,創建數據表可以參考“企業管理器”中“設計表”的操作格式!
包含約束的情況:
create table students
(
no char(4) primary key,
name nvarchar(8) unique,
birthday datetime check(datediff(year, birthday, getdate()) >= 18),
age as datediff(year, birthday, getdate()),
sex nchar(1) default('女') check(sex = '女' or sex = '男')
phone char(11) check((phone is null) or (len(phone) = 11)),
address nvarchar(24)
)
create table scores
(
no char(4) foreign key references students(no),
chinese numeric(4,1) check(chinese >= 0 and chinese <= 100),
english numeric(4,1) check(english >= 0 and english <= 100)
)
以上答案只是最簡單的描述形式!
比較規範的寫法是
先用create table聲明數據表的結構,
CREATE TABLE students
(
no char(4),
name nvarchar(8),
birthday datetime,
age as DATEDIFF(year, birthday, getdate()),
sex nchar(1),
phone char(11),
address nvarchar(24)
)
然後再ALTER TABLE ADD CONSTRAINT 分別指定每個字段的約束:
每個約束都有一個獨特的名稱,其中,命名規範採用以下格式:
約束類型的簡寫_表名_字段名
pk_students_no
ALTER TABLE students
ADD CONSTRAINT pk_students_no PRIMARY KEY (no)
ALTER TABLE students
ADD CONSTRAINT uq_students_name UNIQUE (name)
ALTER TABLE students
ADD CONSTRAINT ck_students_birthday CHECK (datediff(year,[birthday],getdate()) >= 18)
ALTER TABLE students
ADD CONSTRAINT df_students_sex default ('女') for sex
ALTER TABLE students
ADD CONSTRAINT ck_students_sex CHECK ([sex] = '男' or [sex] = '女')
ALTER TABLE students
ADD CONSTRAINT ck_students_phone CHECK ([phone] is null or len([phone]) = 11)
相對應的對約束進行刪除,則是通過DROP CONSTRAINT子句來完成:
ALTER TABLE students
DROP CONSTRAINT pk_students_no
ALTER TABLE students
DROP CONSTRAINT uq_students_name
注意:
約束只有添加與刪除操作,沒有修改操作!
注意:
其中,age(年齡)採用了“計算列”的表示方法!
“計算列”的定義:
在表中某個字段的值源於某一表達式的值(某一函數的運算結果或是其他字段的運算結果)!
比如:
某學生的成績表結構如下:
數學
語文
體育
總分
創建命令如下:
create table scores
(
math numeric(3, 1),
chinese numeric(3, 1),
sport numeric(3, 1),
total as math + Chinese + sport
)
insert into scores values (80, 69, 95)
total 部分的值會自動計算,爲 244
-- 創建臨時表
-- 臨時表將會存儲在TempDB的臨時數據庫中
-- 當用戶斷開連接後,就會自動刪除
-- 語法:在表名前加上#
create table #tt
(
a int,
b int
)
insert into #tt values (1,1)
insert into #tt values (2,2)
select * from #tt
(2)數據操作(添加/刪除/修改)
添加操作(insert)的語法格式:
Insert [into] 數據表 (字段) values (數據)
-- 添加記錄(一般情況)
insert into students
(no,name,birthday,sex,phone,address)
values
('0001', 'AHuang', '2000-01-01', '男', '13307331100', '株洲')
(注意: into 可以省略 )
-- 添加記錄(如果是給所有字段添加數據,可以省略字段標示)
insert into students
values
('0002', 'ANing', '2008-08-08', '女', '13307330011', '北京')
-- 添加記錄(如果是給具有默認約束的字段添加數據,想利用默認約束,可以利用default)
insert into students
values
('0002', 'ANing', '2008-08-08', default, '13307330011', '北京')
刪除操作(delete)的語法格式:
Delete [from] 數據表 where 條件
-- 刪除記錄(刪除所有)
delete from students
(注意: from 可以省略,即可以: delete students )
-- 刪除記錄(刪除特定記錄,可以通過 where 條件來過濾,比如:學號'0001'的學生記錄)
delete from students where no = '0001'
修改操作(update)的語法格式:
update 數據表 set 字段 = 新值 where 條件
-- 修改記錄(修改所有)
update students set 性別 = '女'
-- 修改記錄(修改特定記錄,可以通過 where 條件來過濾,比如:學號'0001'的學生記錄)
update students set 性別 = '男' where no = '0001'