(简答题)
1、按照结构表提示在数据库中创建数据表,要求使用SQL 语句实现。
2、按照下列要求完成操作,要求使用SQL 语句实现。
(1) 为读者表 reader 指定检査约束,即指定性别 sex 字段的值只能是“男”或“女”。
(2) 修改读者表reader 的默认值约束,使读者类别 typeno 字段的默认值为“1”。
(3) 为图书表 book 指定检査约束,即指定定价 price 字段的值必须大于 0。
<1>创建数据库,进入数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| t2 |
+--------------------+
5 rows in set (0.00 sec)mysql> create database test;
Query OK, 1 row affected (0.00 sec)mysql> use test;
Database changed
mysql>
<2>创建数据库 reader 表
mysql> create table reader(rid char(12) NOT NULL PRIMARY KEY comment '读者编号',-> mame varchar(50) comment '读者姓名',-> sex char(1) comment '性别',-> typeno char(1) DEFAULT '2' comment '读者类别',-> dept varchar(50) comment '所属院系',-> tel varchar(50) comment '联系电话');
Query OK, 0 rows affected (0.01 sec)mysql> desc reader;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| rid | char(12) | NO | PRI | NULL | |
| mame | varchar(50) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| typeno | char(1) | YES | | 2 | |
| dept | varchar(50) | YES | | NULL | |
| tel | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
插入几组数据看看
mysql> insert into reader(rid,mame,sex,dept,tel) values ('1',"东方暮雪",'男',"计算机","13766269172");
Query OK, 1 row affected (0.00 sec)mysql> insert into reader(rid,mame,sex,dept,tel) values ('2',"东方初晴",'女',"计算机","12345678901");
Query OK, 1 row affected (0.00 sec)mysql> select * from reader;
+-----+--------------+------+--------+-----------+-------------+
| rid | mame | sex | typeno | dept | tel |
+-----+--------------+------+--------+-----------+-------------+
| 1 | 东方暮雪 | 男 | 2 | 计算机 | 13766269172 |
| 2 | 东方初晴 | 女 | 2 | 计算机 | 12345678901 |
+-----+--------------+------+--------+-----------+-------------+
2 rows in set (0.00 sec)
完美 ~
<3>现在创建另一张表 book
mysql> create table book(bid char(8) NOT NULL PRIMARY KEY comment '图书编号',-> bname varchar(50) comment '书名',-> author varchar(50) comment '作者',-> publisher varchar(50) comment '出版社',-> publishdate date comment '出版日期',-> price float comment '定价',-> total int comment '库存数量',-> position varchar(50) comment '存放位置');
Query OK, 0 rows affected (0.01 sec)mysql> desc book;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| bid | char(8) | NO | PRI | NULL | |
| bname | varchar(50) | YES | | NULL | |
| author | varchar(50) | YES | | NULL | |
| publisher | varchar(50) | YES | | NULL | |
| publishdate | date | YES | | NULL | |
| price | float | YES | | NULL | |
| total | int(11) | YES | | NULL | |
| position | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
插入几组数据看看
mysql> insert into book values ('1',"诗经","孔子","东方出版社",'2024-9-10',9.9,35,"鲁国");
Query OK, 1 row affected (0.00 sec)mysql> insert into book values ('2',"楚辞","屈原","东方出版社",'2024-9-11',9.9,35,"楚国");
Query OK, 1 row affected (0.00 sec)mysql> select * from book;
+-----+--------+--------+-----------------+-------------+-------+-------+----------+
| bid | bname | author | publisher | publishdate | price | total | position |
+-----+--------+--------+-----------------+-------------+-------+-------+----------+
| 1 | 诗经 | 孔子 | 东方出版社 | 2024-09-10 | 9.9 | 35 | 鲁国 |
| 2 | 楚辞 | 屈原 | 东方出版社 | 2024-09-11 | 9.9 | 35 | 楚国 |
+-----+--------+--------+-----------------+-------------+-------+-------+----------+
2 rows in set (0.00 sec)
现在我们第一题已经完成了,现在就是按照题目要求去添加约束
<4>
(1)为读者表 reader 指定检査约束,即指定性别 sex 字段的值只能是“男”或“女”。
mysql> alter table reader ADD CONSTRAINT check (sex ='男' or sex='女');
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
(2)修改读者表reader 的默认值约束,使读者类别 typeno 字段的默认值为“1”。
mysql> alter table reader MODIFY COLUMN typeno char(1) DEFAULT '1';
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc reader;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| rid | char(12) | NO | PRI | NULL | |
| mame | varchar(50) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| typeno | char(1) | YES | | 1 | |
| dept | varchar(50) | YES | | NULL | |
| tel | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
(3)为图书表 book 指定检査约束,即指定定价 price 字段的值必须大于 0。
mysql> alter table book ADD CONSTRAINT check (price>0);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0