《数据库》-基础

-------创建数据库----------
create database ceshi
on primary(
  name = 'ceshi',
  filename = 'F:\数据库\ceshi.mdf',
  size = 5mb,
  filegrowth = 1mb
)
log on(
  name = 'ceshi_log',
  filename = 'F:\数据库\ceshi_log.ldf',
  size = 1mb,
  filegrowth = 10%
)
-------创建表--------------
use ceshi;

create table ClassInfo(
  cId int not null primary key identity(1,1),
  cTitle nvarchar(10)
)

create table StudentInfo(
  [sId] int not null primary key identity(1,1),
  sName nvarchar(10) not null,
  sGender char(6),
  sBirthday datetime,
  sPhone char(11),
  sEmail varchar(20),
  cid int not null,
  foreign key(cid) references ClassInfo(cid)
)

-------查看表--------------
select * from ClassInfo
select * from StudentInfo
--------添加约束-------------
------手动删除一列------
alter table StudentInfo
drop column QQ
------手动添加一列-----
alter table StudentInfo
add sPhone char(11)
------手动修改列的数据类型----
alter table StudentInfo
alter column sPhone char(12)
------为ClassInfo添加一个主键约束---------
alter table ClassInfo
add constraint PK_cid primary key(cId)
------为StudentInfo添加外键约束---------
alter table StudentInfo
add constraint FK_sCid foreign key(cid) references ClassInfo(cid)
------非空约束,为sGender增加一个非空约束---
alter table StudentInfo
alter column sGender char(6) not null
------为sName增加一个唯一约束--------
alter table StudentInfo
add constraint UQ_sName unique(sName)
------为性别增加一个默认约束,默认为'男'---
alter table StudentInfo
add constraint DF_sGender default('男') for sGender
------为年龄增加一个检查约束:年龄必须在0-120岁之间
alter table StudentInfo
add constraint CK_sAge check(sAge >= 0 and sAge <= 120)
------删除约束--------
alter table StudentInfo
drop constraint [FK__StudentInfo__cid__1273C1CD]

 

-------对数据增删改查------
-----增-------
insert into StudentInfo
values('张三','男',1996-2-1,'2312@qq.com',1,'13223455432')
-----查-------
select * from StudentInfo
-----删-------
delete from StudentInfo where sName = '张三'

truncate table StudentInfo ---删除表内容,并释放空间

drop table StudentInfo ---删除表内容和结构

-----改-------
update StudentInfo set sName = '李四' where sId = 4

转载于:https://www.cnblogs.com/lg804721251/p/7753927.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值