- 1、本文档共14页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
SQL serer 创建表练习
实验二 创建数据库和表
/* 一、创建DB_student数据库 */
/*如果DB_student数据库存在,删除它*/
Use master
If exists (select name from master.dbo.sysdatabases where name=DB_student)
Drop database DB_student
Go
/* 以下创建DB_student数据库*/
Create database DB_student
On primary
(name=DB_student_data,
Filename=c:\program files\microsoft sql server\mssql\data\DB_student_data.mdf,
Size=5mb,
Maxsize=50mb,
Filegrowth=10%)
Log on
(name=DB_student_log,
Filename=c:\program files\microsoft sql server\mssql\data\DB_student_data.ldf,
Size=5mb,
Maxsize=25mb,
Filegrowth=10%)
Go
/* 在DB_student数据库中创建student表 */
/*打开DB_student数据库*/
use DB_student
Go
/*在DB_student数据库中,如果student表存在,就删除*/
if exists ( select * from dbo.sysobjects where id=object_id(student)
and OBJECTPROPERTY(id,IsUserTable)=1)
drop table student
Go
/*以下创建student 数据库表*/
create table Student
( sno char(9) Constraint PK_student_sno primary key,
Sname char(20),
ssex char(2),
sage smallint,
sdept char(20)
);
Go
/* 在DB_student数据库中创建course表 */
/*在DB_student数据库中,如果course数据库表存在,就删除*/
if exists ( select * from dbo.sysobjects where id=object_id(course)
and OBJECTPROPERTY(id,IsUserTable)=1)
drop table course
Go
/*以下创建course 数据库表*/
create table course
( cno char(4) Constraint PK_Course_cno primary key,
cname char(40),
cpno char(4),
ccredit smallint,
/*Foreign Key cpno References Course (cno)*/
);
Go
/* 在DB_student数据库中创建SC表 */
/*在DB_student数据库中,如果SC数据库表存在,就删除*/
if exists ( select * from dbo.sysobjects where id=object_id(sc)
and OBJECTPROPERTY(id,IsUserTable)=1)
drop table sc
Go
/*以下创建sc 数据库表*/
create table sc
( sno char(9),
cno char(4),
grade smallint,
Constraint PK_sc_sno_cno primary key (sno,cno),
Constraint RFK_sc_sno Foreign Key (sno) References student (sno),
Constraint RFK_sc_cno Foreign Key (cno) References Course (cno)
);
Go
/* 为student数据表添加数据 */
Insert into student
Values (200215121,李勇,男,20,CS);
Insert into student
Values (200215122,刘
文档评论(0)