SQL Server复习 2013.12.16
--新建数据库Student
create database Student
on primary
(
name='Studeng',
filename='D:\study\course\Database\FileGroup\Student.mdf',
size=20mb,
maxsize=100mb,
filegrowth=10mb
)
log on
(
name='Student_log',
filename='D:\study\course\Database\FileGroup\Student_log.ldf',
size=2mb,
maxsize=5mb,
filegrowth=1mb
)
go
--创建表
use Student
create table S
(
sno nchar(5),
sname nvarchar(3),
ssex nchar(1),
sbirth datetime,
Sdept nchar(2),
primary key (sno)
)
create table C
(
cno tinyint primary key,
cname nvarchar(5),
cpno tinyint,
Ccredit tinyint
)
create table SC
(
sno nchar(5),
cno tinyint,
grade tinyint,
primary key (sno, cno)
)
go
--备份数据库Student
backup database "Student" to disk='D:\study\course\Database\BackUp\Student.bak'
--删除表
use Student
drop table C,S,SC
go
--删除数据库
use master
drop database Student
go
--还原数据库
use master
restore database "Student" from disk='D:\study\course\Database\BackUp\Student.bak'
go
--为SC表添加外键约束
use Student
alter table SC
add constraint fk_sno foreign key(sno) references S(sno)
alter table SC
add constraint fk_cno foreign key(cno) references C(cno)
go
--删除S表中创建的约束或者索引
use Student
alter table S
drop constraint PK_sno
alter table S
drop constraint DF_S_ssex
alter table S
drop constraint uk_sname
alter table S
drop constraint ck_sbirth
alter table S
drop constraint IX_birth
go
--创建一个ssex_rule规则,将其绑定到S表的ssex性别字段上
use Student
go
create rule ssex_rule
as @ssex in (N'男', N'女')
go
--将该规则绑定到S表的ssex性别字段上
use Student
exec sp_bindrule 'ssex_rule', 'S.ssex'
go
--验证
use Student
insert into S(ssex, sno)
values(N'中', N'00001')
go
use Student
insert into S(sno, ssex, sname)
values(N'00002', N'中', N'张三')
go
--删除ssex_rule规则
use Student
exec sp_unbindrule 'S.ssex'
go
--创建一个ssex_def默认对象,将其绑定到S表的ssex性别字段上,使其默认值为男
use Student
go
create default ssex_def as N'男'
go
exec sp_bindefault 'ssex_def', 'S.ssex'
go
--解除默认对象绑定
exec sp_unbindefault 'S.ssex'
go
--删除ssex_def
drop default ssex_def
use Student
select * from S
go
mysql查看表结构命令,如下:
desc 表名;
show columns from 表名;
describe 表名;
show create table 表名;
use information_schema
select * from columns where table_name='表名';
顺便记下:
show databases;
use 数据库名;
show tables;