//导入数据并创立数据库
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2008 */
/* Created on: 2017/12/8 16:20:02 */
/*==============================================================*/
create database nba
on
(name=nba_data,--数据文件的逻辑名称,注意不能与日志逻辑同名
filename='d:\sql_data\nba_data.mdf' ,--物理名称,注意路径必须存在
size=10,--数据初始长度为M
maxsize=50,--最大长度为M
filegrowth=5%)--数据文件每次增长%
log on
( name=nba_log,
filename='d:\sql_data\nba_log.ldf ' ,
size=2 ,
maxsize=5 ,
filegrowth=1)
go
use NBA
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('coach') and o.name = 'FK_COACH_RELATIONS_COACH')
alter table coach
drop constraint FK_COACH_RELATIONS_COACH
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('coach') and o.name = 'FK_COACH_TEACH_TEAM')
alter table coach
drop constraint FK_COACH_TEACH_TEAM
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('player') and o.name = 'FK_PLAYER_BELONGS T_TEAM')
alter table player
drop constraint "FK_PLAYER_BELONGS T_TEAM"
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('player') and o.name = 'FK_PLAYER_TRAINS_COACH')
alter table player
drop constraint FK_PLAYER_TRAINS_COACH
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('team') and o.name = 'FK_TEAM_TEACH2_COACH')
alter table team
drop constraint FK_TEAM_TEACH2_COACH
go
if exists (select 1
from sysindexes
where id = object_id('coach')
and name = 'teach_FK'
and indid > 0
and indid < 255)
drop index coach.teach_FK
go
if exists (select 1
from sysindexes
where id = object_id('coach')
and name = 'Relationship_3_FK'
and indid > 0
and indid < 255)
drop index coach.Relationship_3_FK
go
if exists (select 1
from sysobjects
where id = object_id('coach')
and type = 'U')
drop table coach
go
if exists (select 1
from sysindexes
where id = object_id('player')
and name = 'belongs to_FK'
and indid > 0
and indid < 255)
drop index player."belongs to_FK"
go
if exists (select 1
from sysindexes
where id = object_id('player')
and name = 'trains_FK'
and indid > 0
and indid < 255)
drop index player.trains_FK
go
if exists (select 1
from sysobjects
where id = object_id('player')
and type = 'U')
drop table player
go
if exists (select 1
from sysindexes
where id = object_id('team')
and name = 'teach2_FK'
and indid > 0
and indid < 255)
drop index team.teach2_FK
go
if exists (select 1
from sysobjects
where id = object_id('team')
and type = 'U')
drop table team
go
/*==============================================================*/
/* Table: coach */
/*==============================================================*/
create table coach (
cname varchar(256) not null,
tname varchar(256) null,
coa_cname varchar(256) null,
cscore float(10) null,
constraint PK_COACH primary key nonclustered (cname)
)
go
/*==============================================================*/
/* Index: Relationship_3_FK */
/*==============================================================*/
create index Relationship_3_FK on coach (
coa_cname ASC
)
go
/*==============================================================*/
/* Index: teach_FK */
/*==============================================================*/
create index teach_FK on coach (
tname ASC
)
go
/*==============================================================*/
/* Table: player */
/*==============================================================*/
create table player (
appearnum float(10) null,
rebonds float(10) null,
assists float(10) null,
pscore float null,
pname varchar(256) not null,
cname varchar(256) null,
tname varchar(256) null,
constraint PK_PLAYER primary key nonclustered (pname)
)
go
/*==============================================================*/
/* Index: trains_FK */
/*==============================================================*/
create index trains_FK on player (
cname ASC
)
go
/*==============================================================*/
/* Index: "belongs to_FK" */
/*==============================================================*/
create index "belongs to_FK" on player (
tname ASC
)
go
/*==============================================================*/
/* Table: team */
/*==============================================================*/
create table team (
twin float(10) null,
lose float(10) null,
cname varchar(256) not null,
cname2 varchar(256) null,
constraint PK_TEAM primary key nonclustered (cname)
)
go
/*==============================================================*/
/* Index: teach2_FK */
/*==============================================================*/
create index teach2_FK on team (
cname2 ASC
)
go
alter table coach
add constraint FK_COACH_RELATIONS_COACH foreign key (coa_cname)
references coach (cname)
go
alter table coach
add constraint FK_COACH_TEACH_TEAM foreign key (tname)
references team (cname)
go
alter table player
add constraint "FK_PLAYER_BELONGS T_TEAM" foreign key (tname)
references team (cname)
go
alter table player
add constraint FK_PLAYER_TRAINS_COACH foreign key (cname)
references coach (cname)
go
alter table team
add constraint FK_TEAM_TEACH2_COACH foreign key (cname2)
references coach (cname)
go
ER设计图
由ER图生成的物理模型图