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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值