码迷,mamicode.com
首页 > 其他好文 > 详细

test

时间:2020-12-09 12:28:44      阅读:11      评论:0      收藏:0      [点我收藏+]

标签:unicode   har   课程   时间   str   实验   查看   数据库   check   

数据库课程实验

实验2 定义表和数据库完整性

(1)教务管理数据库JWGL

  • 问题:用企业管理器在教务管理数据库JWGL中创建学生表Student、课程表Course、学生选课表SC,它们的表结构如表B.1所示。

技术图片

  • sql语句
CREATE DATABASE JWGL ;

-- 教务管理数据库
USE jwgl ;

-- 查看数据库的字符集
SELECT DATABASE() ;

-- 创建学生表Student
CREATE TABLE Student (
  Sno CHAR(8) NOT NULL PRIMARY KEY,
  -- 学生学号
  Sname VARCHAR (10) NOT NULL,
  -- 学生姓名
  Sex ENUM (‘男‘, ‘女‘) NOT NULL DEFAULT ‘男‘,
  -- 性别
  Age ENUM (‘15‘,‘16‘,‘17‘,‘18‘,‘19‘,‘20‘,‘21‘,‘22‘,‘23‘,‘24‘,‘25‘,‘26‘,‘27‘,‘28‘,‘29‘,‘30‘) NOT NULL DEFAULT ‘20‘,
  Phonenumber VARCHAR (20) UNICODE,
  -- 手机号
  Sdept VARCHAR (20) NOT NULL
) ;

-- 创建课程表Course
CREATE TABLE Course (
  Cno CHAR(10) NOT NULL PRIMARY KEY,
  Cname VARCHAR (20) UNICODE NOT NULL,
  Total_perior TINYINT (1) DEFAULT ‘64‘ CHECK (Total_perior BETWEEN 32 
    AND 108),
  Week_perior TINYINT (1) DEFAULT ‘4‘ CHECK (Week_perior BETWEEN 2 
    AND 7),
  credit TINYINT (1) NOT NULL DEFAULT ‘4‘ CHECK (credit BETWEEN 1 
    AND 7),
  Pcno CHAR(10)
) ;

-- 学生选课表SC
CREATE TABLE SC (
  Sno CHAR(8) NOT NULL,
  Cno CHAR(10) NOT NULL,
  Grade TINYINT (1) CHECK (Grade BETWEEN 0 
    AND 100),
  PRIMARY KEY (Sno,Cno),
  FOREIGN KEY (Sno) REFERENCES Student (Sno),
  /*表级完整性约束条件,Sno是外码,被参照表是Student*/
  FOREIGN KEY (Cno) REFERENCES Course (Cno) 
  /*表级完整性约束条件,Sno是外码,被参照表是Course*/
) ;
  • 架构设计图

技术图片

(2)Market数据库

  • 问题:用T-SQL语句在Market数据库中创建客户基本信息表Customers、货品信息表Goods、订单信息表Orders,它们的定义如下
CREATE TABLE Customers(
         CustomerID  int  IDENTITY(1,1)  PRIMARY  KEY,
         CName  varchar(8)  NOT  NULL ,
         Address  varchar(50),
         City  varchar(10),
         Tel  varchar(20)  UNIQUE,
         Company  varchar(50),
         Birthday  datetime,
         Type  tinyint  DEFAULT 1);
CREATE TABLE Goods (
         GoodsID int  CONSTRAINT  C1 PRIMARY  KEY ,
         GoodsName  varchar(20)  NOT  NULL,
         Price  money,
         Description  varchar(200),
         Storage   int,
         Provider  varchar(50),
         Status  tinyint  DEFAULT(0));
CREATE TABLE Orders (
         OrderID  int  IDENTITY(1,1)  CONSTRAINT  C2  PRIMARY  KEY ,
         GoodsID  int  NOT NULL  REFERENCES Goods(GoodsID)  ON  DELETE
             CASCADE,
         CustomerID  int  NOT NULL  FOREIGN KEY(CustomerID)
             REFERENCES Customers (CustomerID)  ON  UPDATE  CASCADE
ON DELETE NO ACTION,
         Quantity  int  NOT NULL  CONSTRAINT  C3  CHECK(Quantity >0),
         OrderSum  money  NOT NULL,
         OrderDate  datetime  DEFAULT(getdate()) );
  • sql语句
