USE master
GO
IF EXISTS (SELECT * FROM sys.databases WHERE name ='jhdx') DROP DATABASE jhdx
go
CREATE DATABASE [jhdx] ON PRIMARY
( NAME = N'jhdx', FILENAME = N'G:\sqlServer\DATA\jhdx.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'jhdx_log', FILENAME = N'G:\sqlServer\DATA\jhdx_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
/*************2017年7月18日 上午*********************
一、數據類型:(數字類型 字符數據類型 二進制數據類型 時間數據類型)
-數字類型
-整數類型 bigint int smallint bit tinyint
-小數類型 decimal numeric
-貨幣類型 money smallmoney
-字符數據類型
-字符類型 char(定長字符串) nchar nvarchar varchar(可變長字符串) text(文本2^31-1)
-時間類型
-時間datetime(2017-07-18 12:32:35.620) smalldatetime(2017-07-18 12:33:00) date(2017-07-18)
二、數據完整性
-數據完整性 (精確性 可靠性)
-實體完整性 (主鍵約束primary key,自增長identity(1,2) 唯一性約束unique-不可重複但可爲null)
-域完整性 (檢查約束 check , 外鍵約束foreign key, 默認值default,非空not null)
-引用完整性
-用戶自定義完整性
三、關鍵字(判斷數據庫、表是否存在)
-查看數據庫中是否存在數據庫 存在則刪除 否則直接創建
if exists (select * from sys.databases where name ='jhdx') drop database jhdx go
這裏的刪除數據庫使用drop,並且後面需要使用go關鍵字執行
-同理創建表時候需要判斷表是否存在於數據庫當中,存在則刪除,否則直接創建
if exists (select * from sys.objects where name = 'jhdx_class') drop table jhdx_class go
同理需要帶上關鍵字go
四、 char nchar varchar nvarchar 區別(存儲漢字需要字節)
char: 固定長度,存儲ANSI字符,不足的補英文半角空格。 --兩個字節
nchar: 固定長度,存儲Unicode字符,不足的補英文半角空格 --一個字節
varchar: 可變長度,存儲ANSI字符,根據數據長度自動變化。 -- 兩個字節
nvarchar: 可變長度,存儲Unicode字符,根據數據長度自動變化。 -- 一個字節
五、刪除 修改表
-drop table table_name 刪除表名爲table_name的表
- alter table add constraint pk_testId primary key (testId)
六、數據庫文件
-數據庫文件包括
數據文件(主數據文件有且只有一個.mdf)
次要數據文件(0到多個.ndf)
日誌文件 (一個或多個.ldf)
七、創建數據庫(首先要判斷是否存在)
-create database database_name
-create database database_name
on (
- name 名稱
- filename 物理名稱(路徑)
- maxsize 最大值
- size 初始值
- filegrowth 增長方式
)
log on(
- name
- filename
- maxsize
- size
- filegrowth
)
八、常見約束
- 主鍵 primary key
- 外鍵 foreign key / references
- 唯一性 unique
- 檢查 check / check(age>=0 and age<= 100)
- 默認 default
- 自增長 identity(1,2) 初始值,步長
- 非空 not null
**************************
-增加約束 alter table add constraint 約束名稱(eg:unique , check) (屬性值) or for(字段)
-詳細語法見testTable中的添加約束方法 p156-p162
*/
USE jhdx
go
-- 建表
IF EXISTS (
SELECT
*
FROM
sys.objects
WHERE
name = 'jhdx_class'
) DROP TABLE jhdx_class
go
CREATE TABLE jhdx_class(
classId INT PRIMARY KEY IDENTITY,
className VARCHAR(20) NOT NULL,
createTime DATE NOT NULL DEFAULT getdate() ,
updateTime smalldatetime NOT NULL DEFAULT getdate(),
testTime datetime NOT NULL DEFAULT getdate()
)
-- 創建數據庫student
IF EXISTS (SELECT * FROM sys.objects WHERE name ='jhdx_student') DROP TABLE jhdx_student
go
CREATE TABLE jhdx_student(
id INT PRIMARY KEY IDENTITY(1,2), -- identity(1,2) 起步爲1 步長爲2
studentName nvarchar(20) UNIQUE, -- unique 唯一性約束 可爲空 但不可重複
studentAge INT CHECK(studentAge >= 0 AND studentAge <= 100) , --check 約束,屬性滿足範圍,(在這裏studentAge 0 ~ 100)
studentSex nvarchar(1) CHECK(studentSex = '男' OR studentSex = '女'), -- check約束 studentSex 只能有男或女
createTime smalldatetime NOT NULL DEFAULT getdate(),
classId int foreign key references jhdx_class(classId) --添加外鍵 外鍵爲jhdx_class表中的classId
)
-- ************************** test ********************************
-- 已存在的表 進行修改
if exists (select * from sys.objects where name ='test') drop table test
go
create table test(
testId int not null,
testName nvarchar(20) not null
)
-- 對test表的testId增加主鍵約束
-- 首先進行判斷主鍵是否存在 存在刪除 否則添加
if exists (select * from sys.objects where name = 'pk_testId')
alter table test drop constraint pk_testId
go
alter table test
add constraint pk_testId primary key (testId),
constraint uk_testName unique (testName)
--************************ testTable ***************************************
-- 對已經存在的表 修改 增加多個約束
if exists (select * from sys.objects where name ='testTable') drop table testTable
go
create table testTable (
id int not null,
name nvarchar(20) not null,
age int not null,
sex nchar(1),
testAddress nvarchar(200) not null,
testId int not null
)
-- 添加約束
alter table testTable
add constraint pk_testTableId primary key (id),
constraint uk_testTableName unique (name),
constraint ck_testAge check(age >= 0 and age <= 100),
constraint ck_testSex check(sex = '男' or sex ='女'),
constraint dk_testAddress default ('湖北省武漢市') for testAddress,
constraint pk_testForeignId foreign key (testId) references test(testId)
go
-- ************************* 刪除表*************************************
-- drop table table_name 首先要判斷表是否存在外鍵 如果存在則先刪除外鍵表
-- 查詢該表中數據
SELECT * FROM jhdx_class
sql server 的T-SQL 學習筆記(一)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.