创建数据库:
IF DB_ID(N'CRAZY')IS NOT NULL DROP DATABASE CRAZY
CREATE DATABASE CRAZY
ON (NAME='CRAZY_DATA',
FILENAME='F:\\工作数据文件\\MicrosoftSQLServer2005文件\\CRAZY.MDF',
SIZE=10MB,
MAXSIZE=50MB,
FILEGROWTH=5%)
LOG ON
(NAME='CRAZY_Log',
FILENAME='F:\\工作数据文件\\MicrosoftSQLServer2005文件\\CRAZY_Log.ldf',
SIZE=2MB,
MAXSIZE=5MB,
FILEGROWTH=1MB)
在建立好的数据库中创建相关联的数据库:
use CRAZY
create table S
(
Sno char(5) not null primary key(Sno),
Sname varchar(20),
Sage smallint check(Sage>=15 and Sage<=45),
Ssex char(2) default'男'check(Ssex='男'or Ssex='女'),
Sdept char(2)
);
create table C
(
Cno char(2)not null primary key,
Cname varchar(20),
Cpno char(2),
Ccredit smallint
);
create table SC
(
Sno char(5) not null foreign key references S(Sno),
Cno char(2) not null foreign key references C(Cno),
Grade smallint check((Grade is null) or (Grade between 0 and 100)),
primary key(Sno,Cno),
)
insert into S values('00001','aaa',15,'男','CS')
insert into S values('00002','bbb',15,'男','CS')
insert into S values('00003','ccc',15,'女','CS')
insert into S values('00004','ddd',15,'男','CS')
insert into S values('00005','eee',15,'男','CS')
insert into S values('00006','fff',15,'女','CS')
insert into C values('11','11aaa',12,3)
insert into C values('12','12aaa',13,3)
insert into C values('13','13aaa',11,3)
insert into C values('14','14aaa',15,2)
insert into C values('15','15aaa',12,3)
insert into C values('16','16aaa',15,2)
insert into SC values('00001','11',90)
insert into SC values('00001','12',90)
insert into SC values('00001','13',90)
insert into SC values('00002','11',90)
insert into SC values('00002','14',90)
insert into SC values('00002','15',90)
insert into SC values('00003','11',90)
将TXT文件导入数据库:
BULK INSERT ZIYUAN
FROM 'E:\\Document\\Temporary Document\\临时数据文件\\出租车数据\\粤B000H6.txt'
WITH ( FIELDTERMINATOR =',', ROWTERMINATOR= ',\\n')
更改数据库的语句: 在表S中添加一个不允许空值的列,列名为s_entrance,而且没有通过default 定义提供的值:
alter table S add s_entrance datetime not null default(getdate())
在表S中删除列s_entrance
如果s_entrance有约束的话先要删除约束。
alter table S drop column s_entrance
将表S中的Sage的smallint更改为int类型。
如果s_entrance有约束的话先要删除约束。
alter table S alter column Sage int
给表S的Sname添加一个unique约束。(Management Studio:索引和键)
alter table S add constraint name_unique unique(Sname)
删除name_unique约束。
alter table S drop constraint name_unique
将表S中的Sage的默认值定为15,取值在15-22之间
alter table S add constraint default_age default 15 for Sage
因篇幅问题不能全部显示,请点此查看更多更全内容