数据库——SQL-SERVER CREATE-TABLES

 

给出数据库实验所需要的“CREATE-TABLES.SQL”文件

use master
go

if exists (select * from dbo.sysdatabases where name = 'STUDB') 
drop database STUDB
GO

create database STUDB
go
use STUDB
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SC]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SC]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[STUDENT]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[STUDENT]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[COURSE]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[COURSE]
GO





CREATE TABLE STUDENT
(
    SNO  NUMERIC(5)  CONSTRAINT P_STUDENT PRIMARY KEY,
    SNAME CHAR(6) NOT NULL,
        SSEX  CHAR(2)  DEFAULT ''
                CONSTRAINT C_SSEX CHECK( SSEX IN ('','')),
    SAGE  NUMERIC(2) DEFAULT 20,
        SDEPT CHAR(10)
      
);


CREATE TABLE COURSE
(
    CNO  NUMERIC(2)  CONSTRAINT P_COURSE PRIMARY KEY,
    CNAME CHAR(10) NOT NULL  CONSTRAINT U_CNAME UNIQUE,
    CPNO NUMERIC(2)   CONSTRAINT F_CPNO REFERENCES COURSE(CNO),
    CCREDIT NUMERIC(2)
);

CREATE TABLE SC
(
    SNO  NUMERIC(5)  REFERENCES STUDENT,
    CNO NUMERIC(2)  REFERENCES COURSE(CNO),
    GRADE  NUMERIC(6,2),
        PRIMARY KEY(SNO,CNO) 
);



insert INTO STUDENT  values(  95001,'李勇','',20,'CS');
insert INTO  STUDENT  values( 95002,'刘晨','',19,'IS');
insert  INTO STUDENT  values( 95003,'王敏','',18,'MA');
insert  INTO STUDENT  values( 95004,'张立','',21,'IS');
insert  INTO STUDENT  values( 95005,'周斌','',18,'CS');
insert  INTO STUDENT  values( 95006,'孙兵','',19,'CS');



insert  INTO COURSE  values( 2,'数学',NULL,2);
insert  INTO COURSE  values( 6,'数据处理',2,2);
insert  INTO COURSE  values( 4,'操作系统',6,3);

insert  INTO COURSE  values( 7,'PASCAL',6,4);

insert  INTO COURSE  values( 5,'数据结构',7,4);

insert  INTO COURSE  values( 1,'数据库原理',5,4);

insert  INTO COURSE  values( 3,'信息系统',1,4);





insert  INTO SC  values( 95001,1,92);
insert  INTO SC  values( 95001,2,85);
insert  INTO SC  values( 95001,3,88);
insert  INTO SC  values( 95002,2,90);
insert  INTO SC  values( 95002,3,80);
insert  INTO SC  values( 95003,1,80);
insert  INTO SC  values( 95004,1,75);
insert  INTO SC  values( 95005,1,96);
insert  INTO SC  values( 95003,2,NULL);
insert  INTO SC  values( 95003,4,NULL);


go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GSTAGE]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[GSTAGE]
GO

CREATE TABLE GSTAGE
(
    LOW   NUMERIC(3)   ,
               HIGH  NUMERIC(3)   ,
    STAGE CHAR(2)  
);



insert  INTO  GSTAGE  values( 90,    100,    '');
insert  INTO  GSTAGE  values( 80,    89,    '');
insert  INTO  GSTAGE  values( 70,    79,    '');
insert  INTO  GSTAGE  values( 60,    69,    '');
insert  INTO  GSTAGE  values(0,    59,    '');

GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SPJ]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SPJ]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[P]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[P]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[S]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[S]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[J]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[J]
GO





CREATE TABLE S
(
    SNO  CHAR(2),
    SNAME  CHAR(10),
    CITY  CHAR(10)

);



CREATE TABLE P
(
    PNO  CHAR(2),
    PNAME  CHAR(10),
    COLOR  CHAR(10)

);
CREATE TABLE J
(
    JNO  CHAR(2),
    JNAME  CHAR(10)

);

CREATE TABLE SPJ
(
    SNO  CHAR(3),
    JNO  CHAR(3),
    PNO  CHAR(3),
    QTY NUMERIC(4)
);

INSERT INTO S VALUES('S1','S-A','天津');
INSERT INTO S VALUES('S2','S-B','天津');
INSERT INTO S VALUES('S3','S-C','北京');
INSERT INTO S VALUES('S4','S-D','北京');


INSERT INTO P VALUES('P1','P-A','');
INSERT INTO P VALUES('P2','P-B','');
INSERT INTO P VALUES('P3','P-C','');


INSERT INTO J VALUES('J1','J-A');
INSERT INTO J VALUES('J2','J-B');
INSERT INTO J VALUES('J3','J-C');


INSERT INTO SPJ VALUES('S1','J1', 'P1',5 );
INSERT INTO SPJ VALUES('S1','J1', 'P2',6 );
INSERT INTO SPJ VALUES('S1','J1', 'P3',7 );
INSERT INTO SPJ VALUES('S2','J2', 'P1',9 );
INSERT INTO SPJ VALUES('S2','J2', 'P3',6 );


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ACCOUNT]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ACCOUNT]
GO


CREATE TABLE ACCOUNT
(
    ACCOUNTNUM INT  PRIMARY KEY,
    TOTAL  NUMERIC(10,2) 

);



insert INTO ACCOUNT  values( 2001,20000);
insert INTO ACCOUNT  values( 3001,500);
insert INTO ACCOUNT  values( 3663,1000);

GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DELETEDSTU]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[DELETEDSTU]
GO


CREATE TABLE DELETEDSTU
(
    SNO  NUMERIC(5) ,
    SNAME CHAR(6) ,
              SSEX  CHAR(2) ,
     SAGE  NUMERIC(2),
              SDEPT CHAR(10),
   CCOUNT NUMERIC(3),   
        GTOTAL NUMERIC(5)   
);



GO

 

posted @ 2019-05-10 12:40  王陸  阅读(2060)  评论(0编辑  收藏  举报