From 1a55d6c5f3951edb21d13fb8e6a793c00722953b Mon Sep 17 00:00:00 2001 From: 15860771339 <2816279374@qq.com> Date: Sun, 19 Feb 2023 13:38:55 +0800 Subject: [PATCH] =?UTF-8?q?mysql=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E5=92=8CDDL,DML=E8=AF=AD=E5=8F=A5=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\254\241\344\275\234\344\270\232.md" | 17 + ...55\345\217\245\347\273\203\344\271\240.md" | 381 ++++++++++++++++++ 2 files changed, 398 insertions(+) create mode 100644 "08 \351\251\254\345\255\220\350\266\212/20230214 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "08 \351\251\254\345\255\220\350\266\212/20230218 DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" diff --git "a/08 \351\251\254\345\255\220\350\266\212/20230214 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/08 \351\251\254\345\255\220\350\266\212/20230214 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b24e6db --- /dev/null +++ "b/08 \351\251\254\345\255\220\350\266\212/20230214 MySQL\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,17 @@ +# 作业 + +```sql +CREATE TABLE student( +`学号` VARCHAR(20), +`姓名` VARCHAR(20), +`性别` VARCHAR(20), +`爱好` VARCHAR(20), +`住址` VARCHAR(20), +`联系方式` VARCHAR(20), +`邮箱` VARCHAR(20), +`QQ号` VARCHAR(20) +); + +INSERT INTO student VALUES('2244310408','马子越','男','听音乐,看小说,玩游戏','福建厦门','15860771339','2816279374@qq.com','2816279374'); +``` + diff --git "a/08 \351\251\254\345\255\220\350\266\212/20230218 DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" "b/08 \351\251\254\345\255\220\350\266\212/20230218 DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" new file mode 100644 index 0000000..e5fbfc3 --- /dev/null +++ "b/08 \351\251\254\345\255\220\350\266\212/20230218 DDL\345\222\214DML\350\257\255\345\217\245\347\273\203\344\271\240.md" @@ -0,0 +1,381 @@ +# 1.笔记 + +## DDL语句 + +## (1)和数据库相关 + +### 创建数据库 + +```sql +create database 数据库名 ; +create database 数据库名 charset 字符集; +``` + +### 查看所有的数据库 + +```sql +show databases; #有一个s,代表多个数据库 +``` + +### 查看某个数据库的详细定义语句 + +```sql +show create database 数据库名; +``` + +```sql +show create database 数据库名\G +``` + +### 修改数据库编码 + +```sql +#修改数据库字符集和校对规则 +ALTER DATABASE 数据库名称 CHARACTER SET 字符集名称 COLLATE 字符集对应校对规则; +``` + +```sql +ALTER DATABASE mxdx_chapter3_two CHARACTER SET utf8 COLLATE utf8_general_ci; +``` + +==**注意,**==修改数据库编码只会影响之后创建的表的默认编码,之前创建的表不会受影响。 + +### 删除数据库 + +```sql +drop database 数据库名; +``` + +### 使用数据库 + +```sql +use 数据库名; +``` + +## (2)和数据表相关 + +### 查看某个数据库的所有表格 + +```sql +use 数据库名; +show tables; +``` + +```sql +show tables from 数据库名; +``` + +### 创建表格 + +```sql +create table 数据表名称( + 字段名 数据类型, + 字段名 数据类型 + ... +); +``` + +### 查看表的详细定义信息,即查看建表语句 + +```sql +show create table 表名称; +show create table 表名称\G +``` + +### 修改数据表编码 + +```sql +alter table 表名称 convert to character set utf8; +``` + +### 查看表结构 + +```sql +desc 表名称; +``` + +### 删除表格,包括表结构和里面的数据 + +```sql +drop table 表名称; +``` + +### 修改表结构:删除字段 + +```sql +alter table 表名称 drop 【column】 字段名称; +``` + +### 修改表结构:增加字段 + +```sql +alter table 表名称 add 【column】 字段名称 数据类型; +alter table 表名称 add 【column】 字段名称 数据类型 first; +alter table 表名称 add 【column】 字段名称 数据类型 after 另一个字段; +``` + +### 修改表结构:修改字段的数据类型 + +```sql +alter table 表名称 modify 【column】 字段名称 新的数据类型; +``` + +### 修改表结构:修改字段的名称 + +```sql +alter table 表名称 change 【column】 旧字段名称 新的字段名称 新的数据类型; +``` + +### 修改表结构:修改字段位置 + +```sql +alter table 表名称 modify 【column】 字段名称 数据类型 first; +alter table 表名称 modify 【column】 字段名称 数据类型 after 另一个字段; +``` + +### 修改表名称(重命名表) + +```sql +alter table 旧表名 rename 【to】 新表名; +rename table 旧表名称 to 新表名称; +``` + +## DML语句 + +## 添加语句 + +### (1)添加一条记录到某个表中 + +```sql +insert into 表名称 values(值列表); #值列表中的值的顺序、类型、个数必须与表结构一一对应 +``` + +```sql +mysql> desc teacher; ++----------+------------------------+------+-----+---------+-------+ +| Field | Type | Null | Key | Default | Extra | ++----------+------------------------+------+-----+---------+-------+ +| tid | int(11) | YES | | NULL | | +| tname | varchar(5) | YES | | NULL | | +| salary | double | YES | | NULL | | +| weight | double | YES | | NULL | | +| birthday | date | YES | | NULL | | +| gender | enum('男','女') | YES | | NULL | | +| blood | enum('A','B','AB','O') | YES | | NULL | | +| phone | char(11) | YES | | NULL | | ++----------+------------------------+------+-----+---------+-------+ +8 rows in set (0.00 sec) +``` + +```sql +insert into teacher values(1,'张三',15000,120.5,'1990-5-1','男','O','13789586859'); +``` + +```sql +insert into teacher values(2,'李四',15000,'1990-5-1','男','O','13789586859'); #缺体重weight的值 + +ERROR 1136 (21S01): Column(列) count(数量) doesn't match(不匹配) value(值) count(数量) at row 1 +``` + +### (2)添加一条记录到某个表中 + +```sql +insert into 表名称 (字段列表) values(值列表); #值列表中的值的顺序、类型、个数必须与(字段列表)一一对应 +``` + +### (3)添加多条记录到某个表中 + +```sql +insert into 表名称 values(值列表),(值列表),(值列表); #值列表中的值的顺序、类型、个数必须与表结构一一对应 +``` + +```sql +insert into 表名称 (字段列表) values(值列表),(值列表),(值列表); #值列表中的值的顺序、类型、个数必须与(字段列表)一一对应 +``` + +## 修改语句 + +### 修改所有行 + +```sql +update 表名称 set 字段名 = 值, 字段名 = 值; #给所有行修改 +``` + +```sql +#修改所有人的薪资,都涨了1000 +update teacher set salary = salary + 1000 ; +``` + +### 修改部分行 + +```sql +update 表名称 set 字段名 = 值, 字段名 = 值 where 条件; #给满足条件的行修改 +``` + +## 删除 + +### 删除部分行的数据 + +```sql +delete from 表名称 where 条件; +``` + +```sql +delete from teacher where tname = '天琪'; +``` + +### 删除整张表的数据,但表结构留下 + +```sql +delete from 表名称; +``` + +```sql +delete from teacher; +``` + +### 截断表,清空表中的数据,只留有表结构 + +```sql +truncate 表名称; +``` + +```sql +truncate teacher; +``` + +truncate表和delete表的区别: + +delete是一条一条删除记录的。如果在事务中,事务提交之前支持回滚。(后面会讲事务) + +truncate是把整个表drop,新建一张,效率更高。就算在事务中,也无法回滚。 + +# 2.作业 + +## 第一题 + +```sql +CREATE TABLE customers( +c_num int(11), +c_name VARCHAR(50), +c_contact VARCHAR(50), +c_city VARCHAR(50), +c_birth date +); + +DESC customers; + +ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth; +desc customers; + +ALTER TABLE customers MODIFY c_name VARCHAR(70); +desc customers; + +ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50); +DESC customers; + +ALTER TABLE customers ADD c_gender char(1) after c_name; +DESC customers; + +ALTER TABLE customers rename customers_info; + +ALTER TABLE customers_info drop c_city; + +DESC customers_info; + +``` + +## 第二题 + +```sql +CREATE TABLE books( +b_id int(11) PRIMARY KEY 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; + +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; + +INSERT INTO books VALUES(2,'EmmaT','Jane lura',35,1993,'joke',22); +SELECT * FROM books; + +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 Blade',30,'2010','law',0),(6,'The Battle','Upton Sara',30,'1999','medicine',40),(7,'Rose Hood','Richard haggard',28,'2008','cartoon',28); +SELECT * FROM books; + +UPDATE books set price=price+5 where note='novel'; +SELECT * FROM books; + +UPDATE books set price=40 where b_name='EmmaT'; +SELECT * FROM books; + +DELETE FROM books where num=0; +SELECT * FROM books; +``` + +## 第三题 + +```sql +CREATE TABLE books( +id int(11) PRIMARY KEY not null auto_increment, +title VARCHAR(100) not null, +author 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 books; + +INSERT INTO books VALUES(1,'解忧杂货店','东野圭吾',27.2,102,98,'upload/books/解忧杂货店.jpg'),(2,'边城','沈从文',23,102,98,'upload/boosk/边城.jpg'); +SELECT * FROM books; + +CREATE TABLE users( +id int(11) PRIMARY KEY not null auto_increment, +username VARCHAR(100) UNIQUE KEY not null, +PASSWORD VARCHAR(100) not null, +email VARCHAR(100) +); +desc users; + +INSERT INTO users VALUES(1,'admin','112233','admin@mxdx.com'); +SELECT * FROM users; + +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, +CONSTRAINT orders_suers_id_fk FOREIGN KEY (user_id) REFERENCES users(id) +); +desc orders; + +INSERT INTO orders VALUES('15294258455691','2018-06-20 00:30:45',2,50.2,0,1); +SELECT * FROM orders; + +CREATE TABLE order_items( +id int(11) PRIMARY KEY not null auto_increment, +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, +CONSTRAINT orders_items_orders_id_fk FOREIGN KEY (order_id) REFERENCES orders(id) on DELETE CASCADE +); +desc order_items; + +INSERT INTO order_items VALUES(1,1,27.2,'解忧杂货店','东野圭吾',27.2,'static/img/default.jpg','15294258455691'),(2,1,23,'边城','沈从文',23,'static/img/default.jpg','15294258455691'); +SELECT * FROM order_items; +``` + -- Gitee