美文网首页
小练习(一)

小练习(一)

作者: knot98 | 来源:发表于2018-09-13 21:05 被阅读0次

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| he_guiqing         |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> create database day0913_homework_01;
Query OK, 1 row affected (0.00 sec)

mysql> use day0913_homework_01
Database changed
mysql> create table student(
    -> id int unsigned primary key auto_increment,
    -> `stu name` char(10) not null,
    -> password int not null,
    -> age int not null
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> create table school(
    -> id int primary key auto_increment,
    -> `school name` char(20) not null,
    -> `school address` char(20) not null
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table course(
    -> id int unsigned primary key auto_increment,
    -> `course name` char(20) not null,
    -> `course price` char(10) not null,
    -> `course period`int not null,
    -> `school id` int not null,
    -> foreign key(`school id`) references school(id)
    -> on update cascade
    -> on delete cascade
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table s_c(
    -> id int auto_increment,
    -> `stu id` int unsigned,
    -> `course id` int unsigned,
    -> primary key(id,`stu id`,`course id`),
    -> foreign key(`stu id`) references student(id)
    -> on update cascade
    -> on delete cascade,
    -> foreign key(`course id`) references course(id)
    -> on update cascade
    -> on delete cascade
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc student;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| stu name | char(10)         | NO   |     | NULL    |                |
| password | int(11)          | NO   |     | NULL    |                |
| age      | int(11)          | NO   |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)

mysql> desc school;
+----------------+----------+------+-----+---------+----------------+
| Field          | Type     | Null | Key | Default | Extra          |
+----------------+----------+------+-----+---------+----------------+
| id             | int(11)  | NO   | PRI | NULL    | auto_increment |
| school name    | char(20) | NO   |     | NULL    |                |
| school address | char(20) | NO   |     | NULL    |                |
+----------------+----------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> desc course;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| course name   | char(20)         | NO   |     | NULL    |                |
| course price  | char(10)         | NO   |     | NULL    |                |
| course period | int(11)          | NO   |     | NULL    |                |
| school id     | int(11)          | NO   | MUL | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> desc s_c;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(11)          | NO   | PRI | NULL    | auto_increment |
| stu id    | int(10) unsigned | NO   | PRI | 0       |                |
| course id | int(10) unsigned | NO   | PRI | 0       |                |
+-----------+------------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)

mysql> insert into student values(null,'张三',123,20),
    -> (null,'李四',111,18);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select *from student;
+----+----------+----------+-----+
| id | stu name | password | age |
+----+----------+----------+-----+
|  1 | 张三     |      123 |  20 |
|  2 | 李四     |      111 |  18 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> insert into school values(null,'oldboyBeijing','北京昌平'),
    -> (null,'oldboyShanghai','上海浦东');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select *from school;
+----+----------------+----------------+
| id | school name    | school address |
+----+----------------+----------------+
|  1 | oldboyBeijing  | 北京昌平       |
|  2 | oldboyShanghai | 上海浦东       |
+----+----------------+----------------+
2 rows in set (0.00 sec)

mysql> insert into course values(null,'Python全栈开发一期','2w',5,2),
    -> (null,'Linux运维一期','200',2,2),
    -> (null,'Python全栈开发20期','2w',5,1);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from course;
+----+--------------------------+--------------+---------------+-----------+
| id | course name              | course price | course period | school id |
+----+--------------------------+--------------+---------------+-----------+
|  1 | Python全栈开发一期       | 2w           |             5 |         2 |
|  2 | Linux运维一期            | 200          |             2 |         2 |
|  3 | Python全栈开发20期       | 2w           |             5 |         1 |
+----+--------------------------+--------------+---------------+-----------+
3 rows in set (0.00 sec)

mysql> insert into s_c values(null,1,1),(null,2,2);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select *from s_c;
+----+--------+-----------+
| id | stu id | course id |
+----+--------+-----------+
|  1 |      1 |         1 |
|  2 |      2 |         2 |
+----+--------+-----------+
2 rows in set (0.00 sec)

mysql> select *from course where `school id`=1;
+----+-------------------------+--------------+---------------+-----------+
| id | course name             | course price | course period | school id |
+----+-------------------------+--------------+---------------+-----------+
|  3 | Python全栈开发20期      | 2w           |             5 |         1 |
+----+-------------------------+--------------+---------------+-----------+
1 row in set (0.00 sec)

mysql> select *from course where `school id`=2;
+----+--------------------------+--------------+---------------+-----------+
| id | course name              | course price | course period | school id |
+----+--------------------------+--------------+---------------+-----------+
|  1 | Python全栈开发一期       | 2w           |             5 |         2 |
|  2 | Linux运维一期            | 200          |             2 |         2 |
+----+--------------------------+--------------+---------------+-----------+
2 rows in set (0.00 sec)

mysql> select *from student where age >= 19;
+----+----------+----------+-----+
| id | stu name | password | age |
+----+----------+----------+-----+
|  1 | 张三     |      123 |  20 |
+----+----------+----------+-----+
1 row in set (0.00 sec)

mysql> select *from course where `course period` >= 4;
+----+--------------------------+--------------+---------------+-----------+
| id | course name              | course price | course period | school id |
+----+--------------------------+--------------+---------------+-----------+
|  1 | Python全栈开发一期       | 2w           |             5 |         2 |
|  3 | Python全栈开发20期       | 2w           |             5 |         1 |
+----+--------------------------+--------------+---------------+-----------+
2 rows in set (0.00 sec)

相关文章

网友评论

      本文标题:小练习(一)

      本文链接:https://www.haomeiwen.com/subject/nbvggftx.html