CREATE DATABASE Market ;
-- 创建Market数据库
USE Market ;
-- 客户基本信息表Customers
CREATE TABLE Customers (
  CustomerID INT PRIMARY KEY AUTO_INCREMENT,
  CName VARCHAR (8) NOT NULL,
  Address VARCHAR (50),
  City VARCHAR (10),
  Tel VARCHAR (20) UNIQUE,
  Company VARCHAR (50),
  Birthday DATETIME,
  TYPE TINYINT DEFAULT 1
) ;
-- 货品信息表Goods
CREATE TABLE Goods (
 --  GoodsID INT CONSTRAINT C1 PRIMARY KEY,
  GoodsID INT  PRIMARY KEY,
  GoodsName VARCHAR (20) NOT NULL,
  Price DOUBLE,
  Description VARCHAR (200),
  STORAGE INT,
  Provider VARCHAR (50),
  STATUS TINYINT DEFAULT 0
) ;
-- 订单信息表Orders
CREATE TABLE Orders (
  -- OrderID INT IDENTITY (1, 1) CONSTRAINT C2 PRIMARY KEY,
  OrderID INT PRIMARY KEY AUTO_INCREMENT,
  GoodsID INT NOT NULL REFERENCES Goods (GoodsID) ON DELETE CASCADE,
  -- CustomerID INT NOT NULL FOREIGN KEY (CustomerID) REFERENCES Customers (CustomerID) ON DELETE NO ACTION,
  CustomerID INT NOT NULL,
  Quantity INT NOT NULL CHECK (Quantity > 0),
  OrderSum DOUBLE NOT NULL,
  -- OrderDate DATETIME DEFAULT(getdate ()),
  OrderDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
  FOREIGN KEY (GoodsID) REFERENCES Goods(GoodsID) 
) ;
  • 架构设计图

技术图片

(3)SPJ数据库

  • 问题:使用T-SQL语句在SPJ数据库中创建第2章习题10中的4张表:供应商表S、零件表P、工程项目表J和供应情况表SPJ,数据类型和长度自行设计,注意要同时定义主键、外键和其他的数据完整性。

  • sql语句:

CREATE DATABASE SPJ;
USE spj;
-- 零件表P
CREATE TABLE P(
	Pno CHAR(2) NOT NULL PRIMARY KEY,
	Pname VARCHAR(20) NOT NULL,
	Color CHAR(4) NOT NULL,
	Weight TINYINT NOT NULL
); 

 -- 供应商表S
CREATE TABLE S(
	Sno CHAR(2) NOT NULL PRIMARY KEY,
	Sname VARCHAR(20) NOT NULL,
	STATUS TINYINT NOT NULL,
	City VARCHAR(10) NOT NULL
);

-- 工程项目表J
CREATE TABLE J( 
	Jno CHAR(2) NOT NULL PRIMARY KEY,
	Jname VARCHAR(20) NOT NULL,
	City VARCHAR(20) NOT NULL
);

-- 供应情况表SPJ
CREATE TABLE SPJ(
	Sno CHAR(2) NOT NULL,
	Pno CHAR(2) NOT NULL,
	Jno CHAR(2) NOT NULL,
	Qity INT NOT NULL,
    PRIMARY KEY(Sno,Pno,Jno),
	FOREIGN KEY (Sno) REFERENCES S(Sno),
	FOREIGN KEY (Pno) REFERENCES P(Pno),
	FOREIGN KEY (Jno) REFERENCES J(Jno)
);
  • 架构设计图

技术图片

(4)图书借阅管理数据库TSGL

  • 问题: 使用T-SQL语句在图书借阅管理数据库TSGL中建立图书、读者和借阅3个表,其结构为

    图书(书号,书名,类别,出版社,作者,定价,出版时间)

    读者(借书证号,姓名,单位,性别,地址,电话号码)

    借阅(书号,借书证号,借阅日期)

    要求为属性选择合适的数据类型,定义每个主键、外键,是否允许空值等数据完整性约束。

  • sql语句

-- 图书管理数据库
CREATE DATABASE TSGL;
USE TSGL;
-- 图书Book(书号ISBN,书名BookName,类别BookType,出版社PressNo,作者Author,定价Price,出版时间PublishTime)
CREATE TABLE Book(
	ISBN CHAR(20) NOT NULL PRIMARY KEY,
	BookName TEXT,
	BookType TEXT ,
	PressNo VARCHAR(20),
	Author VARCHAR(20),
	Price DOUBLE,
	PublishTime DATETIME
);
-- 读者Reader(借书证号ReaderAccount,姓名ReaderName,单位ReaderCompany,性别Sex,地址Address,电话号码Tel)
CREATE TABLE Reader(
	ReaderAccount CHAR(15) NOT NULL PRIMARY KEY,
	ReaderName VARCHAR(8),
	ReaderCompany VARCHAR(20),
	ReaderSex ENUM (‘男‘, ‘女‘) NOT NULL DEFAULT ‘男‘,
	ReaderAddress TEXT,
	ReaderTel CHAR(12)
);
-- 借阅BorrowR(书号ISBN,借书证号ReaderAccount,借阅日期BorrowTime)
CREATE TABLE Borrow(
	ISBN CHAR(20) NOT NULL,
	ReaderAccount CHAR(15) NOT NULL,
	BorrowTime DATETIME NOT NULL,
    PRIMARY KEY (ISBN,ReaderAccount),
    FOREIGN KEY (ISBN) REFERENCES Book(ISBN),
    FOREIGN KEY (ReaderAccount)REFERENCES Reader(ReaderAccount)
);
  • 架构设计图

技术图片

test

标签:unicode   har   课程   时间   str   实验   查看   数据库   check   

原文地址:https://www.cnblogs.com/CloudGuest/p/14089628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!