diff --git "a/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240 .md" "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240 .md" new file mode 100644 index 0000000000000000000000000000000000000000..939300c89abf5abe2d5f52e74c4c78f2ada14c8b --- /dev/null +++ "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240 .md" @@ -0,0 +1,590 @@ +## 第1题 + +1、创建数据库test01_market + +2、创建表格customers + +| 字段名 | 数据类型 | +| --------- | ----------- | +| c_num | int(11) | +| c_name | varchar(50) | +| c_contact | varchar(50) | +| c_city | varchar(50) | +| c_birth | date | + +**要求3:**将c_contact字段移动到c_birth字段后面 + +**要求4:**将c_name字段数据类型改为 varchar(70) + +**要求5:**将c_contact字段改名为c_phone + +**要求6:**增加c_gender字段到c_name后面,数据类型为char(1) + +**要求7:**将表名改为customers_info + +**要求8:**删除字段c_city + +```mysql +创建数据库test01_market: +mysql> create database test01_market; +查看所有数据库: +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| test01_market | ++--------------------+ +5 rows in set (0.00 sec) +使用test01_market数据库: +mysql> use test01_market +Database changed +创建表格customers: +mysql> create table customers( + -> c_num int(11) , + -> c_name varchar(50) , + -> c_contact varchar(50) , + -> c_city varchar(50) , + -> c_birth date + -> ) comment 'customers' ; +Query OK, 0 rows affected (0.06 sec) +将c_contact字段移动到c_birth字段后面: +mysql> Alter table customers modify c_contact varchar(50) after c_birth ; +Query OK, 0 rows affected (0.10 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(50) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_contact | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +将c_name字段数据类型改为 varchar(70): +mysql> Alter table customers modify c_name varchar(70); +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_contact | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +将c_contact字段改名为c_phone: +mysql> Alter table customers change c_contact c_phone varchar(50); +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++---------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++---------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++---------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +增加c_gender字段到c_name后面,数据类型为char(1): +mysql> Alter table customers add c_gender char(1) after c_name; +Query OK, 0 rows affected (0.09 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_gender | char(1) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++----------+-------------+------+-----+---------+-------+ +6 rows in set (0.00 sec) +将表名改为customers_info: +mysql> Alter table customers rename to customers_info; +Query OK, 0 rows affected (0.03 sec) +查看现有表格: +mysql> show tables; ++-------------------------+ +| Tables_in_test01_market | ++-------------------------+ +| customers_info | ++-------------------------+ +1 row in set (0.00 sec) +删除字段c_city: +mysql> Alter table customers_info drop c_city; +Query OK, 0 rows affected (0.08 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看表结构: +mysql> desc customers_info; ++----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_gender | char(1) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +``` + +## 第2题 + +1、创建数据库test02_library + +2、创建表格books + +| 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | +| ------- | -------- | ------------- | -------- | ---- | +| b_id | 书编号 | int(11) | 否 | 是 | +| b_name | 书名 | varchar(50) | 否 | 否 | +| authors | 作者 | varchar(100) | 否 | 否 | +| price | 价格 | float | 否 | 否 | +| pubdate | 出版日期 | year | 否 | 否 | +| note | 说明 | varchar(100) | 是 | 否 | +| num | 库存 | int(11) | 否 | 否 | + +3、向books表中插入记录 + +1) 指定所有字段名称插入第一条记录 + +2)不指定字段名称插入第二记录 + +3)同时插入多条记录(剩下的所有记录) + +| b_id | b_name | authors | price | pubdate | note | num | +| ---- | ------------- | --------------- | ----- | ------- | -------- | ---- | +| 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +| 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +| 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | + +4、将小说类型(novel)的书的价格都增加5。 + +5、将名称为EmmaT的书的价格改为40。 + +6、删除库存为0的记录 + +```mysql +创建数据库test02_library: +mysql> create database test02_library charset =utf8; +Query OK, 1 row affected (0.01 sec) +创建表格books: +mysql> create table books ( + -> b_id int not null primary key comment '书编号', + -> b_name varchar(50) not null comment '书名', + -> authors varchar(100) not null comment '作者', + -> price float not null comment '价格', + -> pubdate year not null comment '出版日期', + -> note varchar(100) null comment '说明', + -> num int not null comment '库存' + -> ) comment 'books' ; +Query OK, 0 rows affected (0.05 sec) +查看表结构: +mysql> desc books ; ++---------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++---------+--------------+------+-----+---------+-------+ +| b_id | int(11) | NO | PRI | NULL | | +| b_name | varchar(50) | NO | | NULL | | +| authors | varchar(100) | NO | | NULL | | +| price | float | NO | | NULL | | +| pubdate | year(4) | NO | | NULL | | +| note | varchar(100) | YES | | NULL | | +| num | int(11) | NO | | NULL | | ++---------+--------------+------+-----+---------+-------+ +7 rows in set (0.00 sec) +向books表中插入记录: +mysql> INSERT INTO books + -> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("1","Tal of AAA","Dickes","23","1995","novel","11"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("2","EmmaT","Jane lura","35","1993","joke","22"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("3","Story of Jane","Jane Tim","40","2001","novel","0"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("4","Lovey Day","George Byron","20","2005","novel","30"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("5","Old land","Honore Blade","30","2010","law","0"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("6","The Battle","Upton Sara","30","1999","medicine","40"); + Query OK, 0 rows affected (0.01 sec) +mysql> INSERT INTO books +-> (b_id,b_name,authors,price,pubdate,note,num) + -> VALUES + -> ("7","Rose Hood","Richard haggard","28","2008","cartoon","28"); +Query OK, 0 rows affected (0.01 sec) +读取数据表 +mysql> select * from books; ++------+---------------+-----------------+-------+---------+----------+-----+ +| b_id | b_name | authors | price | pubdate | note | num | ++------+---------------+-----------------+-------+---------+----------+-----+ +| 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +| 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | +| 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | +| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | ++------+---------------+-----------------+-------+---------+----------+-----+ +7 rows in set (0.00 sec) +将小说类型(novel)的书的价格都增加5: +mysql> UPDATE books SET price = '28' WHERE b_id = 1; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 +mysql> UPDATE books SET price = '45' WHERE b_id = 3; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 +mysql> UPDATE books SET price = '25' WHERE b_id = 4; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 +查看数据表: +mysql> select * from books; ++------+---------------+-----------------+-------+---------+----------+-----+ +| b_id | b_name | authors | price | pubdate | note | num | ++------+---------------+-----------------+-------+---------+----------+-----+ +| 1 | Tal of AAA | Dickes | 28 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | +| 3 | Story of Jane | Jane Tim | 45 | 2001 | novel | 0 | +| 4 | Lovey Day | George Byron | 25 | 2005 | novel | 30 | +| 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | ++------+---------------+-----------------+-------+---------+----------+-----+ +7 rows in set (0.00 sec) +将名称为EmmaT的书的价格改为40: +mysql> UPDATE books SET price ='40' WHERE b_id =2; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 +查看数据内容: +mysql> select * from books WHERE b_id =2; ++------+--------+-----------+-------+---------+------+-----+ +| b_id | b_name | authors | price | pubdate | note | num | ++------+--------+-----------+-------+---------+------+-----+ +| 2 | EmmaT | Jane lura | 40 | 1993 | joke | 22 | ++------+--------+-----------+-------+---------+------+-----+ +1 row in set (0.00 sec) +删除库存为0的记录: +mysql> DELETE FROM books WHERE num = 0; +Query OK, 2 rows affected (0.01 sec) +查看数据列表: +mysql> select * from books; ++------+------------+-----------------+-------+---------+----------+-----+ +| b_id | b_name | authors | price | pubdate | note | num | ++------+------------+-----------------+-------+---------+----------+-----+ +| 1 | Tal of AAA | Dickes | 28 | 1995 | novel | 11 | +| 2 | EmmaT | Jane lura | 40 | 1993 | joke | 22 | +| 4 | Lovey Day | George Byron | 25 | 2005 | novel | 30 | +| 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | +| 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | ++------+------------+-----------------+-------+---------+----------+-----+ +5 rows in set (0.00 sec) +``` + + + +## 第3题 + +1、创建数据库test03_bookstore + +2、创建book表 + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| sales | int(11) | NO | | NULL | | +| stock | int(11) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+-------------+------------+-------+-------+-------+----------------------------+ +| id | title | author | price | sales | stock | img_path | ++----+-------------+------------+-------+-------+-------+-----------------------------+ +| 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | +| 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | ++----+---------------+------------+-------+-------+-------+----------------------------+ +``` + +3、创建用户表users,并插入数据 + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| username | varchar(100) | NO | UNI | NULL | | +| password | varchar(100) | NO | | NULL | | +| email | varchar(100) | YES | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+----------+----------------------------------+--------------------+ +| id | username | password | email | ++----+----------+----------------------------------+--------------------+ +| 1 | admin | 112233 | admin@mxdx.com | ++----+----------+----------------------------------+--------------------+ +``` + +4、创建订单表orders + +```mysql ++--------------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++--------------+--------------+------+-----+---------+-------+ +| id | varchar(100) | NO | PRI | NULL | | +| order_time | datetime | NO | | NULL | | +| total_count | int(11) | NO | | NULL | | +| total_amount | double(11,2) | NO | | NULL | | +| state | int(11) | NO | | NULL | | +| user_id | int(11) | NO | MUL | NULL | | ++--------------+--------------+------+-----+---------+-------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----------------+---------------------+-------------+--------------+-------+---------+ +| id | order_time | total_count | total_amount | state | user_id | ++----------------+---------------------+-------------+--------------+-------+---------+ +| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | ++----------------+---------------------+-------------+--------------+-------+---------+ +``` + +5、创建订单明细表order_items + +```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| count | int(11) | NO | | NULL | | +| amount | double(11,2) | NO | | NULL | | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | +| order_id | varchar(100) | NO | MUL | NULL | | ++----------+--------------+------+-----+---------+----------------+ +``` + +尝试添加部分模拟数据,参考示例如下: + +```mysql ++----+-------+--------+---------+---------+-------+----------------+----------------+ +| id |count| amount| title | author | price | img_path | order_id | ++----+-------+--------+------------+----------+-------+----------------+----------------+ +| 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | +| 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | ++----+-------+--------+------------+----------+-------+------------+----------------+ +``` + +参考答案: + +```mysql +创建数据库test03_bookstore: +mysql> create database test03_bookstore charset =utf8; +Query OK, 1 row affected (0.01 sec) +创建book表: +mysql> create table book( + -> id int not null primary key auto_increment, + -> title varchar(100) not null, + -> author varchar(100) not null, + -> price double(11,2) not null, + -> sales int not null, + -> stock int not null, + -> img_path varchar(100) not null + -> ) comment 'book' ; +Query OK, 0 rows affected (0.04 sec) +创建用户表users,并插入数据: +mysql> INSERT INTO book + -> (id,title,author,price,sales,stock,img_path) + -> VALUES + -> ("1","解忧杂货店","东野圭吾","27.02","102","98","upload/books/解忧杂货店.jpg"); +Query OK, 1 row affected (0.01 sec) +mysql> INSERT INTO book + -> (id,title,author,price,sales,stock,img_path) + -> VALUES + -> ("2","边城","沈从文","23.00","102","98","upload/books/边城.jpg"); +Query OK, 1 row affected (0.01 sec) +查看表数据: +mysql> select * from book; ++----+------------+----------+-------+-------+-------+-----------------------------+ +| id | title | author | price | sales | stock | img_path | ++----+------------+----------+-------+-------+-------+-----------------------------+ +| 1 | 解忧杂货店 | 东野圭吾 | 27.02 | 102 | 98 | upload/books/解忧杂货店.jpg | +| 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | ++----+------------+----------+-------+-------+-------+-----------------------------+ +2 rows in set (0.00 sec) +创建用户表users,并插入数据: +mysql> create table users( + -> id int not null primary key auto_increment, + -> username varchar(100) not null unique, + -> password varchar(100) not null, + -> email varchar(100) null + -> ) comment 'users' ; +Query OK, 0 rows affected (0.07 sec) +查看表结构: +mysql> desc users; ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| username | varchar(100) | NO | UNI | NULL | | +| password | varchar(100) | NO | | NULL | | +| email | varchar(100) | YES | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +4 rows in set (0.01 sec) +尝试添加部分模拟数据: +mysql> INSERT INTO users + -> (id,username,password,email) + -> VALUES + -> ("1","admin","112233","admin@mxdx.com"); +Query OK, 1 row affected (0.01 sec) +创建订单表orders: +mysql> create table orders( + -> id varchar(100) not null primary key, + -> order_time datetime not null, + -> total_count int not null, + -> total_amount double(11,2) not null, + -> state int not null, + -> user_id int not null, + -> constraint fk_users + -> foreign key(user_id) references users(id) + -> ) comment 'orders' ; +Query OK, 0 rows affected (0.07 sec) +查看表结构: +mysql> desc orders; ++--------------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++--------------+--------------+------+-----+---------+-------+ +| id | varchar(100) | NO | PRI | NULL | | +| order_time | datetime | NO | | NULL | | +| total_count | int(11) | NO | | NULL | | +| total_amount | double(11,2) | NO | | NULL | | +| state | int(11) | NO | | NULL | | +| user_id | int(11) | NO | MUL | NULL | | ++--------------+--------------+------+-----+---------+-------+ +6 rows in set (0.00 sec) +尝试添加部分模拟数据: +mysql> INSERT INTO orders + -> (id,order_time,total_count,total_amount,state,user_id) + -> VALUES + -> ("15294258455691","2018-06-20 00:30:45","2","50.20","0","1"); +Query OK, 1 row affected (0.01 sec) +查看表数据: +mysql> select * from orders; ++----------------+---------------------+-------------+--------------+-------+---------+ +| id | order_time | total_count | total_amount | state | user_id | ++----------------+---------------------+-------------+--------------+-------+---------+ +| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | ++----------------+---------------------+-------------+--------------+-------+---------+ +1 row in set (0.00 sec) +创建订单明细表order_items: +mysql> create table order_items( + -> id int not null primary key auto_increment, + -> count int not null, + -> amount double(11,2) not null, + -> title varchar(100) not null, + -> author varchar(100) not null, + -> price double(11,2) not null, + -> img_path varchar(100) not null, + -> order_id varchar(100) not null, + -> constraint fk_orders + -> foreign key(order_id) references orders(id) + -> ) comment 'order_items' ; +Query OK, 0 rows affected (0.10 sec) +查看表结构: +mysql> desc order_items; ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| count | int(11) | NO | | NULL | | +| amount | double(11,2) | NO | | NULL | | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | +| order_id | varchar(100) | NO | MUL | NULL | | ++----------+--------------+------+-----+---------+----------------+ +8 rows in set (0.01 sec) +mysql> INSERT INTO order_items + -> (id,count,amount,title,author,price,img_path,order_id) + -> VALUES + -> ("1","1","27.20","解忧杂货店","东野圭吾","27.20","static/img/default.jpg","15294258455691"); +Query OK, 1 row affected (0.01 sec) +尝试添加部分模拟数据: +mysql> INSERT INTO order_items + -> (id,count,amount,title,author,price,img_path,order_id) + -> VALUES + -> ("2","1","23.00","边城","沈从文","23.00","static/img/default.jpg","15294258455691"); +Query OK, 1 row affected (0.01 sec) +查看表数据: +mysql> select * from order_items; ++----+-------+--------+------------+----------+-------+------------------------+----------------+ +| id | count | amount | title | author | price | img_path | order_id | ++----+-------+--------+------------+----------+-------+------------------------+----------------+ +| 1 | 1 | 27.20 | 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg | 15294258455691 | +| 2 | 1 | 23.00 | 边城 | 沈从文 | 23.00 | static/img/default.jpg | 15294258455691 | ++----+-------+--------+------------+----------+-------+------------------------+----------------+ +2 rows in set (0.00 sec) +``` + +# 笔记 + +创建数据库的mysql命令语法: + +create database 数据库名称 charset =utf8; + + + + + diff --git "a/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\344\275\234\344\270\2322.md" "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\344\275\234\344\270\2322.md" new file mode 100644 index 0000000000000000000000000000000000000000..e067f7ab3be428c4c9df59dfad7b20cc6968bb7b --- /dev/null +++ "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\344\275\234\344\270\2322.md" @@ -0,0 +1,221 @@ +1、创建数据库test01_company + +2、创建表格offices + +| 字段名 | 数据类型 | +| ---------- | ----------- | +| officeCode | int | +| city | varchar(30) | +| address | varchar(50) | +| country | varchar(50) | +| postalCode | varchar(25) | + +3、创建表格employees + +| 字段名 | 数据类型 | +| --------- | ------------- | +| empNum | int(11) | +| lastName | varchar(50) | +| firstName | varchar(50) | +| mobile | varchar(25) | +| code | int | +| jobTitle | varchar(50) | +| birth | date | +| Note | varchar(255) | +| Sex | varchar(5) | + +**要求4:**将表employees的mobile字段修改到code字段后面。 + +**要求5:**将表employees的birth字段改名为birthday; + +**要求6:**修改sex字段,数据类型为char(1)。 + +**要求7:**删除字段note; + +**要求8:**增加字段名favoriate_activity,数据类型为varchar(100); + +**要求9:**将表employees的名称修改为 employees_info + +```mysql +创建数据库test01_company: +mysql> create database test01_company charset=utf8; +Query OK, 1 row affected (0.00 sec) +进入指定数据库: +mysql> use test01_company; +Database changed +创建表格offices: +mysql> create table offices( + -> officeCode int , + -> city varchar(30) , + -> address varchar(50) , + -> country varchar(50) , + -> postalCode varchar(25) + -> ) comment 'offices' ; +Query OK, 0 rows affected (0.27 sec) +查询表结构: +mysql> desc offices; ++------------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++------------+-------------+------+-----+---------+-------+ +| officeCode | int(11) | YES | | NULL | | +| city | varchar(30) | YES | | NULL | | +| address | varchar(50) | YES | | NULL | | +| country | varchar(50) | YES | | NULL | | +| postalCode | varchar(25) | YES | | NULL | | ++------------+-------------+------+-----+---------+-------+ +5 rows in set (0.08 sec) +创建表格employees: +mysql> create table employees( + -> empNum int , + -> lastName varchar(50) , + -> firstName varchar(50) , + -> mobile varchar(25) , + -> code int , + -> jobTitle varchar(50) , + -> birth date , + -> Note varchar(255) , + -> Sex varchar(5) + -> ) comment 'employees' ; +Query OK, 0 rows affected (0.17 sec) +查询表结构: +mysql> desc employees; ++-----------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+--------------+------+-----+---------+-------+ +| empNum | int(11) | YES | | NULL | | +| lastName | varchar(50) | YES | | NULL | | +| firstName | varchar(50) | YES | | NULL | | +| mobile | varchar(25) | YES | | NULL | | +| code | int(11) | YES | | NULL | | +| jobTitle | varchar(50) | YES | | NULL | | +| birth | date | YES | | NULL | | +| Note | varchar(255) | YES | | NULL | | +| Sex | varchar(5) | YES | | NULL | | ++-----------+--------------+------+-----+---------+-------+ +9 rows in set (0.00 sec) +将表employees的mobile字段修改到code字段后面: +mysql> Alter table employees modify mobile varchar(25) after code ; +Query OK, 0 rows affected (0.48 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看表结构: +mysql> desc employees; ++-----------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+--------------+------+-----+---------+-------+ +| empNum | int(11) | YES | | NULL | | +| lastName | varchar(50) | YES | | NULL | | +| firstName | varchar(50) | YES | | NULL | | +| code | int(11) | YES | | NULL | | +| mobile | varchar(25) | YES | | NULL | | +| jobTitle | varchar(50) | YES | | NULL | | +| birth | date | YES | | NULL | | +| Note | varchar(255) | YES | | NULL | | +| Sex | varchar(5) | YES | | NULL | | ++-----------+--------------+------+-----+---------+-------+ +9 rows in set (0.00 sec) +将表employees的birth字段改名为birthday: +mysql> Alter table employees change birth birthday date ; +Query OK, 0 rows affected (0.06 sec) +Records: 0 Duplicates: 0 Warnings: 0 +修改sex字段,数据类型为char(1): +mysql> Alter table employees modify sex char(1) ; +Query OK, 0 rows affected (0.50 sec) +Records: 0 Duplicates: 0 Warnings: 0 +删除字段note; +mysql> Alter table employees + -> drop note ; +Query OK, 0 rows affected (0.34 sec) +Records: 0 Duplicates: 0 Warnings: 0 +增加字段名favoriate_activity,数据类型为varchar(100); +mysql> Alter table employees add favorite_activity varchar(100) ; +Query OK, 0 rows affected (0.34 sec) +Records: 0 Duplicates: 0 Warnings: 0 +将表employees的名称修改为 employees_info: +mysql> Alter table employees rename to employees_info; +Query OK, 0 rows affected (0.11 sec) +``` + + + +## 第2题 + +1、创建数据库test02db + +2、创建表格pet + +| 字段名 | 字段说明 | 数据类型 | +| ------- | -------- | ----------- | +| name | 宠物名称 | varchar(20) | +| owner | 宠物主人 | varchar(20) | +| species | 种类 | varchar(20) | +| sex | 性别 | char(1) | +| birth | 出生日期 | year | +| death | 死亡日期 | year | + +3、添加记录 + +| name | owner | species | sex | birth | death | +| ------ | ------ | ------- | ---- | ----- | ----- | +| Fluffy | harold | Cat | f | 2003 | 2010 | +| Claws | gwen | Cat | m | 2004 | | +| Buffy | | Dog | f | 2009 | | +| Fang | benny | Dog | m | 2000 | | +| bowser | diane | Dog | m | 2003 | 2009 | +| Chirpy | | Bird | f | 2008 | | + +4、 添加字段主人的生日owner_birth。 + +5、 将名称为Claws的猫的主人改为kevin + +6、 将没有死的狗的主人改为duck + +7、 查询没有主人的宠物的名字; + +8、 查询已经死了的cat的姓名,主人,以及去世时间; + +9、 删除已经死亡的狗 + +10、查询所有宠物信息 + +```sql +创建数据库test02db: +mysql> create database test02db charset=utf8; +Query OK, 1 row affected (0.00 sec) +创建表格pet: +mysql> create table pet( + -> name varchar(20) comment '宠物名称', + -> owner varchar(20) comment '宠物主人', + -> species varchar(20) comment '种类', + -> sex char(1) comment '性别', + -> birth year comment '出生日期', + -> death year comment '死亡日期' + -> ) comment 'pet' ; +Query OK, 0 rows affected (0.28 sec) +添加记录: +mysql> INSERT INTO pet + -> (name,owner,species,sex,birth,death) + -> VALUES + -> ("Claws","gwen","Cat","m","2004",null), + -> ("Buffy"," ","Dog","f","2009",null), + -> ("Fang","benny","Dog","m","2000",null), + -> ("bowser","diane","Dog","m","2003","2009"), + -> ("Chirpy",null,"Bird","f","2008",null); +Query OK, 5 rows affected (0.03 sec) +Records: 5 Duplicates: 0 Warnings: 0 + 添加字段主人的生日owner_birth: + mysql> Alter table pet add owner_birth int comment '主人的生日'; +Query OK, 0 rows affected (0.41 sec) +Records: 0 Duplicates: 0 Warnings: 0 +将名称为Claws的猫的主人改为kevin: + mysql> UPDATE pet set owner = "kevin" WHERE name ="Claws"; + 将没有死的狗的主人改为duck + mysql> UPDATE pet set owner ="duck" WHERE death is null and species = "Dog"; + 查询没有主人的宠物的名字; + mysql> SELECT name from pet WHERE owner is null ; + 查询已经死了的cat的姓名,主人,以及去世时间; + mysql> SELECT name from pet WHERE owner is null ; + 删除已经死亡的狗: + mysql> DELETE FROM pet WHERE death is not null and species="Dog"; + 查询所有宠物信息: + mysql> SELECT * from pet ; +``` diff --git "a/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\347\254\254\344\270\200\351\242\230.md" "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\347\254\254\344\270\200\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..ad5987114bdffa93b20c30b4adbe9bb932d95e5e --- /dev/null +++ "b/\350\275\257\344\273\266\346\212\200\346\234\2574\347\217\25512\350\224\241\344\271\224\346\243\256/\347\254\254\344\270\200\351\242\230.md" @@ -0,0 +1,118 @@ +创建数据库test01_market: +mysql> create database test01_market; +查看所有数据库: +mysql> show databases; ++--------------------+ +| Database | ++--------------------+ +| information_schema | +| mysql | +| performance_schema | +| sys | +| test01_market | ++--------------------+ +5 rows in set (0.00 sec) +使用test01_market数据库: +mysql> use test01_market +Database changed +创建表格customers: +mysql> create table customers( + -> c_num int(11) , + -> c_name varchar(50) , + -> c_contact varchar(50) , + -> c_city varchar(50) , + -> c_birth date + -> ) comment 'customers' ; +Query OK, 0 rows affected (0.06 sec) +将c_contact字段移动到c_birth字段后面: +mysql> Alter table customers modify c_contact varchar(50) after c_birth ; +Query OK, 0 rows affected (0.10 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(50) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_contact | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +将c_name字段数据类型改为 varchar(70): +mysql> Alter table customers modify c_name varchar(70); +Query OK, 0 rows affected (0.02 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++-----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++-----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_contact | varchar(50) | YES | | NULL | | ++-----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +将c_contact字段改名为c_phone: +mysql> Alter table customers change c_contact c_phone varchar(50); +Query OK, 0 rows affected (0.03 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++---------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++---------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++---------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) +增加c_gender字段到c_name后面,数据类型为char(1): +mysql> Alter table customers add c_gender char(1) after c_name; +Query OK, 0 rows affected (0.09 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看调整后的结构: +mysql> desc customers; ++----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_gender | char(1) | YES | | NULL | | +| c_city | varchar(50) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++----------+-------------+------+-----+---------+-------+ +6 rows in set (0.00 sec) +将表名改为customers_info: +mysql> Alter table customers rename to customers_info; +Query OK, 0 rows affected (0.03 sec) +查看现有表格: +mysql> show tables; ++-------------------------+ +| Tables_in_test01_market | ++-------------------------+ +| customers_info | ++-------------------------+ +1 row in set (0.00 sec) +删除字段c_city: +mysql> Alter table customers_info drop c_city; +Query OK, 0 rows affected (0.08 sec) +Records: 0 Duplicates: 0 Warnings: 0 +查看表结构: +mysql> desc customers_info; ++----------+-------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+-------------+------+-----+---------+-------+ +| c_num | int(11) | YES | | NULL | | +| c_name | varchar(70) | YES | | NULL | | +| c_gender | char(1) | YES | | NULL | | +| c_birth | date | YES | | NULL | | +| c_phone | varchar(50) | YES | | NULL | | ++----------+-------------+------+-----+---------+-------+ +5 rows in set (0.00 sec) \ No newline at end of file