From 819660c5bf62fedd5680e64069e037830e0d3ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Mon, 20 Feb 2023 23:06:25 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" "b/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" new file mode 100644 index 0000000..e69de29 -- Gitee From b95612570fe369ebc65185439c392d531703a0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 21:33:13 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 "17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" "b/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" new file mode 100644 index 0000000..a72df23 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" @@ -0,0 +1,3 @@ +``` + +``` -- Gitee From 338e4b986b88fcc9c1fe46d27fec6f66e316d0a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 21:44:40 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\271\240\345\222\214\344\275\234\344\270\232.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" "b/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" index e69de29..8381af8 100644 --- "a/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" +++ "b/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" @@ -0,0 +1,13 @@ +```mysql +cr ta 表格名( + 字段 类型 primary key + 或者 primary key(字段1,字段2) + 或primary key(字段) +); +#建表后增加主键: +alter table 表名 add primary key(字段 (可以多个)) +#删除: +alter table 表名 drop primary key; +``` + + -- Gitee From db8da9d7041866d46a953ad4f085fc6ca9b50b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 23:23:22 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\346\215\256\345\272\223test01_market.md" | 31 ++ ...6\215\256\345\272\223test02_library(1).md" | 49 ++++ ...46\215\256\345\272\223test03_bookstore.md" | 123 ++++++++ ...72\345\272\223\345\273\272\350\241\250.md" | 35 +++ .../20230221 \347\272\246\346\235\237.md" | 269 ++++++++++++++++++ .../20230221DDL\345\222\214DML.md" | 226 +++++++++++++++ 6 files changed, 733 insertions(+) create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" new file mode 100644 index 0000000..60bbbc1 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" @@ -0,0 +1,31 @@ +#1、创建数据库test01_market +CREATE database test01_market; +use test01_market; +#2、创建表格customers +#| 字段名 | 数据类型 | +#| --------- | ----------- | +#| c_num | int(11) | +#| c_name | varchar(50) | +#| c_contact | varchar(50) | +#| c_city | varchar(50) | +#| c_birth | date | +CREATE TABLE 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字段后面 +ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; +#要求4:将c_name字段数据类型改为 varchar(70) +ALTER TABLE customers MODIFY c_name VARCHAR(70); +#要求5:将c_contact字段改名为c_phone +ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); +#要求6:增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers ADD COLUMN c_gender char(1); +ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; +#要求7:将表名改为customers_info +ALTER TABLE customers RENAME TO customers_info; +#要求8:删除字段c_city +ALTER TABLE customers_info DROP COLUMN c_city; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" new file mode 100644 index 0000000..fdf1167 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" @@ -0,0 +1,49 @@ +#1、创建数据库test02_library +CREATE DATABASE test02_library; +USE test02_library; +#2、创建表格books +CREATE TABLE books( +b_id int(11) not null , +b_name VARCHAR(50) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price FLOAT NOT null , +pubdate year NOT NULL, +note varchar(100) , +num int(11) not NULL +); +DESC 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) 指定所有字段名称插入第一条记录 +INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); +SELECT*FROM books; +#2)不指定字段名称插入第二记录 +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); +#3)同时插入多条记录(剩下的所有记录) +INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +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 | + +#4、将小说类型(novel)的书的价格都增加5。 +UPDATE books set price =price+5 WHERE note='novel'; +#5、将名称为EmmaT的书的价格改为40。 +UPDATE books set price=40 WHERE b_name='EmmaT'; +#6、删除库存为0的记录 +DELETE from books where num=0; +SELECT*FROM books; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" new file mode 100644 index 0000000..d957f1a --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" @@ -0,0 +1,123 @@ +#1、创建数据库test03_bookstore +CREATE DATABASE test03_booksore; +USE test03_booksore; +#2、创建book表 +#+----------+--------------+------+-----+---------+----------------+ +#| 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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE book( +id int(11) UNSIGNED auto_increment PRIMARY KEY not null, +title VARCHAR(100) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price double(11,2) NOT NULL, +sales INT(11) NOT NULL, +stock INT(11) NOT NULL, +img_path VARCHAR(100) NOT null +); +DESC book; +#尝试添加部分模拟数据,参考示例如下: +#```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 | +#+----+---------------+------------+-------+-------+-------+----------------------------+ +INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +SELECT*FROM book; + +#3、创建用户表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE users( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +usersname VARCHAR(100) UNIQUE KEY NOT NULL, +`password` VARCHAR(100) NOT null, +email VARCHAR(100) NULL +); +DESC users; +#尝试添加部分模拟数据,参考示例如下: +#+----+----------+----------------------------------+--------------------+ +#| id | username | password | email | +#+----+----------+----------------------------------+--------------------+ +#| 1 | admin | 112233 | admin@mxdx.com | +#+----+----------+----------------------------------+--------------------+ +INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); +SELECT*FROM users; + +#4、创建订单表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 | | +#+--------------+--------------+------+-----+---------+-------+ +CREATE TABLE orders( +id VARCHAR(100) PRIMARY KEY NOT NULL, +order_time DATETIME NOT NULL, +total_count INT(11) NOT NULL, +total_amount DOUBLE(11,2) NOT NULL, +state INT(11) NOT NULL, +user_id INT(11) not null +); +DESC orders; +#尝试添加部分模拟数据,参考示例如下: +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| id | order_time | total_count | total_amount | state | user_id | +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +#+----------------+---------------------+-------------+--------------+-------+----- +INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); +SELECT*FROM orders; + +#5、创建订单明细表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE order_items( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +count INT(11) 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 +); +DESC 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 | +#+----+-------+--------+------------+----------+-------+------------+----------------+ +INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); +SELECT*FROM order_items; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" new file mode 100644 index 0000000..cfdd8c8 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" @@ -0,0 +1,35 @@ +```mysql +笔记 + +DATABASE 数据库 +#创建数据库 +show database 数据库名字 +使用数据库 +use 数据库 +插入数据到表中 +insert into 表名字 values() +``` + +```mysql +作业 + +SHOW DATABASES; +CREATE DATABASE class;#创建数据库 +use class;#使用数据库 +#创建表单 +CREATE TABLE student( +student_学号 VARCHAR(12), +student_姓名 VARCHAR(4), +student_性别 VARCHAR(2), +student_爱好 VARCHAR(10), +student_住址 VARCHAR(15), +student_电话 VARCHAR(15), +student_邮箱 VARCHAR(20), +student_QQ VARCHAR(15) +); +DESC student;#查询表单结构 +#插入数据 +INSERT into student VALUES('2244310155','杨旭东','男','打游戏,羽毛球','江贵州省贵阳市白云区','18275209683','1875779940@qq.com','1875779940') + +``` + diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" new file mode 100644 index 0000000..8de5b0b --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" @@ -0,0 +1,269 @@ +笔记 + +```mysql + + +添加外键 +表内 +create table 表名(。。。。。 {constraint}{外键名称}foreign key{外键名字段}references 主表(主表字段名) ) +表外 +alter table 表名 add constraint 外键名 foreign key (外键名字)references 主表(字段) +删除外键 +alter table drop foreign key 外键名 +设置外键属性 +alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade + +primary key 主键唯一约束且不为空 + +not null 为空 + +default 设置默认值 + +unique 唯一 +``` + +作业 + +```mysql +#1、创建数据库test01_company +create database test01_company; +alter database test01_company charset utf8; +use test01_company; +#2、创建表格offices +#| 字段名 | 数据类型 | +#| ---------- | ----------- | +#| officeCode | int | +#| city | varchar(30) | +#| address | varchar(50) | +#| country | varchar(50) | +#| postalCode | varchar(25) | +create table offices( +officeCode int, +city varchar(50), +address varchar(50), +country varchar(50), +postalCode varchar(25) +); +desc offices; +select*from offices; + +#3、创建表格employees +#| 字段名 | 数据类型 | +#| --------- | ------------- | +#| empNum | int(11) | +#| lastName | varchar(50) | +#| firstName | varchar(50) | +#| mobile | varchar(25) | +#| code | int | +#| jobTitle | varchar(50) | +#| birth | date | +#| Note | varchar(255) |l +#| Sex | varchar(5) | +create table employees( +empNum int(11), +lastNum varchar(50), +firstName varchar(50), +mobile varchar(25), +`code` int, +jobTitle varchar(50), +birth date, +Note varchar(255), +Sex varchar(5) +); +desc employees; +select*from employees; +#要求4:将表employees的mobile字段修改到code字段后面。 +alter table employees modify mobile varchar(25) after code; +#要求5:将表employees的birth字段改名为birthday; +alter table employees change birth birthday date; +#要求6:修改sex字段,数据类型为char(1) +alter table employees modify Sex char(1); +#要求7:删除字段note; +alter table employees drop Note; +#要求8:增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +#要求9:将表employees的名称修改为 employees_in +alter table employees rename employees_info; + +#1、创建数据库test02db +create database test02db; +alter database test02db charset utf8; +use test02db; +#2、创建表格pet +#| 字段名 | 字段说明 | 数据类型 | +#| ------- | -------- | ----------- | +#| name | 宠物名称 | varchar(20) | +#| owner | 宠物主人 | varchar(20) | +#| species | 种类 | varchar(20) | +#| sex | 性别 | char(1) | +#| birth | 出生日期 | year | +#| death | 死亡日期 | year | +create table 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 | | +insert into pet values +('Fluffy', 'harold','Cat','f','2003','2010'), +('Claws','gwen','Cat','m','2004',null), +('Buffy',null, 'Dog','f','2009',null), +('Fang','benny','Dog','m','2000',null), +('bowser','diane','Dog','m','2003','2009'), +('Chitpy',null,'Bird','f','2008',null); +desc pet; +select*from pet; +#4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth year; +#5、 将名称为Claws的猫的主人改为kevin +update pet set name='kevin' where name='Claws'; +#6、 将没有死的狗的主人改为duck +update pet set `owner`='duck' where species='Dog'and death is null; +#7、 查询没有主人的宠物的名字; +select name from pet where owner is null; +#8、 查询已经死了的cat的姓名,主人,以及去世时间; +select*from pet where death is not null; +#9、 删除已经死亡的狗 +delete from pet where death='2009'; +#10、查询所有宠物信息 +select*from pet; + +#1、创建数据库:test03_company +create database test03_company; +alter database test03_company charset utf8; +use test03_company; +#2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +#A.部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +); +#| 部门编号 | 部门名称 | 部门简介 | +#| -------- | -------- | ------------ | +#| 111 | 生产部 | Null | +#| 222 | 销售部 | Null | +#| 333 | 人事部 | 人力资源管理 | +insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源部'); +#B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +#*​雇员编号为主键; +#*部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +#*性别默认为男; +create table employee( +empid int primary key auto_increment, +name varchar(10) not null, +sex enum('男','女') not null default '男', +title varchar(10), +birthday date, +depid int +); + #雇员表: +#| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +#| -------- | ---- | ---- | ---------- | ---------- | ------------ | +#| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +#| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +#| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +#| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +insert into employee values +(1001,'张三','男','高级工程师','1975-1-1',111), +(1002,'李四','女','助工','1985-1-1',111), +(1003,'王五','男','工程师','1978-11-11',222), +(1004,'张六','男','工程师','1999-1-1',222); +alter table employee add constraint kf_employee_depid foreign key (depid) references department(depid) on update cascade on delete cascade; +#C工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +#3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +#| empid |basesalary|titlesarlary| deduction | +#| -------- | -------- | -------- | ---- | +#| 1001 | 2200 | 1100 | 200 | +#| 1002 | 1200 | 200 | NULL | +#| 1003 | 2900 | 700 | 200 | +#| 1004 | 1950 | 700 | 150 | +create table salary( +empid int primary key auto_increment, +basesalary int, +titlesalary int, +deduction int +); +insert into salary values +(1001,2200,1100,200), +(1002,1200,200,null), +(1003,2900,700,200), +(1004,1950,700,150); +desc salary; +select*from salary; +alter table employee add constraint kf_salary_empid foreign key(empid) references salary(empid) on update cascade on delete cascade; + +#1、创建一个数据库:test04_school +create database test04_school; +alter database test04_school charset utf8; +use test04_school; +#2、创建如下表格 +#表1 Department表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +#| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +#| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +create table Deparment( +DepNo int(10) primary key not null, +DepName varchar(20) not null, +DepNote varchar(20) +); +#3、添加记录 +#| **DepNo** | **DepName** | **DepNote** | +#| --------- | ----------- | ------------------ | +#| 601 | 软件技术系 | 软件技术等专业 | +#| 602 | 网络技术系 | 多媒体技术等专业 | +#| 603 | 艺术设计系 | 广告艺术设计等专业 | +#| 604 | 管理工程系 | 连锁经营管理等专业 | +insert into Deparment values(601,'软件技术系','软件技术等专业'), +(602,'网络技术系','多媒体技术等专业'), +(603,'艺术设计系','广告艺术设计等专业'), +(604,'管理工程系','连锁经营管理等专业'); +#表2 Teacher表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| Number | 教工号 | int | 是 | 否 | 是 | 是 | +#| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +#| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +#| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +#| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +#| Salary | 工资 | float | 否 | 否 | 否 | 否 | +#| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +create table Teacher( +Number int primary key, +Name varchar(30) not null, +Sex varchar(4), +Birth date, +DepNo int, +Salary float, +Address varchar(100) +); +#| **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | +#| ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | +#| 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | +#| 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | +#| 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | +#| 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | +#| 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | +insert into Teacher values(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'), +(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'), +(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'), +(2004,'James','女','1980-10-20',602,3500,'四川省成都市'), +(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); +#4、用SELECT语句查询Teacher表的所有记录。 +alter table Teacher add constraint ky_teacher_depno foreign key (DepNo) references Deparment(DepNo) on update cascade on delete cascade; +select*from Teacher; +``` + diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" new file mode 100644 index 0000000..952a0b6 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" @@ -0,0 +1,226 @@ +笔记 + +```mysql +#修改字段位置 +alter table 表名 modify 字段名 数据类型 after 字段; +#修改字段数据类型 +alter table 表名 modify 字段名 新数据类型; +#修改表名 +alter table 表名 rename 新表名 +#删除字段 +alter table 表名 drop column 字段名 +#增加字段 +alter table 表名 add column 字段 数据类型 + +``` + +作业 + +```mysql +#1、创建数据库test01_market +CREATE database test01_market; +use test01_market; +#2、创建表格customers +#| 字段名 | 数据类型 | +#| --------- | ----------- | +#| c_num | int(11) | +#| c_name | varchar(50) | +#| c_contact | varchar(50) | +#| c_city | varchar(50) | +#| c_birth | date | +CREATE TABLE 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字段后面 +ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; +#要求4:将c_name字段数据类型改为 varchar(70) +ALTER TABLE customers MODIFY c_name VARCHAR(70); +#要求5:将c_contact字段改名为c_phone +ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); +#要求6:增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers ADD COLUMN c_gender char(1); +ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; +#要求7:将表名改为customers_info +ALTER TABLE customers RENAME TO customers_info; +#要求8:删除字段c_city +ALTER TABLE customers_info DROP COLUMN c_city; + +#1、创建数据库test02_library +CREATE DATABASE test02_library; +USE test02_library; +#2、创建表格books +CREATE TABLE books( +b_id int(11) not null , +b_name VARCHAR(50) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price FLOAT NOT null , +pubdate year NOT NULL, +note varchar(100) , +num int(11) not NULL +); +DESC 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) 指定所有字段名称插入第一条记录 +INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); +SELECT*FROM books; +#2)不指定字段名称插入第二记录 +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); +#3)同时插入多条记录(剩下的所有记录) +INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +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 | + +#4、将小说类型(novel)的书的价格都增加5。 +UPDATE books set price =price+5 WHERE note='novel'; +#5、将名称为EmmaT的书的价格改为40。 +UPDATE books set price=40 WHERE b_name='EmmaT'; +#6、删除库存为0的记录 +DELETE from books where num=0; +SELECT*FROM books; + +#1、创建数据库test03_bookstore +CREATE DATABASE test03_booksore; +USE test03_booksore; +#2、创建book表 +#+----------+--------------+------+-----+---------+----------------+ +#| 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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE book( +id int(11) UNSIGNED auto_increment PRIMARY KEY not null, +title VARCHAR(100) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price double(11,2) NOT NULL, +sales INT(11) NOT NULL, +stock INT(11) NOT NULL, +img_path VARCHAR(100) NOT null +); +DESC book; +#尝试添加部分模拟数据,参考示例如下: +#```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 | +#+----+---------------+------------+-------+-------+-------+----------------------------+ +INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +SELECT*FROM book; + +#3、创建用户表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE users( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +usersname VARCHAR(100) UNIQUE KEY NOT NULL, +`password` VARCHAR(100) NOT null, +email VARCHAR(100) NULL +); +DESC users; +#尝试添加部分模拟数据,参考示例如下: +#+----+----------+----------------------------------+--------------------+ +#| id | username | password | email | +#+----+----------+----------------------------------+--------------------+ +#| 1 | admin | 112233 | admin@mxdx.com | +#+----+----------+----------------------------------+--------------------+ +INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); +SELECT*FROM users; + +#4、创建订单表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 | | +#+--------------+--------------+------+-----+---------+-------+ +CREATE TABLE orders( +id VARCHAR(100) PRIMARY KEY NOT NULL, +order_time DATETIME NOT NULL, +total_count INT(11) NOT NULL, +total_amount DOUBLE(11,2) NOT NULL, +state INT(11) NOT NULL, +user_id INT(11) not null +); +DESC orders; +#尝试添加部分模拟数据,参考示例如下: +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| id | order_time | total_count | total_amount | state | user_id | +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +#+----------------+---------------------+-------------+--------------+-------+----- +INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); +SELECT*FROM orders; + +#5、创建订单明细表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE order_items( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +count INT(11) 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 +); +DESC 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 | +#+----+-------+--------+------------+----------+-------+------------+----------------+ +INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); +SELECT*FROM order_items; +``` + -- Gitee From 9186d8b7324edd7b9690952e967f5162b8e1f45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:26:13 +0000 Subject: [PATCH 05/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/20230221=20=E5=BB=BA=E5=BA=93?= =?UTF-8?q?=E5=BB=BA=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\345\272\223\345\273\272\350\241\250.md" | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" deleted file mode 100644 index cfdd8c8..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/20230221 \345\273\272\345\272\223\345\273\272\350\241\250.md" +++ /dev/null @@ -1,35 +0,0 @@ -```mysql -笔记 - -DATABASE 数据库 -#创建数据库 -show database 数据库名字 -使用数据库 -use 数据库 -插入数据到表中 -insert into 表名字 values() -``` - -```mysql -作业 - -SHOW DATABASES; -CREATE DATABASE class;#创建数据库 -use class;#使用数据库 -#创建表单 -CREATE TABLE student( -student_学号 VARCHAR(12), -student_姓名 VARCHAR(4), -student_性别 VARCHAR(2), -student_爱好 VARCHAR(10), -student_住址 VARCHAR(15), -student_电话 VARCHAR(15), -student_邮箱 VARCHAR(20), -student_QQ VARCHAR(15) -); -DESC student;#查询表单结构 -#插入数据 -INSERT into student VALUES('2244310155','杨旭东','男','打游戏,羽毛球','江贵州省贵阳市白云区','18275209683','1875779940@qq.com','1875779940') - -``` - -- Gitee From b1058deab195642f006141c6361e47e25837f892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:26:37 +0000 Subject: [PATCH 06/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/20230220=E7=BA=A6=E6=9D=9F?= =?UTF-8?q?=E7=9A=84=E5=AD=A6=E4=B9=A0=E5=92=8C=E4=BD=9C=E4=B8=9A.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\271\240\345\222\214\344\275\234\344\270\232.md" | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" "b/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" deleted file mode 100644 index 8381af8..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" +++ /dev/null @@ -1,13 +0,0 @@ -```mysql -cr ta 表格名( - 字段 类型 primary key - 或者 primary key(字段1,字段2) - 或primary key(字段) -); -#建表后增加主键: -alter table 表名 add primary key(字段 (可以多个)) -#删除: -alter table 表名 drop primary key; -``` - - -- Gitee From cc84643d9c8d68b606f083b207c7d2ff6aa10c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:31:19 +0000 Subject: [PATCH 07/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/#1=E3=80=81=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93test01=5Fmarket.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\346\215\256\345\272\223test01_market.md" | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" deleted file mode 100644 index 60bbbc1..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" +++ /dev/null @@ -1,31 +0,0 @@ -#1、创建数据库test01_market -CREATE database test01_market; -use test01_market; -#2、创建表格customers -#| 字段名 | 数据类型 | -#| --------- | ----------- | -#| c_num | int(11) | -#| c_name | varchar(50) | -#| c_contact | varchar(50) | -#| c_city | varchar(50) | -#| c_birth | date | -CREATE TABLE 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字段后面 -ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; -#要求4:将c_name字段数据类型改为 varchar(70) -ALTER TABLE customers MODIFY c_name VARCHAR(70); -#要求5:将c_contact字段改名为c_phone -ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); -#要求6:增加c_gender字段到c_name后面,数据类型为char(1) -ALTER TABLE customers ADD COLUMN c_gender char(1); -ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; -#要求7:将表名改为customers_info -ALTER TABLE customers RENAME TO customers_info; -#要求8:删除字段c_city -ALTER TABLE customers_info DROP COLUMN c_city; \ No newline at end of file -- Gitee From d5f8d1ca994fb4224b2a710d39b23e3a8cd4fd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:31:25 +0000 Subject: [PATCH 08/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/#1=E3=80=81=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93test02=5Flibrary(1).md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\215\256\345\272\223test02_library(1).md" | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" deleted file mode 100644 index fdf1167..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" +++ /dev/null @@ -1,49 +0,0 @@ -#1、创建数据库test02_library -CREATE DATABASE test02_library; -USE test02_library; -#2、创建表格books -CREATE TABLE books( -b_id int(11) not null , -b_name VARCHAR(50) NOT NULL, -`authors` VARCHAR(100) NOT NULL, -price FLOAT NOT null , -pubdate year NOT NULL, -note varchar(100) , -num int(11) not NULL -); -DESC 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) 指定所有字段名称插入第一条记录 -INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); -SELECT*FROM books; -#2)不指定字段名称插入第二记录 -INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); -#3)同时插入多条记录(剩下的所有记录) -INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); -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 | - -#4、将小说类型(novel)的书的价格都增加5。 -UPDATE books set price =price+5 WHERE note='novel'; -#5、将名称为EmmaT的书的价格改为40。 -UPDATE books set price=40 WHERE b_name='EmmaT'; -#6、删除库存为0的记录 -DELETE from books where num=0; -SELECT*FROM books; \ No newline at end of file -- Gitee From 3dcee363e431dc2b088050d0bea768e36d493e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:31:31 +0000 Subject: [PATCH 09/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/#1=E3=80=81=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93test03=5Fbookstore.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\215\256\345\272\223test03_bookstore.md" | 123 ------------------ 1 file changed, 123 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" deleted file mode 100644 index d957f1a..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" +++ /dev/null @@ -1,123 +0,0 @@ -#1、创建数据库test03_bookstore -CREATE DATABASE test03_booksore; -USE test03_booksore; -#2、创建book表 -#+----------+--------------+------+-----+---------+----------------+ -#| 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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE book( -id int(11) UNSIGNED auto_increment PRIMARY KEY not null, -title VARCHAR(100) NOT NULL, -`authors` VARCHAR(100) NOT NULL, -price double(11,2) NOT NULL, -sales INT(11) NOT NULL, -stock INT(11) NOT NULL, -img_path VARCHAR(100) NOT null -); -DESC book; -#尝试添加部分模拟数据,参考示例如下: -#```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 | -#+----+---------------+------------+-------+-------+-------+----------------------------+ -INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); -SELECT*FROM book; - -#3、创建用户表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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE users( -id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, -usersname VARCHAR(100) UNIQUE KEY NOT NULL, -`password` VARCHAR(100) NOT null, -email VARCHAR(100) NULL -); -DESC users; -#尝试添加部分模拟数据,参考示例如下: -#+----+----------+----------------------------------+--------------------+ -#| id | username | password | email | -#+----+----------+----------------------------------+--------------------+ -#| 1 | admin | 112233 | admin@mxdx.com | -#+----+----------+----------------------------------+--------------------+ -INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); -SELECT*FROM users; - -#4、创建订单表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 | | -#+--------------+--------------+------+-----+---------+-------+ -CREATE TABLE orders( -id VARCHAR(100) PRIMARY KEY NOT NULL, -order_time DATETIME NOT NULL, -total_count INT(11) NOT NULL, -total_amount DOUBLE(11,2) NOT NULL, -state INT(11) NOT NULL, -user_id INT(11) not null -); -DESC orders; -#尝试添加部分模拟数据,参考示例如下: -#+----------------+---------------------+-------------+--------------+-------+---------+ -#| id | order_time | total_count | total_amount | state | user_id | -#+----------------+---------------------+-------------+--------------+-------+---------+ -#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | -#+----------------+---------------------+-------------+--------------+-------+----- -INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); -SELECT*FROM orders; - -#5、创建订单明细表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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE order_items( -id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, -count INT(11) 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 -); -DESC 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 | -#+----+-------+--------+------------+----------+-------+------------+----------------+ -INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); -SELECT*FROM order_items; \ No newline at end of file -- Gitee From 52b4b13664f4f1d6d10c6343591c39aac0eb6c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:31:39 +0000 Subject: [PATCH 10/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/20230221=20=E7=BA=A6=E6=9D=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230221 \347\272\246\346\235\237.md" | 269 ------------------ 1 file changed, 269 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" deleted file mode 100644 index 8de5b0b..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" +++ /dev/null @@ -1,269 +0,0 @@ -笔记 - -```mysql - - -添加外键 -表内 -create table 表名(。。。。。 {constraint}{外键名称}foreign key{外键名字段}references 主表(主表字段名) ) -表外 -alter table 表名 add constraint 外键名 foreign key (外键名字)references 主表(字段) -删除外键 -alter table drop foreign key 外键名 -设置外键属性 -alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade - -primary key 主键唯一约束且不为空 - -not null 为空 - -default 设置默认值 - -unique 唯一 -``` - -作业 - -```mysql -#1、创建数据库test01_company -create database test01_company; -alter database test01_company charset utf8; -use test01_company; -#2、创建表格offices -#| 字段名 | 数据类型 | -#| ---------- | ----------- | -#| officeCode | int | -#| city | varchar(30) | -#| address | varchar(50) | -#| country | varchar(50) | -#| postalCode | varchar(25) | -create table offices( -officeCode int, -city varchar(50), -address varchar(50), -country varchar(50), -postalCode varchar(25) -); -desc offices; -select*from offices; - -#3、创建表格employees -#| 字段名 | 数据类型 | -#| --------- | ------------- | -#| empNum | int(11) | -#| lastName | varchar(50) | -#| firstName | varchar(50) | -#| mobile | varchar(25) | -#| code | int | -#| jobTitle | varchar(50) | -#| birth | date | -#| Note | varchar(255) |l -#| Sex | varchar(5) | -create table employees( -empNum int(11), -lastNum varchar(50), -firstName varchar(50), -mobile varchar(25), -`code` int, -jobTitle varchar(50), -birth date, -Note varchar(255), -Sex varchar(5) -); -desc employees; -select*from employees; -#要求4:将表employees的mobile字段修改到code字段后面。 -alter table employees modify mobile varchar(25) after code; -#要求5:将表employees的birth字段改名为birthday; -alter table employees change birth birthday date; -#要求6:修改sex字段,数据类型为char(1) -alter table employees modify Sex char(1); -#要求7:删除字段note; -alter table employees drop Note; -#要求8:增加字段名favoriate_activity,数据类型为varchar(100); -alter table employees add favoriate_activity varchar(100); -#要求9:将表employees的名称修改为 employees_in -alter table employees rename employees_info; - -#1、创建数据库test02db -create database test02db; -alter database test02db charset utf8; -use test02db; -#2、创建表格pet -#| 字段名 | 字段说明 | 数据类型 | -#| ------- | -------- | ----------- | -#| name | 宠物名称 | varchar(20) | -#| owner | 宠物主人 | varchar(20) | -#| species | 种类 | varchar(20) | -#| sex | 性别 | char(1) | -#| birth | 出生日期 | year | -#| death | 死亡日期 | year | -create table 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 | | -insert into pet values -('Fluffy', 'harold','Cat','f','2003','2010'), -('Claws','gwen','Cat','m','2004',null), -('Buffy',null, 'Dog','f','2009',null), -('Fang','benny','Dog','m','2000',null), -('bowser','diane','Dog','m','2003','2009'), -('Chitpy',null,'Bird','f','2008',null); -desc pet; -select*from pet; -#4、 添加字段主人的生日owner_birth。 -alter table pet add owner_birth year; -#5、 将名称为Claws的猫的主人改为kevin -update pet set name='kevin' where name='Claws'; -#6、 将没有死的狗的主人改为duck -update pet set `owner`='duck' where species='Dog'and death is null; -#7、 查询没有主人的宠物的名字; -select name from pet where owner is null; -#8、 查询已经死了的cat的姓名,主人,以及去世时间; -select*from pet where death is not null; -#9、 删除已经死亡的狗 -delete from pet where death='2009'; -#10、查询所有宠物信息 -select*from pet; - -#1、创建数据库:test03_company -create database test03_company; -alter database test03_company charset utf8; -use test03_company; -#2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 -#A.部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 -create table department( - depid int primary key auto_increment, - depname char(10) not null unique key, - deinfo varchar(200) -); -#| 部门编号 | 部门名称 | 部门简介 | -#| -------- | -------- | ------------ | -#| 111 | 生产部 | Null | -#| 222 | 销售部 | Null | -#| 333 | 人事部 | 人力资源管理 | -insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源部'); -#B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 -#*​雇员编号为主键; -#*部门编号为外键,外键约束等级为(on update cascade 和on delete set null); -#*性别默认为男; -create table employee( -empid int primary key auto_increment, -name varchar(10) not null, -sex enum('男','女') not null default '男', -title varchar(10), -birthday date, -depid int -); - #雇员表: -#| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | -#| -------- | ---- | ---- | ---------- | ---------- | ------------ | -#| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | -#| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | -#| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | -#| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | -insert into employee values -(1001,'张三','男','高级工程师','1975-1-1',111), -(1002,'李四','女','助工','1985-1-1',111), -(1003,'王五','男','工程师','1978-11-11',222), -(1004,'张六','男','工程师','1999-1-1',222); -alter table employee add constraint kf_employee_depid foreign key (depid) references department(depid) on update cascade on delete cascade; -#C工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 -#3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) -#| empid |basesalary|titlesarlary| deduction | -#| -------- | -------- | -------- | ---- | -#| 1001 | 2200 | 1100 | 200 | -#| 1002 | 1200 | 200 | NULL | -#| 1003 | 2900 | 700 | 200 | -#| 1004 | 1950 | 700 | 150 | -create table salary( -empid int primary key auto_increment, -basesalary int, -titlesalary int, -deduction int -); -insert into salary values -(1001,2200,1100,200), -(1002,1200,200,null), -(1003,2900,700,200), -(1004,1950,700,150); -desc salary; -select*from salary; -alter table employee add constraint kf_salary_empid foreign key(empid) references salary(empid) on update cascade on delete cascade; - -#1、创建一个数据库:test04_school -create database test04_school; -alter database test04_school charset utf8; -use test04_school; -#2、创建如下表格 -#表1 Department表的定义 -#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -#| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | -#| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | -#| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | -create table Deparment( -DepNo int(10) primary key not null, -DepName varchar(20) not null, -DepNote varchar(20) -); -#3、添加记录 -#| **DepNo** | **DepName** | **DepNote** | -#| --------- | ----------- | ------------------ | -#| 601 | 软件技术系 | 软件技术等专业 | -#| 602 | 网络技术系 | 多媒体技术等专业 | -#| 603 | 艺术设计系 | 广告艺术设计等专业 | -#| 604 | 管理工程系 | 连锁经营管理等专业 | -insert into Deparment values(601,'软件技术系','软件技术等专业'), -(602,'网络技术系','多媒体技术等专业'), -(603,'艺术设计系','广告艺术设计等专业'), -(604,'管理工程系','连锁经营管理等专业'); -#表2 Teacher表的定义 -#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | -#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | -#| Number | 教工号 | int | 是 | 否 | 是 | 是 | -#| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | -#| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | -#| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | -#| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | -#| Salary | 工资 | float | 否 | 否 | 否 | 否 | -#| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | -create table Teacher( -Number int primary key, -Name varchar(30) not null, -Sex varchar(4), -Birth date, -DepNo int, -Salary float, -Address varchar(100) -); -#| **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | -#| ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | -#| 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | -#| 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | -#| 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | -#| 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | -#| 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | -insert into Teacher values(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'), -(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'), -(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'), -(2004,'James','女','1980-10-20',602,3500,'四川省成都市'), -(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); -#4、用SELECT语句查询Teacher表的所有记录。 -alter table Teacher add constraint ky_teacher_depno foreign key (DepNo) references Deparment(DepNo) on update cascade on delete cascade; -select*from Teacher; -``` - -- Gitee From 36bed2d0f826f20b7499a02f1a50a98a594f9955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:31:50 +0000 Subject: [PATCH 11/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA/20230221DDL=E5=92=8CDML.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230221DDL\345\222\214DML.md" | 226 ------------------ 1 file changed, 226 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" deleted file mode 100644 index 952a0b6..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" +++ /dev/null @@ -1,226 +0,0 @@ -笔记 - -```mysql -#修改字段位置 -alter table 表名 modify 字段名 数据类型 after 字段; -#修改字段数据类型 -alter table 表名 modify 字段名 新数据类型; -#修改表名 -alter table 表名 rename 新表名 -#删除字段 -alter table 表名 drop column 字段名 -#增加字段 -alter table 表名 add column 字段 数据类型 - -``` - -作业 - -```mysql -#1、创建数据库test01_market -CREATE database test01_market; -use test01_market; -#2、创建表格customers -#| 字段名 | 数据类型 | -#| --------- | ----------- | -#| c_num | int(11) | -#| c_name | varchar(50) | -#| c_contact | varchar(50) | -#| c_city | varchar(50) | -#| c_birth | date | -CREATE TABLE 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字段后面 -ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; -#要求4:将c_name字段数据类型改为 varchar(70) -ALTER TABLE customers MODIFY c_name VARCHAR(70); -#要求5:将c_contact字段改名为c_phone -ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); -#要求6:增加c_gender字段到c_name后面,数据类型为char(1) -ALTER TABLE customers ADD COLUMN c_gender char(1); -ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; -#要求7:将表名改为customers_info -ALTER TABLE customers RENAME TO customers_info; -#要求8:删除字段c_city -ALTER TABLE customers_info DROP COLUMN c_city; - -#1、创建数据库test02_library -CREATE DATABASE test02_library; -USE test02_library; -#2、创建表格books -CREATE TABLE books( -b_id int(11) not null , -b_name VARCHAR(50) NOT NULL, -`authors` VARCHAR(100) NOT NULL, -price FLOAT NOT null , -pubdate year NOT NULL, -note varchar(100) , -num int(11) not NULL -); -DESC 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) 指定所有字段名称插入第一条记录 -INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); -SELECT*FROM books; -#2)不指定字段名称插入第二记录 -INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); -#3)同时插入多条记录(剩下的所有记录) -INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); -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 | - -#4、将小说类型(novel)的书的价格都增加5。 -UPDATE books set price =price+5 WHERE note='novel'; -#5、将名称为EmmaT的书的价格改为40。 -UPDATE books set price=40 WHERE b_name='EmmaT'; -#6、删除库存为0的记录 -DELETE from books where num=0; -SELECT*FROM books; - -#1、创建数据库test03_bookstore -CREATE DATABASE test03_booksore; -USE test03_booksore; -#2、创建book表 -#+----------+--------------+------+-----+---------+----------------+ -#| 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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE book( -id int(11) UNSIGNED auto_increment PRIMARY KEY not null, -title VARCHAR(100) NOT NULL, -`authors` VARCHAR(100) NOT NULL, -price double(11,2) NOT NULL, -sales INT(11) NOT NULL, -stock INT(11) NOT NULL, -img_path VARCHAR(100) NOT null -); -DESC book; -#尝试添加部分模拟数据,参考示例如下: -#```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 | -#+----+---------------+------------+-------+-------+-------+----------------------------+ -INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); -SELECT*FROM book; - -#3、创建用户表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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE users( -id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, -usersname VARCHAR(100) UNIQUE KEY NOT NULL, -`password` VARCHAR(100) NOT null, -email VARCHAR(100) NULL -); -DESC users; -#尝试添加部分模拟数据,参考示例如下: -#+----+----------+----------------------------------+--------------------+ -#| id | username | password | email | -#+----+----------+----------------------------------+--------------------+ -#| 1 | admin | 112233 | admin@mxdx.com | -#+----+----------+----------------------------------+--------------------+ -INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); -SELECT*FROM users; - -#4、创建订单表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 | | -#+--------------+--------------+------+-----+---------+-------+ -CREATE TABLE orders( -id VARCHAR(100) PRIMARY KEY NOT NULL, -order_time DATETIME NOT NULL, -total_count INT(11) NOT NULL, -total_amount DOUBLE(11,2) NOT NULL, -state INT(11) NOT NULL, -user_id INT(11) not null -); -DESC orders; -#尝试添加部分模拟数据,参考示例如下: -#+----------------+---------------------+-------------+--------------+-------+---------+ -#| id | order_time | total_count | total_amount | state | user_id | -#+----------------+---------------------+-------------+--------------+-------+---------+ -#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | -#+----------------+---------------------+-------------+--------------+-------+----- -INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); -SELECT*FROM orders; - -#5、创建订单明细表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 | | -#+----------+--------------+------+-----+---------+----------------+ -CREATE TABLE order_items( -id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, -count INT(11) 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 -); -DESC 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 | -#+----+-------+--------+------------+----------+-------+------------+----------------+ -INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); -SELECT*FROM order_items; -``` - -- Gitee From be29d3cdcec8ae1f4f33f5293e59f863dd55ec0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 15:40:43 +0000 Subject: [PATCH 12/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2017?= =?UTF-8?q?=20=E8=91=A3=E5=BA=86=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 "17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" "b/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" deleted file mode 100644 index a72df23..0000000 --- "a/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" +++ /dev/null @@ -1,3 +0,0 @@ -``` - -``` -- Gitee From a30f2548abc0141def2aaf211b0008e0df1a4a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA?= Date: Tue, 21 Feb 2023 23:48:23 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E8=91=A3=E5=BA=86=E5=BC=BA=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\346\215\256\345\272\223test01_market.md" | 31 ++ ...6\215\256\345\272\223test02_library(1).md" | 49 ++++ ...46\215\256\345\272\223test03_bookstore.md" | 123 ++++++++ ...52\344\272\272\344\273\213\347\273\215.md" | 34 +++ .../20230221 \347\272\246\346\235\237.md" | 269 ++++++++++++++++++ .../20230221DDL\345\222\214DML.md" | 226 +++++++++++++++ ...07\346\234\254\346\226\207\346\241\243.md" | 3 + 7 files changed, 735 insertions(+) create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \344\270\252\344\272\272\344\273\213\347\273\215.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" create mode 100644 "17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" new file mode 100644 index 0000000..60bbbc1 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test01_market.md" @@ -0,0 +1,31 @@ +#1、创建数据库test01_market +CREATE database test01_market; +use test01_market; +#2、创建表格customers +#| 字段名 | 数据类型 | +#| --------- | ----------- | +#| c_num | int(11) | +#| c_name | varchar(50) | +#| c_contact | varchar(50) | +#| c_city | varchar(50) | +#| c_birth | date | +CREATE TABLE 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字段后面 +ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; +#要求4:将c_name字段数据类型改为 varchar(70) +ALTER TABLE customers MODIFY c_name VARCHAR(70); +#要求5:将c_contact字段改名为c_phone +ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); +#要求6:增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers ADD COLUMN c_gender char(1); +ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; +#要求7:将表名改为customers_info +ALTER TABLE customers RENAME TO customers_info; +#要求8:删除字段c_city +ALTER TABLE customers_info DROP COLUMN c_city; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" new file mode 100644 index 0000000..fdf1167 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test02_library(1).md" @@ -0,0 +1,49 @@ +#1、创建数据库test02_library +CREATE DATABASE test02_library; +USE test02_library; +#2、创建表格books +CREATE TABLE books( +b_id int(11) not null , +b_name VARCHAR(50) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price FLOAT NOT null , +pubdate year NOT NULL, +note varchar(100) , +num int(11) not NULL +); +DESC 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) 指定所有字段名称插入第一条记录 +INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); +SELECT*FROM books; +#2)不指定字段名称插入第二记录 +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); +#3)同时插入多条记录(剩下的所有记录) +INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +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 | + +#4、将小说类型(novel)的书的价格都增加5。 +UPDATE books set price =price+5 WHERE note='novel'; +#5、将名称为EmmaT的书的价格改为40。 +UPDATE books set price=40 WHERE b_name='EmmaT'; +#6、删除库存为0的记录 +DELETE from books where num=0; +SELECT*FROM books; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" new file mode 100644 index 0000000..d957f1a --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/#1\343\200\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223test03_bookstore.md" @@ -0,0 +1,123 @@ +#1、创建数据库test03_bookstore +CREATE DATABASE test03_booksore; +USE test03_booksore; +#2、创建book表 +#+----------+--------------+------+-----+---------+----------------+ +#| 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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE book( +id int(11) UNSIGNED auto_increment PRIMARY KEY not null, +title VARCHAR(100) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price double(11,2) NOT NULL, +sales INT(11) NOT NULL, +stock INT(11) NOT NULL, +img_path VARCHAR(100) NOT null +); +DESC book; +#尝试添加部分模拟数据,参考示例如下: +#```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 | +#+----+---------------+------------+-------+-------+-------+----------------------------+ +INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +SELECT*FROM book; + +#3、创建用户表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE users( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +usersname VARCHAR(100) UNIQUE KEY NOT NULL, +`password` VARCHAR(100) NOT null, +email VARCHAR(100) NULL +); +DESC users; +#尝试添加部分模拟数据,参考示例如下: +#+----+----------+----------------------------------+--------------------+ +#| id | username | password | email | +#+----+----------+----------------------------------+--------------------+ +#| 1 | admin | 112233 | admin@mxdx.com | +#+----+----------+----------------------------------+--------------------+ +INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); +SELECT*FROM users; + +#4、创建订单表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 | | +#+--------------+--------------+------+-----+---------+-------+ +CREATE TABLE orders( +id VARCHAR(100) PRIMARY KEY NOT NULL, +order_time DATETIME NOT NULL, +total_count INT(11) NOT NULL, +total_amount DOUBLE(11,2) NOT NULL, +state INT(11) NOT NULL, +user_id INT(11) not null +); +DESC orders; +#尝试添加部分模拟数据,参考示例如下: +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| id | order_time | total_count | total_amount | state | user_id | +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +#+----------------+---------------------+-------------+--------------+-------+----- +INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); +SELECT*FROM orders; + +#5、创建订单明细表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE order_items( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +count INT(11) 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 +); +DESC 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 | +#+----+-------+--------+------------+----------+-------+------------+----------------+ +INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); +SELECT*FROM order_items; \ No newline at end of file diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \344\270\252\344\272\272\344\273\213\347\273\215.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \344\270\252\344\272\272\344\273\213\347\273\215.md" new file mode 100644 index 0000000..05bac79 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221 \344\270\252\344\272\272\344\273\213\347\273\215.md" @@ -0,0 +1,34 @@ +```mysql +笔记 + +DATABASE 数据库 +#创建数据库 +show database 数据库名字 +使用数据库 +use 数据库 +插入数据到表中 +insert into 表名字 values() +``` + +```mysql +作业 + +SHOW DATABASES; +CREATE DATABASE class;#创建数据库 +use class;#使用数据库 +#创建表单 +CREATE TABLE student( +student_学号 VARCHAR(12), +student_姓名 VARCHAR(4), +student_性别 VARCHAR(2), +student_爱好 VARCHAR(10), +student_住址 VARCHAR(15), +student_电话 VARCHAR(15), +student_邮箱 VARCHAR(20), +student_QQ VARCHAR(15) +); +DESC student;#查询表单结构 +#插入数据 +INSERT into student VALUES('2244310117','董庆强','男','打游戏,羽毛球','江福建省泉州市丰泽区','15980012393','无','2314084770') + +``` diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" new file mode 100644 index 0000000..8de5b0b --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221 \347\272\246\346\235\237.md" @@ -0,0 +1,269 @@ +笔记 + +```mysql + + +添加外键 +表内 +create table 表名(。。。。。 {constraint}{外键名称}foreign key{外键名字段}references 主表(主表字段名) ) +表外 +alter table 表名 add constraint 外键名 foreign key (外键名字)references 主表(字段) +删除外键 +alter table drop foreign key 外键名 +设置外键属性 +alter table 表单 add constraint 外键名称 foreign key (外键字段)references on update cascade on delete cascade + +primary key 主键唯一约束且不为空 + +not null 为空 + +default 设置默认值 + +unique 唯一 +``` + +作业 + +```mysql +#1、创建数据库test01_company +create database test01_company; +alter database test01_company charset utf8; +use test01_company; +#2、创建表格offices +#| 字段名 | 数据类型 | +#| ---------- | ----------- | +#| officeCode | int | +#| city | varchar(30) | +#| address | varchar(50) | +#| country | varchar(50) | +#| postalCode | varchar(25) | +create table offices( +officeCode int, +city varchar(50), +address varchar(50), +country varchar(50), +postalCode varchar(25) +); +desc offices; +select*from offices; + +#3、创建表格employees +#| 字段名 | 数据类型 | +#| --------- | ------------- | +#| empNum | int(11) | +#| lastName | varchar(50) | +#| firstName | varchar(50) | +#| mobile | varchar(25) | +#| code | int | +#| jobTitle | varchar(50) | +#| birth | date | +#| Note | varchar(255) |l +#| Sex | varchar(5) | +create table employees( +empNum int(11), +lastNum varchar(50), +firstName varchar(50), +mobile varchar(25), +`code` int, +jobTitle varchar(50), +birth date, +Note varchar(255), +Sex varchar(5) +); +desc employees; +select*from employees; +#要求4:将表employees的mobile字段修改到code字段后面。 +alter table employees modify mobile varchar(25) after code; +#要求5:将表employees的birth字段改名为birthday; +alter table employees change birth birthday date; +#要求6:修改sex字段,数据类型为char(1) +alter table employees modify Sex char(1); +#要求7:删除字段note; +alter table employees drop Note; +#要求8:增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +#要求9:将表employees的名称修改为 employees_in +alter table employees rename employees_info; + +#1、创建数据库test02db +create database test02db; +alter database test02db charset utf8; +use test02db; +#2、创建表格pet +#| 字段名 | 字段说明 | 数据类型 | +#| ------- | -------- | ----------- | +#| name | 宠物名称 | varchar(20) | +#| owner | 宠物主人 | varchar(20) | +#| species | 种类 | varchar(20) | +#| sex | 性别 | char(1) | +#| birth | 出生日期 | year | +#| death | 死亡日期 | year | +create table 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 | | +insert into pet values +('Fluffy', 'harold','Cat','f','2003','2010'), +('Claws','gwen','Cat','m','2004',null), +('Buffy',null, 'Dog','f','2009',null), +('Fang','benny','Dog','m','2000',null), +('bowser','diane','Dog','m','2003','2009'), +('Chitpy',null,'Bird','f','2008',null); +desc pet; +select*from pet; +#4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth year; +#5、 将名称为Claws的猫的主人改为kevin +update pet set name='kevin' where name='Claws'; +#6、 将没有死的狗的主人改为duck +update pet set `owner`='duck' where species='Dog'and death is null; +#7、 查询没有主人的宠物的名字; +select name from pet where owner is null; +#8、 查询已经死了的cat的姓名,主人,以及去世时间; +select*from pet where death is not null; +#9、 删除已经死亡的狗 +delete from pet where death='2009'; +#10、查询所有宠物信息 +select*from pet; + +#1、创建数据库:test03_company +create database test03_company; +alter database test03_company charset utf8; +use test03_company; +#2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +#A.部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +); +#| 部门编号 | 部门名称 | 部门简介 | +#| -------- | -------- | ------------ | +#| 111 | 生产部 | Null | +#| 222 | 销售部 | Null | +#| 333 | 人事部 | 人力资源管理 | +insert into department values(111,'生产部',null),(222,'销售部',null),(333,'人事部','人力资源部'); +#B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +#*​雇员编号为主键; +#*部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +#*性别默认为男; +create table employee( +empid int primary key auto_increment, +name varchar(10) not null, +sex enum('男','女') not null default '男', +title varchar(10), +birthday date, +depid int +); + #雇员表: +#| 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +#| -------- | ---- | ---- | ---------- | ---------- | ------------ | +#| 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +#| 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +#| 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +#| 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +insert into employee values +(1001,'张三','男','高级工程师','1975-1-1',111), +(1002,'李四','女','助工','1985-1-1',111), +(1003,'王五','男','工程师','1978-11-11',222), +(1004,'张六','男','工程师','1999-1-1',222); +alter table employee add constraint kf_employee_depid foreign key (depid) references department(depid) on update cascade on delete cascade; +#C工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +#3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +#| empid |basesalary|titlesarlary| deduction | +#| -------- | -------- | -------- | ---- | +#| 1001 | 2200 | 1100 | 200 | +#| 1002 | 1200 | 200 | NULL | +#| 1003 | 2900 | 700 | 200 | +#| 1004 | 1950 | 700 | 150 | +create table salary( +empid int primary key auto_increment, +basesalary int, +titlesalary int, +deduction int +); +insert into salary values +(1001,2200,1100,200), +(1002,1200,200,null), +(1003,2900,700,200), +(1004,1950,700,150); +desc salary; +select*from salary; +alter table employee add constraint kf_salary_empid foreign key(empid) references salary(empid) on update cascade on delete cascade; + +#1、创建一个数据库:test04_school +create database test04_school; +alter database test04_school charset utf8; +use test04_school; +#2、创建如下表格 +#表1 Department表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +#| DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +#| DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +create table Deparment( +DepNo int(10) primary key not null, +DepName varchar(20) not null, +DepNote varchar(20) +); +#3、添加记录 +#| **DepNo** | **DepName** | **DepNote** | +#| --------- | ----------- | ------------------ | +#| 601 | 软件技术系 | 软件技术等专业 | +#| 602 | 网络技术系 | 多媒体技术等专业 | +#| 603 | 艺术设计系 | 广告艺术设计等专业 | +#| 604 | 管理工程系 | 连锁经营管理等专业 | +insert into Deparment values(601,'软件技术系','软件技术等专业'), +(602,'网络技术系','多媒体技术等专业'), +(603,'艺术设计系','广告艺术设计等专业'), +(604,'管理工程系','连锁经营管理等专业'); +#表2 Teacher表的定义 +#| **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +#| ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +#| Number | 教工号 | int | 是 | 否 | 是 | 是 | +#| Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +#| Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +#| Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +#| DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +#| Salary | 工资 | float | 否 | 否 | 否 | 否 | +#| Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +create table Teacher( +Number int primary key, +Name varchar(30) not null, +Sex varchar(4), +Birth date, +DepNo int, +Salary float, +Address varchar(100) +); +#| **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | +#| ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | +#| 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | +#| 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | +#| 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | +#| 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | +#| 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | +insert into Teacher values(2001,'Tom','女','1970-01-10',602,4500,'四川省绵阳市'), +(2002,'Lucy','男','1983-12-18',601,2500,'北京市昌平区'), +(2003,'Mike','男','1990-06-01',604,1500,'重庆市渝中区'), +(2004,'James','女','1980-10-20',602,3500,'四川省成都市'), +(2005,'Jack','男','1975-05-30',603,1200,'重庆市南岸区'); +#4、用SELECT语句查询Teacher表的所有记录。 +alter table Teacher add constraint ky_teacher_depno foreign key (DepNo) references Deparment(DepNo) on update cascade on delete cascade; +select*from Teacher; +``` + diff --git "a/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" "b/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" new file mode 100644 index 0000000..952a0b6 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/20230221DDL\345\222\214DML.md" @@ -0,0 +1,226 @@ +笔记 + +```mysql +#修改字段位置 +alter table 表名 modify 字段名 数据类型 after 字段; +#修改字段数据类型 +alter table 表名 modify 字段名 新数据类型; +#修改表名 +alter table 表名 rename 新表名 +#删除字段 +alter table 表名 drop column 字段名 +#增加字段 +alter table 表名 add column 字段 数据类型 + +``` + +作业 + +```mysql +#1、创建数据库test01_market +CREATE database test01_market; +use test01_market; +#2、创建表格customers +#| 字段名 | 数据类型 | +#| --------- | ----------- | +#| c_num | int(11) | +#| c_name | varchar(50) | +#| c_contact | varchar(50) | +#| c_city | varchar(50) | +#| c_birth | date | +CREATE TABLE 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字段后面 +ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; +#要求4:将c_name字段数据类型改为 varchar(70) +ALTER TABLE customers MODIFY c_name VARCHAR(70); +#要求5:将c_contact字段改名为c_phone +ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); +#要求6:增加c_gender字段到c_name后面,数据类型为char(1) +ALTER TABLE customers ADD COLUMN c_gender char(1); +ALTER TABLE customers MODIFY c_gender char(1) AFTER c_name; +#要求7:将表名改为customers_info +ALTER TABLE customers RENAME TO customers_info; +#要求8:删除字段c_city +ALTER TABLE customers_info DROP COLUMN c_city; + +#1、创建数据库test02_library +CREATE DATABASE test02_library; +USE test02_library; +#2、创建表格books +CREATE TABLE books( +b_id int(11) not null , +b_name VARCHAR(50) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price FLOAT NOT null , +pubdate year NOT NULL, +note varchar(100) , +num int(11) not NULL +); +DESC 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) 指定所有字段名称插入第一条记录 +INSERT INTO books (b_id,b_name,`authors`,price,pubdate,note,num) VALUES (1,'Tal of AAA','Dickes',23,'1995','novel',11); +SELECT*FROM books; +#2)不指定字段名称插入第二记录 +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,'1993','joke',22); +#3)同时插入多条记录(剩下的所有记录) +INSERT INTO books VALUES(3,'Story of Jane','Jane Tim',40,'2001','novel',0),(4,'Lovey Day','George Byron',20,'2005','novel',30),(5,'Old land','Honore Byron',20,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +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 | + +#4、将小说类型(novel)的书的价格都增加5。 +UPDATE books set price =price+5 WHERE note='novel'; +#5、将名称为EmmaT的书的价格改为40。 +UPDATE books set price=40 WHERE b_name='EmmaT'; +#6、删除库存为0的记录 +DELETE from books where num=0; +SELECT*FROM books; + +#1、创建数据库test03_bookstore +CREATE DATABASE test03_booksore; +USE test03_booksore; +#2、创建book表 +#+----------+--------------+------+-----+---------+----------------+ +#| 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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE book( +id int(11) UNSIGNED auto_increment PRIMARY KEY not null, +title VARCHAR(100) NOT NULL, +`authors` VARCHAR(100) NOT NULL, +price double(11,2) NOT NULL, +sales INT(11) NOT NULL, +stock INT(11) NOT NULL, +img_path VARCHAR(100) NOT null +); +DESC book; +#尝试添加部分模拟数据,参考示例如下: +#```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 | +#+----+---------------+------------+-------+-------+-------+----------------------------+ +INSERT INTO book VALUES(1,'解忧杂货店','东野吾圭',27.20,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); +SELECT*FROM book; + +#3、创建用户表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE users( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +usersname VARCHAR(100) UNIQUE KEY NOT NULL, +`password` VARCHAR(100) NOT null, +email VARCHAR(100) NULL +); +DESC users; +#尝试添加部分模拟数据,参考示例如下: +#+----+----------+----------------------------------+--------------------+ +#| id | username | password | email | +#+----+----------+----------------------------------+--------------------+ +#| 1 | admin | 112233 | admin@mxdx.com | +#+----+----------+----------------------------------+--------------------+ +INSERT INTO users VALUES(1,'admin',112233,'admin@mxdx.com'); +SELECT*FROM users; + +#4、创建订单表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 | | +#+--------------+--------------+------+-----+---------+-------+ +CREATE TABLE orders( +id VARCHAR(100) PRIMARY KEY NOT NULL, +order_time DATETIME NOT NULL, +total_count INT(11) NOT NULL, +total_amount DOUBLE(11,2) NOT NULL, +state INT(11) NOT NULL, +user_id INT(11) not null +); +DESC orders; +#尝试添加部分模拟数据,参考示例如下: +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| id | order_time | total_count | total_amount | state | user_id | +#+----------------+---------------------+-------------+--------------+-------+---------+ +#| 15294258455691 | 2018-06-20 00:30:45 | 2 | 50.20 | 0 | 1 | +#+----------------+---------------------+-------------+--------------+-------+----- +INSERT INTO orders VALUES(152942258455691,'2018-06-20 00:30:45',2,50.20,0,1); +SELECT*FROM orders; + +#5、创建订单明细表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 | | +#+----------+--------------+------+-----+---------+----------------+ +CREATE TABLE order_items( +id INT(11) UNSIGNED auto_increment PRIMARY key NOT null, +count INT(11) 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 +); +DESC 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 | +#+----+-------+--------+------------+----------+-------+------------+----------------+ +INSERT INTO order_items VALUES(1,1,27.20,'解忧杂货店','东野圭吾',27.20,'static/img/default.jpg',15294258455691),(2,1,23.00,'边城','沈从文',23.00,'static/img/default.jpg',15294258455691); +SELECT*FROM order_items; +``` + diff --git "a/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" "b/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" new file mode 100644 index 0000000..a72df23 --- /dev/null +++ "b/17 \350\221\243\345\272\206\345\274\272/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.md" @@ -0,0 +1,3 @@ +``` + +``` -- Gitee