SQL Server database

USE master -- set the current database as master to release the occupation of Students
GO
IF  EXISTS(SELECT * FROM sys.databases WHERE name='Students')
DROP DATABASE Students

--Create a Students database
CREATE DATABASE Students
on primary
(
	 -- The specific description of the data file
	 NAME='Students_data',
	 FILENAME='D:\Students_data.mdf',
	 SIZE=10MB,
	 MAXSIZE=100MB,
	 FILEGROWTH = 10%
)
LOG ON
(
	 --The specific description of the log file, the meaning of each parameter is the same as above
	 NAME='Students_log',
	 FILENAME='D:\Students_log.ldf',
	 SIZE=10MB
)
GO

GO
IF EXISTS(SELECT * FROM sys.sysobjects WHERE xtype = 'U' AND name='StuInfo')
DROP TABLE StuInfo

USE Students -- set the current database to Students
CREATE TABLE StuInfo            
(
	-- Create student information sheet
	StuID int NOT NULL PRIMARY KEY, -- student ID, primary key
	StuName varchar(10) NOT NULL, -- student name, not null
	StuSex char(2) NOT NULL DEFAULT('male'), -- student gender, not null	
	CHECK (StuSex ='男' or StuSex='女' )
)
GO

GO
IF EXISTS(SELECT * FROM sys.sysobjects WHERE xtype = 'U' AND name='StuMarks')
DROP TABLE StuMarks

USE Students -- set the current database to Students
CREATE TABLE StuMarks
(
	-- Create student grade sheet
	ExamNo int identity(1,1) primary key, -- exam number, identity
	StuID int NOT NULL references StuInfo(StuID), -- student ID, foreign key
	Course varchar(10) not null, -- exam subject
	Score int default(0), -- test score
)
GO

GO
USE Students -- set the current database to Students
INSERT INTO StuInfo (StuID,StuName,StuSex)
SELECT 1,'Zhang San','Male' UNION
SELECT 2,'Li Si','Male' UNION
SELECT 3,'Qian Qi','Female' UNION
SELECT 4,'Wang Wu','Female' UNION
SELECT 5,'Zhao Liu','male'
GO

GO
USE Students -- set the current database to Students
INSERT INTO StuMarks (StuID,Course,Score)
SELECT 1,'language',70 UNION
SELECT 1,'Math',89 UNION
SELECT 2,'language',33 UNION
SELECT 2,'math',50 UNION
SELECT 3,'language',79 UNION
SELECT 3,'Math',90 UNION
SELECT 4,'language',88 UNION
SELECT 4,'math',74 UNION
SELECT 5,'language',64 UNION
SELECT 5,'math',92
GO

GO
SELECT * FROM StuInfo;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325151805&siteId=291194637