diff --git "a/49 \345\272\204\347\217\212\347\217\212/2023.02.21.md" "b/49 \345\272\204\347\217\212\347\217\212/2023.02.21.md" new file mode 100644 index 0000000000000000000000000000000000000000..7ab86e0cb21a251a0f6e56d6af695b3b575190ee --- /dev/null +++ "b/49 \345\272\204\347\217\212\347\217\212/2023.02.21.md" @@ -0,0 +1,333 @@ +## 第1题 + +```sql +-- 1、创建数据库test01_company +create database test01_company; +use test01_company; +2、创建表格offices +create table offices( + officeCode int, + city varchar(30), + address varchar(50), + country varchar(50), + postalCode varchar(25) +); +/* + +| 字段名 | 数据类型 | +| ---------- | ----------- | +| officeCode | int | +| city | varchar(30) | +| address | varchar(50) | +| country | varchar(50) | +| postalCode | varchar(25) | + +*/ + +-- 3、创建表格employees +create table employees( + empNum int(11), + lastName varchar(50), + firstName varchar(50), + mobile varchar(25), + code int, + jobTitle varchar(50), + birth date, + Note varchar(255), + sex varchar(5) +); +/* + +| 字段名 | 数据类型 | +| --------- | ------------- | +| empNum | int(11) | +| lastName | varchar(50) | +| firstName | varchar(50) | +| mobile | varchar(25) | +| code | int | +| jobTitle | varchar(50) | +| birth | date | +| Note | varchar(255) | +| Sex | varchar(5) | + +*/ +-- **要求4:**将表employees的mobile字段修改到code字段后面。 +alter table employees modify mobile varchar(25) after code; +-- **要求5:**将表employees的birth字段改名为birthday; +alter table employees change birth birthady date; +-- **要求6:**修改sex字段,数据类型为char(1)。 +alter table employees modify column Sex char(1); +-- **要求7:**删除字段note; +alter table employees drop column Note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +-- **要求9:**将表employees的名称修改为 employees_info +alter table employees rename employees_info; + + + + +-- 1、创建数据库test02db +create database test02db; +use test02db; +2、创建表格pet +create table pet( + name varchar(20) comment'宠物名称', + owner varchar(20) comment'宠物主人', + species varchar(20) comment'种类', + sex char(1)comment'性别', + birth year comment'出生日期', + death year comment'死亡日期' +);/* + +| 字段名 | 字段说明 | 数据类型 | +| ------- | -------- | ----------- | +| name | 宠物名称 | varchar(20) | +| owner | 宠物主人 | varchar(20) | +| species | 种类 | varchar(20) | +| sex | 性别 | char(1) | +| birth | 出生日期 | year | +| death | 死亡日期 | year | + +*/ + +-- 3、添加记录 +insert into pet values ('Fluffy','harold','Cat','f',2003,2010); +insert into pet (name, owner, species, sex, birth)values ('Claws','gwen','Cat','m',2004 ) ; +insert into pet (name, species, sex, birth) values ('Buffy','Dog','f',2009); +insert into pet(name, owner, species, sex, birth)values('Fang','benny','Dog','m',2000); +insert into pet(name, owner, species, sex, birth, death) values('bowser','diane','Dog','m',2003,2009); +insert into pet(name, species, sex, birth)values('Chirpy','Bird','f',2008); + +| name | owner | species | sex | birth | death | +| ------ | ------ | ------- | ---- | ----- | ----- | +| Fluffy | harold | Cat | f | 2003 | 2010 | +| Claws | gwen | Cat | m | 2004 | | +| Buffy | | Dog | f | 2009 | | +| Fang | benny | Dog | m | 2000 | | +| bowser | diane | Dog | m | 2003 | 2009 | +| Chirpy | | Bird | f | 2008 | | + +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth date; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set name = 'kevin' where name='Claws'; +/*6、 查询没有主人的宠物的名字*/ +select'name'from pet where name ='owner'=null; +-- 7、没有死的狗的主人改为duck +update pet set owner ='duck' where species = 'dog' and death = null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,owner,death from pet where death is not null and owner ='cat'; +9、 删除已经死亡的狗 +delete from pet where death is not null; +10、查询所有宠物信息 +select * from pet; + + +``` + +## 第2题 + +```sql +-- 1、创建数据库test02db +create database test02db; +use test02db; +2、创建表格pet +create table pet( + name varchar(20) comment'宠物名称', + owner varchar(20) comment'宠物主人', + species varchar(20) comment'种类', + sex char(1)comment'性别', + birth year comment'出生日期', + death year comment'死亡日期' +);/* +| 字段名 | 字段说明 | 数据类型 | +| ------- | -------- | ----------- | +| name | 宠物名称 | varchar(20) | +| owner | 宠物主人 | varchar(20) | +| species | 种类 | varchar(20) | +| sex | 性别 | char(1) | +| birth | 出生日期 | year | +| death | 死亡日期 | year | +*/ + +-- 3、添加记录 +insert into pet values ('Fluffy','harold','Cat','f',2003,2010); +insert into pet (name, owner, species, sex, birth)values ('Claws','gwen','Cat','m',2004 ) ; +insert into pet (name, species, sex, birth) values ('Buffy','Dog','f',2009); +insert into pet(name, owner, species, sex, birth)values('Fang','benny','Dog','m',2000); +insert into pet(name, owner, species, sex, birth, death) values('bowser','diane','Dog','m',2003,2009); +insert into pet(name, species, sex, birth)values('Chirpy','Bird','f',2008); +| name | owner | species | sex | birth | death | +| ------ | ------ | ------- | ---- | ----- | ----- | +| Fluffy | harold | Cat | f | 2003 | 2010 | +| Claws | gwen | Cat | m | 2004 | | +| Buffy | | Dog | f | 2009 | | +| Fang | benny | Dog | m | 2000 | | +| bowser | diane | Dog | m | 2003 | 2009 | +| Chirpy | | Bird | f | 2008 | | +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth date; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set name = 'kevin' where name='Claws'; +/*6、 查询没有主人的宠物的名字*/ +select'name'from pet where name ='owner'=null; +-- 7、没有死的狗的主人改为duck +update pet set owner ='duck' where species = 'dog' and death = null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,owner,death from pet where death is not null and owner ='cat'; +9、 删除已经死亡的狗 +delete from pet where death is not null; +10、查询所有宠物信息 +select * from pet; + +``` + +## 第3题 + +```sql +-- 1、创建数据库:test03_company +create database test03_company; +​```sql +create database test03_company charset utf8; +​``` +/* +2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +​```mysql*/ +use test03_company ; +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) +); +/*B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 + +* ​ 雇员编号为主键; +* ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); + * ​ 性别默认为男; + + ```mysql*/ +create table employee ( + empid int primary key auto_increment) + alter table employee add name varchar(10) not null; +alter table employee add sex char(2) not null ; +alter table employee modify column sex char(3) not null default 'man'; + alter table employee add title varchar(10); + alter table employee add birthday date; +alter table employee add depid int; +alter table employee add constraint fk_emp_dapt_id foreign key (depid) references employee(empid)on update cascade on delete set null; + +-- C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +create table salary( + empid int, + basesalary int, + titilesalary int, + deduction int +); +alter table salary add constraint primary key (empid) ; + -- 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +alter table salary add constraint salary_money foreign key(empid) references employee(empid) on update cascade on delete cascade; +/* 4、添加数据如下: + + 部门表: + + | 部门编号 | 部门名称 | 部门简介 | + | -------- | -------- | ------------ | + | 111 | 生产部 | Null | + | 222 | 销售部 | Null | + | 333 | 人事部 | 人力资源管理 | */ + insert into department (depid, depname) values ('111','生产部'),('222','销售部'); +insert into department values (333,'人事部','人力资源管理'); +/* + 雇员表: + + | 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | + | -------- | ---- | ---- | ---------- | ---------- | ------------ | + | 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); +/* + 工资表: + + | 雇员编号 | 基本工资 | 职务工资 | 扣除 | + | -------- | -------- | -------- | ---- | + | 1001 | 2200 | 1100 | 200 | + | 1002 | 1200 | 200 | NULL | + | 1003 | 2900 | 700 | 200 | + | 1004 | 1950 | 700 | 150 |*/ +insert into salary values (1001,2200,1100,200); +insert into salary(empid, basesalary, titilesalary) values (1002,1200,200); +insert into salary values (1003,2900,700,200),(1004,1950,700,150); +``` + +## 第4题 + +```sql + + 1、创建一个数据库:test04_school +create database test04_school; +use test04_school; + 2、创建如下表格 +create table Department( + DepNo int(10) comment '部号名' not null primary key , + DepName varchar(20) comment '部门名称', + DepNote varchar(50) comment '部门备注' +); + 表1 Department表的定义 + + | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | + | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | + | DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | + | DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | + | DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | + + 表2 Teacher表的定义 +create table Teacher( + Number int comment '教工号' not null primary key, + Name varchar(30) comment '姓名' not null, + Sex varchar(4) comment '性别', +Birth date comment '出生日期', +DepNo int comment '部门号' , +Salary float comment '工资', +Address varchar(100) comment '家庭住址', +); + | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | + | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | + | Number | 教工号 | int | 是 | 否 | 是 | 是 | + | Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | + | Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | + | Birth | 出生日期 | date | 否 | 否 | 否 | 否 | + | DepNo | 部门号 | int | 否 | 是 | 否 | 否 | + | Salary | 工资 | float | 否 | 否 | 否 | 否 | + | Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | + + 3、添加记录 +insert into Department values (601,'软件技术系','软件技术等专业'),(602,'网络技术系','多媒体技术等专业'), + (603,'艺术设计系','广告艺术设计等专业'),(604,'管理工程系','连锁经营管理等专业'); + | **DepNo** | **DepName** | **DepNote** | + | --------- | ----------- | ------------------ | + | 601 | 软件技术系 | 软件技术等专业 | + | 602 | 网络技术系 | 多媒体技术等专业 | + | 603 | 艺术设计系 | 广告艺术设计等专业 | + | 604 | 管理工程系 | 连锁经营管理等专业 | +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,'重庆市南岸区'); + | **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 | 重庆市南岸区 | + + 4、用SELECT语句查询Teacher表的所有记录。 + select *from Teacher; +``` + diff --git "a/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" "b/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" new file mode 100644 index 0000000000000000000000000000000000000000..b6a0332d1c117b69db1faa2b025846c7beebc8e2 --- /dev/null +++ "b/49 \345\272\204\347\217\212\347\217\212/20230219 mysql.md" @@ -0,0 +1,394 @@ +# 作业 + +## 练习1 + +```mysql +-- 1、创建数据库test01_market +create database test01_market; + +-- 2、创建表格customers +create table customers( + c_num int, + c_name varchar(50), + c_contact varchar(50), + c_city varchar(50), + c_birth date +); +alter table customers modify c_num int(11); +desc 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 column 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 c_gender char(1) after c_name; + +-- **要求7:**将表名改为customers_info +alter table customers rename customers_info; + +-- **要求8:**删除字段c_city +alter table customers_info drop column c_city; +``` + +## 练习2 + +```mysql +-- 1、创建数据库test02_library +create database test02_library; + +-- 2、创建表格books +create table books( + b_id int(10), + authors varchar(100), + price float, + pubdate year, + note varchar(100), + num int(11) +); +alter table books add b_name varchar(50) after b_id; +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; +delete from books where b_id = 1; +2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura','35','1993','joke','22'); +3)同时插入多条记录(剩下的所有记录) +insert into books values (3,'Story of Jane','ane 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') + +/*| 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; +``` + +## 练习3 + +```mysql +-- 1、创建数据库test01_market +create database test01_market; + +-- 2、创建表格customers +create table customers( + c_num int, + c_name varchar(50), + c_contact varchar(50), + c_city varchar(50), + c_birth date +); +alter table customers modify c_num int(11); +desc 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 column 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 c_gender char(1) after c_name; + +-- **要求7:**将表名改为customers_info +alter table customers rename customers_info; + +-- **要求8:**删除字段c_city +alter table customers_info drop column c_city; + + +-- 1、创建数据库test02_library +create database test02_library; + +-- 2、创建表格books +create table books( + b_id int(10), + authors varchar(100), + price float, + pubdate year, + note varchar(100), + num int(11) +); +alter table books add b_name varchar(50) after b_id; +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; +delete from books where b_id = 1; +2)不指定字段名称插入第二记录 +insert into books values (2,'EmmaT','Jane lura','35','1993','joke','22'); +3)同时插入多条记录(剩下的所有记录) +insert into books values (3,'Story of Jane','ane 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') + +/*| 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; + + + + + + + + +-- 1、创建数据库test03_bookstore +create database test03_bookstore; +desc book; +-- 2、创建book表 +create table book( + id int(11), + title varchar(100), + author varchar(100), + price double(11,2), + sales int(11), + stock int(11), + img_path varchar(100) + ); + +alter table book add primary key(id); +alter table book modify title varchar (100) not null; +alter table book modify author varchar(100) not null; +alter table book modify price double(11,2) not null; +alter table book modify sales int(11) not null; +alter table book modify stock int(11) not null; +alter table book modify img_path varchar(100) not null; +/* ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| sales | int(11) | NO | | NULL | | +| stock | int(11) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +​``` +*/ +/*尝试添加部分模拟数据,参考示例如下: + +​```mysql ++----+-------------+------------+-------+-------+-------+----------------------------+ +| id | title | author | price | sales | stock | img_path | ++----+-------------+------------+-------+-------+-------+-----------------------------+ +| 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | +| 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | ++----+---------------+------------+-------+-------+-------+----------------------------+ +​```*/ +select * from book; +use class3; +insert into book values(1,'解忧杂货店','东野圭吾','27.20','102','98','upload/books/解忧杂货店.jpg'); +-- 3、创建用户表users,并插入数据 +create table users( + id int(11), + username varchar(100), + password varchar(100), + email varchar(100) +); +/* +​```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| username | varchar(100) | NO | UNI | NULL | | +| password | varchar(100) | NO | | NULL | | +| email | varchar(100) | YES | | NULL | | ++----------+--------------+------+-----+---------+----------------+ +​``` +*/ +alter table users add primary key (id); +select * from users; +desc users; +alter table users modify password varchar(100) not null; +alter table users modify email varchar(100) null; +alter table users add unique key(username); +alter table users drop primary key where = username; +alter table users modify id int(11) not null auto_increment; +/* +--尝试添加部分模拟数据,参考示例如下: + +​```mysql ++----+----------+----------------------------------+--------------------+ +| id | username | password | email | ++----+----------+----------------------------------+--------------------+ +| 1 | admin | 112233 | admin@mxdx.com | ++----+----------+----------------------------------+--------------------+ +​``` +*/ +create table test ( + id int, + username varchar(11), + password varchar(11), + email varchar(11) + ); +alter table test modify column email varchar(20); +insert into test values(1,'admin',112233,'admin@xdx.com'); +select * from test; +/* +4、创建订单表orders + +​```mysql ++--------------+--------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++--------------+--------------+------+-----+---------+-------+ +| id | varchar(100) | NO | PRI | NULL | | +| order_time | datetime | NO | | NULL | | +| total_count | int(11) | NO | | NULL | | +| total_amount | double(11,2) | NO | | NULL | | +| state | int(11) | NO | | NULL | | +| user_id | int(11) | NO | MUL | NULL | | ++--------------+--------------+------+-----+---------+-------+ +​```*/ +create table orders( + id varchar(100) not null , + order_time datetime not null, + total_ciunt int(11) , + total_amount double(11,2) not null , + state int(11) not null , + user_id int(11) not null +); + alter table orders add primary key(id); +alter table orders add key(user_id); +desc orders; +alter table orders modify column total_ciunt int(11) not null ; +/* +尝试添加部分模拟数据,参考示例如下: + +​```mysql ++----------------+---------------------+-------------+--------------+-------+---------+ +| 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(id, order_time, total_ciunt, total_amount, state, user_id) + values (15294258455691,'2018-06-20 00:30:45 ',2,50.20,0,1); +select * from orders; +-- 5、创建订单明细表order_items +create table ordedr_items ( + id int(11) not null , + count int(11) not null, + amout 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 +); +alter table ordedr_items add primary key (id); +alter table ordedr_items add key (order_id); +desc ordedr_items; +alter table ordedr_items modify id int(11) not null auto_increment; + /*```mysql ++----------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| count | int(11) | NO | | NULL | | +| amount | double(11,2) | NO | | NULL | | +| title | varchar(100) | NO | | NULL | | +| author | varchar(100) | NO | | NULL | | +| price | double(11,2) | NO | | NULL | | +| img_path | varchar(100) | NO | | NULL | | +| order_id | varchar(100) | NO | MUL | NULL | | ++----------+--------------+------+-----+---------+----------------+ +​``` + +尝试添加部分模拟数据,参考示例如下:*/ +insert into ordedr_items values (1,1,27.20,'解忧杂货店','东野圭吾','27.20','static/img/default.jpg','15294258455691 '); + (2,1,23.00,'边城','沈从文',23.00.'static/img/default.jpg','15294258455691'); +/* +​```mysql ++----+-------+--------+---------+---------+-------+----------------+----------------+ +| id |count| amount| title | author | price | img_path | order_id | ++----+-------+--------+------------+----------+-------+----------------+----------------+ +| 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | +| 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | ++----+-------+--------+------------+----------+-------+------------+----------------+ +​``` +``` +