休息了好長一段時間,這幾天照著書本自己慢慢敲的命令,看的再多不如手動去做,
use HrSystem
go
create table Employees
(
Em_id int primary key identity(1,1),--設定這個列為主鍵,并且為唯一標識列--
Emp_name varchar(50) not null, --不為空--
Sex char(2) DEFAULT('男'), -- 設定這個列默認為男 --
Title varchar(20) not null,
Wage float default(0),
IdCard varchar(20),
Dep_id int not null
)
--查看資料庫的儲存空間--
sp_spaceused N'Employees'
--使用dbcc checkident命令檢查和設定表的標識值--
use HrSystem
go
create table tmpTable
( id int primary key identity,
name varchar(50)
)
insert into tmpTable values('a')
insert into tmpTable values('b')
insert into tmpTable values('c')
insert into tmpTable values('d')
go
dbcc checkident(tmpTable,noreseed)
go
--使用delete陳述句洗掉tmpTable表中的兩條資料--
delete from tmpTable where id>2
--使用下面的陳述句重置識別符號為2--
dbcc checkident(tmpTable,reseed,2)
--驗證識別符號的值--
insert into tmpTable values('c')
--重命名表名--
--語法:sp_rename 原物件名,新物件名,物件型別--
--存盤程序使用sp_rename將Employees重命名為EmpInfo--
use HrSystem
go
sp_rename Employees,EmpInfo
--修改表的列名,將表名--
/*使用存盤程序sp_rename時,使用column對列名進行重命名
將表名Employees列Wage重命名為Salary*/
sp_rename 'Employees.Wage','Salary','column'
--向表中添加列--
--使用alter table 陳述句向表中添加列--
--alter table 表名 add 列名 資料型別和長度 列屬性
在Employees中增加列,列名為Tele,資料型別為varchar,長度為50,列屬性允許為空
use HrSystem
go
alter table Employees add Tele varchar(50) null
--修改列屬性--
--alter table 表名 alter column 列名 新資料型別和長度 新列屬性
--在表Employees中修改列Tele的資料值為char,長度為30,并允許為空
alter table Employees alter column Tele char(30) null
--洗掉表中列--
alter table 表名 drop column 列名
--在表中Employees中洗掉列Tele
alter table Employees drop column Tele
--洗掉表--
drop table 表名
--洗掉表Departments
drop table Departments
--創建主鍵約束--
/*主鍵是表中的一列或者一組列,他們的值唯一的標識標中的每一行,在創建和修改表時,可以定義主鍵約束,
主鍵的列的值不允許為空*/
constraint 主鍵名 primary key [clustered|nonclustered]
(列名1,[列名2])
--在HrSystem中建立表Super,并使用constraint子句定義主鍵PK_test
use HrSystem
go
create table Super
( Super_id int,
Super_name varchar(50),
constraint pk_Super primary key(Super_id)
)
--修改主鍵約束--
add constraint 主鍵名 primary key [clustered|nonclustered]
(列名1,[列名2,])
--創建student,然后將Stuid列設定為主鍵
create table Student
(Stuid int identity(1,1),
StuName varchar(50) not null,
Sex bit default(0),
Class varchar(50) not null,
Score float default(0),
)
go
alter table Student
add constraint pk_Stu primary key nonclustered (Stuid)
go
--洗掉主鍵約束--
alter table 表名 drop constraint <primary key 約束名>
洗掉表Student主鍵約束PK_Stu
alter table Student drop constraint PK_Stu
--創建、修改、洗掉唯一約束性
--唯一性約束可以保證除主鍵外的其他一個或者多個列的資料唯一性,以防止在列中輸入重復的值
--在Departents中的Dep_name列定義唯一性約束,在表中插入兩條相同的資料,查看提示不允許插入重復值
/*該示例在表 PasswordHash 中的 PasswordSalt 和 Person.Password列上創建唯一約束,*/
USE AdventureWorks2012;
GO
ALTER TABLE Person.Password
ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt);
GO
/*上條命令來自SQL SERVER示例*/
use HrSystem
go
alter table Departments
add constraint Dep_name unique (Departments)
--創建表Facker,將Facker_name設定為唯一性約束代碼
use HrSystem
go
create table Facker
(Facker_id int,
Facker_name varchar(50),
constraint PK_Facker primary key(Facker_id),
constraint IX_Facker unique(Facker_name)
)
--洗掉表Facker唯一約束IK_Facker
alter table Facker drop constraint IK_Facker
--從sys.key_constraints獲取約束資訊--
--系統視圖 sys.key_constraints用來保存主鍵約束和唯一約束資訊--
use HrSystem
go
select * from sys.key_constraints
go
--單獨查詢某一個約束資訊
select * from sys.key_constraints where name='pk_Super'
--違反約束檢查--
/*constraint 約束名
check [not for replication]
(邏輯運算式)
*/
/*創建Sutentone表,設定索引鍵和check約束*/
use HrSystem
GO
create table Sutentone
(Stuid int identity(1,1),
StuName varchar(50) not null,
Sex bit default(0),
Class varchar(50) not null,
Score float default(0),
constraint PK_Stdent primary key(Stuid),
constraint ix_Stdent unique(StuName),
constraint ck_Stdent check(Score>=0),
)
go
/*創建Client表,同時創建檢查約束,定義郵政編碼Postcode是由6位數字組成的字串*/
use HrSystem
go
create table Client
(id int identity(1,1),
CitOrg varchar(50) not null,
address varchar(100) not null,
Postcode varchar(10) not null,
constraint pk_Client primary key(id),
constraint ix_Client unique(CitOrg),
constraint ck_Client check(Postcode like'[0-9][0-9][0-9][0-9][0-9][0-9]'),
--like是SQL SERVER關鍵字,用于對指定的字串進行模式匹配,[0-9]指定單字符在0至9的范圍,--
)
--修改約束檢查--
use HrSystem
go
alter table Employees
add constraint ck_sex check (Sex='男' or Sex='女')
--洗掉檢查約束--
--洗掉表Employees中ck_Sex的約束--
use HrSystem
go
alter table Employees
drop constraint ck_Sex
go
--從information_schema.check_constraints獲取檢查約束資訊--
--從系統視圖中information_schema.check_constraints可以查看當前資料庫中當前用戶 有權限查看的所有檢查約束資訊--
select * from information_schema.check_constraints
--創建和使用默認約束--
constraint 約束名
default 約束運算式 [for 列名]
--在表Employees中的列Title的默認約束為‘職員’--
use HrSystem
go
alter table Employees
add constraint de_Title default '職員' for Title
go
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/445394.html
標籤:SQL Server
