SQL Server学习1(建数据库,建表,建约束)
--建數據庫NetBarDB
IF exists(select * from sys.sysdatabases where [NAME]='NetBarDB')
??? drop database NetBarDB
go
create database NetBarDB
ON
(
name=NetBar_mdf,
FileName='E:\NetBar_mdf.mdf',
size=3MB,
maxsize=100MB,
filegrowth=1MB
)
log on
(
name=NetBar_ldf,
FileName='E:\NetBar_ldf.ldf',
size=3MB,
maxsize=100MB,
filegrowth=1MB
)
go
use NetBarDB
go
--創建表cardInfo會員卡信息表
create table cardInfo
(
CardId int not null identity(1,1),--會員編號
CardNumber nvarchar(50) not null,--會員號
CardPassword varchar(50) not null,--密碼
CardBalance int not null,--余額
TransacTime datetime --辦理時間
)
go
--更改表1。添加列
alter table cardInfo
? add Remark varchar(20) null
go
--為cardInfo表添加兩列:Remark1,Remark2.
alter table cardInfo
? add Remark1 varchar(20) null,
????? Remark2 varchar(20) null
go
--2.刪除列
alter table cardInfo
?? drop column TransacTime
go
--3.修改列:將cardInfo表的CardBalance字段的數據類型從int類型修改為decimal(6,2)
alter table cardInfo
?? alter column CardBalance decimal(6,2)
go
--修改列的可空性
alter table cardInfo
? alter column TransactTime datetime null
go
--修改列的名稱(用系統的存儲過程sp_rename)
EXEC sp_rename'cardInfo.TransactTime','CreateTime','column'
go
--2.4.3刪除表(用drop關鍵字)
if exists(select * from sys.sysobjects where [name]='cardInfo')
drop table cardInfo
go
--另一種簡單的方式判斷表是否存在
if OBJECT_ID('cardInfo') is not null
?drop table cardInfo
go
/*sys.sysobjects和sys.sysdatabases都屬于SQLServer中的系統視圖
OBJECT_ID類似DB_ID,它是一個系統函數,用于返回數據庫對象的標識號,對象名如
表名,約束名,存儲過程名,視圖名等
*/
--添加約束
--1.主鍵約束
alter table cardInfo
? add constraint PK_cardInfo_CardId primary key(cardId)
go
--2.添加唯一約束
--為cardInfo表的CardNumber字段建立唯一約束
alter table cardInfo
? add constraint UQ_cardInfo_CardNumber UNIQUE(CardNumber)
go
--3.添加默認值約束
--為PCInfo表的PCNote字段建立默認值約束
alter table PCInfo
?add constraint FK_PCInfo_PCNote default '這臺電腦不錯' for PCNote
go
--4.添加檢查約束
--為cardInfo表的CardPassword字段建立check約束
alter table cardInfo
?? add constraint CK_cardInfo_CardPassword
??? check (len(CardPassword)>0 and len(CardPassword)<=6)
go
--5.外鍵約束:命名規則:FK_從表名_主表名_外鍵字段名
--為recordInfo表和cardInfo表建立外鍵關系
alter table recordInfo
? add constraint FK_recordInfo_cardInfo_CardId
??? foreign key(CardId) references cardInfo(CardId)
go
--亦可以使用一條ALter Table語句,添加多個約束
alter table cardInfo
?? add constraint PK_cardInfo_CardId primary key(cardId),
?????? constraint UQ_cardInfo_CardNumber UNIQUE(CardNumber),
?????? constraint CK_cardInfo_CardPassword
????????? check (len(CardPassword)>0 and len(CardPassword)<=6)
go
--2.6.2刪除約束
--刪除表cardInfo主鍵約束
alter table cardInfo
? drop constraint PK_cardInfo_CardId
go
--也可以
if exists(select * from sys.sysobjects where [name]='PK_cardInfo_CardId')
?alter table cardInfo
??? drop? constraint PK_cardInfo_CardId
go
--另一種簡單的方式判斷表是否存在
if OBJECT_ID('PK_cardInfo_CardId') is not null
?alter table cardInfo
?? drop? constraint PK_cardInfo_CardId
go
--建表:
--創建表cardInfo會員卡信息表
create table cardInfo
(
CardId int not null identity(1,1) primary key,--會員編號
CardNumber nvarchar(50) not null unique,--會員號
CardPassword varchar(50) not null check(len(CardPassword)>6),--密碼
CardBalance int not null default'50',--余額
TransacTime datetime not null default getdate() --辦理時間
)
go
--創建計算機信息表(PCInfo)
create table PCInfo
(
PCId int not null identity(1,1) primary key,--計算機編號
PCUse int not null default'0' check(PCUse=0 or PCUse=1),--計算機是否使用
CardNote varchar(30) default'這臺機器不錯' --計算機的描述
)
go
--創建記錄信息表(recordInfo)
create table recordInfo
(
RecordId int not null identity(1,1) primary key,--記錄編號
CardId? int not null foreign key references CardInfo(CardId),--會員卡編號
PCId? int not null foreign key references PCInfo(PCId),--計算機編號
BeginTime datetime not null default getdate(),--開始上機時間。
EndTime datetime not null? ,--結束下機時間,上機時間應早于下機時間
Free int not null Check(Free>0),--上機費用
)
go
--Check(Convert(int,DateDiff(second,BeginTime,EndTime))>0)
--select DateDiff(second,'2012-11-20',GetDate())
--用戶信息表(userInfo)
create table userInfo
(
UserId int not null identity(1,1) primary key,--用戶編號
UserName varchar(20) not null unique,--用戶名
UserPwd varchar(20) not null check(len(UserPwd)>6) --用戶密碼
)
go
轉載于:https://www.cnblogs.com/jiHuaJiao/archive/2012/11/20/2779880.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的SQL Server学习1(建数据库,建表,建约束)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈 Request Response
- 下一篇: web.py 0.3 新手指南 - 安装