SQL Server学习1(建数据库,建表,建约束)
--建數(shù)據(jù)庫(kù)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
--創(chuàng)建表cardInfo會(huì)員卡信息表
create table cardInfo
(
CardId int not null identity(1,1),--會(huì)員編號(hào)
CardNumber nvarchar(50) not null,--會(huì)員號(hào)
CardPassword varchar(50) not null,--密碼
CardBalance int not null,--余額
TransacTime datetime --辦理時(shí)間
)
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字段的數(shù)據(jù)類型從int類型修改為decimal(6,2)
alter table cardInfo
?? alter column CardBalance decimal(6,2)
go
--修改列的可空性
alter table cardInfo
? alter column TransactTime datetime null
go
--修改列的名稱(用系統(tǒng)的存儲(chǔ)過(guò)程sp_rename)
EXEC sp_rename'cardInfo.TransactTime','CreateTime','column'
go
--2.4.3刪除表(用drop關(guān)鍵字)
if exists(select * from sys.sysobjects where [name]='cardInfo')
drop table cardInfo
go
--另一種簡(jiǎn)單的方式判斷表是否存在
if OBJECT_ID('cardInfo') is not null
?drop table cardInfo
go
/*sys.sysobjects和sys.sysdatabases都屬于SQLServer中的系統(tǒng)視圖
OBJECT_ID類似DB_ID,它是一個(gè)系統(tǒng)函數(shù),用于返回?cái)?shù)據(jù)庫(kù)對(duì)象的標(biāo)識(shí)號(hào),對(duì)象名如
表名,約束名,存儲(chǔ)過(guò)程名,視圖名等
*/
--添加約束
--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.添加默認(rèn)值約束
--為PCInfo表的PCNote字段建立默認(rèn)值約束
alter table PCInfo
?add constraint FK_PCInfo_PCNote default '這臺(tái)電腦不錯(cuò)' 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.外鍵約束:命名規(guī)則:FK_從表名_主表名_外鍵字段名
--為recordInfo表和cardInfo表建立外鍵關(guān)系
alter table recordInfo
? add constraint FK_recordInfo_cardInfo_CardId
??? foreign key(CardId) references cardInfo(CardId)
go
--亦可以使用一條ALter Table語(yǔ)句,添加多個(gè)約束
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
--另一種簡(jiǎn)單的方式判斷表是否存在
if OBJECT_ID('PK_cardInfo_CardId') is not null
?alter table cardInfo
?? drop? constraint PK_cardInfo_CardId
go
--建表:
--創(chuàng)建表cardInfo會(huì)員卡信息表
create table cardInfo
(
CardId int not null identity(1,1) primary key,--會(huì)員編號(hào)
CardNumber nvarchar(50) not null unique,--會(huì)員號(hào)
CardPassword varchar(50) not null check(len(CardPassword)>6),--密碼
CardBalance int not null default'50',--余額
TransacTime datetime not null default getdate() --辦理時(shí)間
)
go
--創(chuàng)建計(jì)算機(jī)信息表(PCInfo)
create table PCInfo
(
PCId int not null identity(1,1) primary key,--計(jì)算機(jī)編號(hào)
PCUse int not null default'0' check(PCUse=0 or PCUse=1),--計(jì)算機(jī)是否使用
CardNote varchar(30) default'這臺(tái)機(jī)器不錯(cuò)' --計(jì)算機(jī)的描述
)
go
--創(chuàng)建記錄信息表(recordInfo)
create table recordInfo
(
RecordId int not null identity(1,1) primary key,--記錄編號(hào)
CardId? int not null foreign key references CardInfo(CardId),--會(huì)員卡編號(hào)
PCId? int not null foreign key references PCInfo(PCId),--計(jì)算機(jī)編號(hào)
BeginTime datetime not null default getdate(),--開(kāi)始上機(jī)時(shí)間。
EndTime datetime not null? ,--結(jié)束下機(jī)時(shí)間,上機(jī)時(shí)間應(yīng)早于下機(jī)時(shí)間
Free int not null Check(Free>0),--上機(jī)費(fèi)用
)
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,--用戶編號(hào)
UserName varchar(20) not null unique,--用戶名
UserPwd varchar(20) not null check(len(UserPwd)>6) --用戶密碼
)
go
轉(zhuǎn)載于:https://www.cnblogs.com/jiHuaJiao/archive/2012/11/20/2779880.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的SQL Server学习1(建数据库,建表,建约束)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 浅谈 Request Response
- 下一篇: SQLite的事务和锁