MySQL第六次作业

作业3:视图


mysql> create database vw;
mysql> use vw;
mysql> create table Student(
    -> Sno int primary key,
    -> Sname varchar(20),
    -> Ssex varchar(20),
    -> Sage int,
    -> Sdept varchar(20));

mysql> create table Course(
    -> Cno int primary key,
    -> Cname varchar(20));

mysql> create table SC( Sno int, Cno int , Score int,primary key(Sno,Cno));
Query OK, 0 rows affected (0.00 sec)

mysql> desc SC;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| Sno   | int(11) | NO   | PRI | NULL    |       |
| Cno   | int(11) | NO   | PRI | NULL    |       |
| Score | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from Student;
+-----+-------+------+------+---------+
| Sno | Sname | Ssex | Sage | Sdept   |
+-----+-------+------+------+---------+
|   1 | xixi  | M    |   12 | chinese |
|   2 | mama  | F    |   23 | english |
|   3 | lala  | M    |   34 | chinese |
+-----+-------+------+------+---------+
3 rows in set (0.00 sec)

mysql> select * from Course;
+-----+-------+
| Cno | Cname |
+-----+-------+
|   1 | mysql |
|   2 | java  |
|   3 | k8s   |
+-----+-------+
3 rows in set (0.00 sec)

mysql> select * from SC;
+-----+-----+-------+
| Sno | Cno | Score |
+-----+-----+-------+
|   1 |   1 |    66 |
|   1 |   3 |    89 |
|   2 |   1 |    78 |
|   2 |   2 |    90 |
|   2 |   3 |    88 |
|   3 |   1 |    67 |
|   3 |   2 |    89 |
+-----+-----+-------+
7 rows in set (0.00 sec)

mysql> create view stu_info (name,sex,cname,score) AS select  Sname,Ssex,Cname,Scoore from Student s,Course c,SC a where s.Sno=a.Sno and c.Cno=a.Cno;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from stu_info;
+------+------+-------+-------+
| name | sex  | cname | score |
+------+------+-------+-------+
| xixi | M    | mysql |    66 |
| mama | F    | mysql |    78 |
| lala | M    | mysql |    67 |
| mama | F    | java  |    90 |
| lala | M    | java  |    89 |
| xixi | M    | k8s   |    89 |
| mama | F    | k8s   |    88 |
+------+------+-------+-------+
7 rows in set (0.00 sec)

mysql> drop view stu_info;
Query OK, 0 rows affected (0.00 sec)

作业2索引


create table goods( goods_id int(11) primary key auto_increment, goods__name varchar(20) not null, cat_id int(11) not null default 0, brand_id int(11) not null default 0, goods_sn char(12) not null, shop_price float(6,2) not null default 0.00, goods_desc text default null)engine=myisam charset=utf8mb4  collate=utf8mb4_general_ci;

二、索引


    1、建立一个utf8编码的数据库test1
    2、建立商品表goods和栏目表category
    按如下表结构创建表:存储引擎engine myisam 字符集charset utf8

        mysql> desc goods;
        +------------+-------------+------+-----+---------+----------------+
        | Field      | Type        | Null | Key | Default | Extra          |
        +------------+-------------+------+-----+---------+----------------+
        | goods_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
        | goods_name | varchar(20) | NO   |     |         |                |
        | cat_id     | int(11)     | NO   |     | 0       |                |
        | brand_id   | int(11)     | NO   |     | 0       |                |
        | goods_sn   | char(12)    | NO   |     |         |                |
        | shop_price | float(6,2)  | NO   |     | 0.00    |                |
        | goods_desc | text        | YES  |     | NULL    |                |
        +------------+-------------+------+-----+---------+----------------+
        7 rows in set (0.00 sec)

        
        mysql> desc category;
        +-----------+-------------+------+-----+---------+----------------+
        | Field     | Type        | Null | Key | Default | Extra          |
        +-----------+-------------+------+-----+---------+----------------+
        | cat_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
        | cate_name | varchar(20) | NO   |     |         |                |
        | parent_id | int(11)     | NO   |     | 0       |                |
        +-----------+-------------+------+-----+---------+----------------+
        3 rows in set (0.00 sec)

3、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段 
mysql> alter table goods drop goods_desc;
mysql> alter table goods drop brand_id ;
mysql> alter table goods add click_count int(11);
mysql> desc goods;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| goods_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| goods__name | varchar(20) | NO   |     | NULL    |                |
| cat_id      | int(11)     | NO   |     | 0       |                |
| goods_sn    | char(12)    | NO   |     | NULL    |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
| click_count | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

    4、在 goods_name 列上加唯一性索引(用alter table方式) 
    5、在 shop_price 列上加普通索引(用create index方式)
mysql> create index index2 on goods(shop_price);
mysql> alter table goods add unique index index1 (goods__name);
mysql> mysql> show index from goods;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| goods |          0 | PRIMARY  |            1 | goods_id    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          0 | index1   |            1 | goods__name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          1 | index2   |            1 | shop_price  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |


6、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
mysql> alter table goods add index index3 (click_count);

mysql> drop index index3 on goods;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from goods;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| goods |          0 | PRIMARY  |            1 | goods_id    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          0 | index1   |            1 | goods__name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          1 | index2   |            1 | shop_price  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

mysql> alter table goods add index index4 (click_count);

mysql> show index from goods;
| goods |          0 | PRIMARY  |            1 | goods_id    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          0 | index1   |            1 | goods__name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          1 | index2   |            1 | shop_price  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| goods |          1 | index4   |            1 | click_count | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |


mysql> alter table goods drop index index4;

mysql> show index from goods;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| goods |          0 | PRIMARY  |            1 | goods_id    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          0 | index1   |            1 | goods__name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| goods |          1 | index2   |            1 | shop_price  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
 

你可能感兴趣的:(mysql,adb,数据库)