一、课程名称: 数据库原理 |
二、实验名称:实验1 数据库基本表定义及修改 |
三、实验地点与实验环境: 实验环境: 装有MySQL的PC机。 |
四、实验内容 1、创建数据库 创建名为“books_db”的数据库。 2、建立基本表 在MySQL命令行客户端中,使用SQL语句在“books_db”数据库中建立以下四个表。表结构如下:(注意:在创建表之前,需使用use命令打开“books_db”数据库) 3、修改基本表(使用SQL语句) (1)给读者表增加一个“备注”字段,类型为varchar(150); (2)将“备注”字段的类型修改为varchar(200); (3)删除“备注”字段; (4)将图书表中的“库存”字段类型改为smallint。 SQL语句:
4.CREATE TABLE book ( book_number char(10) PRIMARY KEY, category varchar(12), book_name varchar(30) NOT NULL, publisher char(30) NOT NULL, author char(10), price numeric(5,2), book_total int NOT NULL, inventory int NOT NULL ); 5. CREATE TABLE reader( reader_number CHAR(10) PRIMARY KEY, name varchar(8) not null, department char(20) , gender char(2) not null, telephone char(15) ); 6. CREATE TABLE borrow_book( reader_number CHAR(10) not null, book_number char(10) not null, borrow_time datetime not null, PRIMARY KEY(reader_number,book_number), foreign KEY(book_number) references book (book_number), foreign KEY(reader_number) references reader(reader_number) ); 7. CREATE TABLE return_book( reader_number CHAR(10) not null, book_number char(10) not null, retrun_time datetime not null, PRIMARY KEY(reader_number,book_number), foreign KEY(book_number) references book (book_number), foreign KEY(reader_number) references reader(reader_number) ); 8. ALTER TABLE reader ADD (note varchar(150)); 9.ALTER TABLE reader MODIFY note varchar(200); 10. ALTER TABLE reader DROP COLUMN note; 11. ALTER TABLE book MODIFY inventory smallint; |
五、实验步骤、结果或结论: 1. create database books_db DEFAULT CHARACTER SET utf8mb4; 2.建立基本表 (1)图书表,表名:book CREATE TABLE book ( book_number char(10) PRIMARY KEY, category varchar(12), book_name varchar(30) NOT NULL, publisher char(30) NOT NULL, author char(10), price numeric(5,2), book_total int NOT NULL, inventory int NOT NULL ); (2)读者表, 表名:reader CREATE TABLE reader( reader_number CHAR(10) PRIMARY KEY, name varchar(8) not null, department char(20) , gender char(2) not null, telephone char(15) ); (3)借书表, 表名:borrow_book CREATE TABLE borrow_book( reader_number CHAR(10) not null, book_number char(10) not null, borrow_time datetime not null, PRIMARY KEY(reader_number,book_number), foreign KEY(book_number) references book (book_number), foreign KEY(reader_number) references reader(reader_number) ); 4. CREATE TABLE return_book( reader_number CHAR(10) not null, book_number char(10) not null, retrun_time datetime not null, PRIMARY KEY(reader_number,book_number), foreign KEY(book_number) references book (book_number), foreign KEY(reader_number) references reader(reader_number) ); 3、修改基本表(使用SQL语句) (1)给读者表增加一个“备注”字段,类型为varchar(150); ALTER TABLE reader ADD (note varchar(150)); (2)将“备注”字段的类型修改为varchar(200); ALTER TABLE reader MODIFY note varchar(200); (3)删除“备注”字段; ALTER TABLE reader DROP COLUMN note; (4)将图书表中的“库存”字段类型改为smallint。 ALTER TABLE book MODIFY inventory smallint; |
【实验1 数据库基本表定义及修改】
最新推荐文章于 2024-06-10 12:33:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)