MySQL database - table operation -SQL statement (b)

1. MySQL multi-table query

1.1 foreign key constraints

Cartesian product in order to eliminate the phenomenon of multiple tables appear in the query, MySQL under construction between the table and the key to multi-table queries can use foreign key query.

Foreign keys : a column references from Table 1 (Sub) of (ref) other (main) the value of a column in Table 2, the column of Table 1 is referred to which the foreign key in Table 2 of this column.

1.2 foreign key is provided the use of

For example, the above easiest employees (employee) and the department table (department), foreign key dept_id associated with id.

Proceed as follows:

① set in the multi fk foreign key tables, foreign key association dept_id one primary key of the table id, and choosing the reference table;

 

② modify the option settings for the storage engine InnoDB, foreign key support operations;

 

Note: In MySQL, InnoDB supports transactions and foreign keys .MyISAM does not support transactions and foreign keys .

 

This operation may modify the SQL statement can be used as a storage engine InnDB:

ALTER TABLE 表名 ENGINE='InnoDB';

The command line interface input sentence as follows:

 

Also create a foreign key using the SQL statement:

ALTER TABLE employee ADD CONSTRAINT employee_fk(外键名) FOREIGN KEY (dept_id) REFERENCES  dept(dept_id);

 

1.3 Classification join query

Join query may be a general classification of FIG simply described, is divided into an outer join query ( left outer join, right outer join, full connection ), the connection query ( the connection, since the connection ).

 

Join Query 1.4

内连接两张表的情况如下图,连接查询得到的是两张表的交集部分。

 

SQL语句写法上可分为显式隐式写法:

隐式内连接写法:

select <selectList>
From table1,table2 where table1.列 = table2.列;

显式内连接写法(推荐写法):

select <selectList>
From table1 [inner] join table2 on table1.列 = table2.列;

查询实例:

说明:本例以下所有查询以product、product_category、product_stock三张表为例;

 

 

①需求:查询所有商品的名称和分类名称:

隐式写法:

SELECT p.product_name, pc.category_name

FROM product p, product_category pc

WHERE p.category_id = pc.id;

显式写法:

SELECT p.product_name, pc.category_name

FROM product p INNER JOIN product_category pc

ON p.category_id = pc.id;

 
SELECT p.product_name, pc.category_name

FROM product p JOIN product_category pc

ON p.category_id = pc.id;

②需求: 查询货品id,货品名称,货品所属分类名称;

隐式写法:

SELECT *

FROM product p, product_category pc

WHERE p.category_id = pc.id AND

p.sale_price > 200 AND pc.category_name = '无线鼠标';

 

显式写法:

SELECT *

FROM product p

JOIN product_category pc ON p.category_id = pc.id

AND p.sale_price > 200 AND pc.category_name = '无线鼠标';

1.5 外连接查询

外连接查询分为左外连接查询右外连接查询

左外连接:查询出JOIN左边表的全部数据与右表满足ON条件的部分,JOIN右边表不匹配的数据使用NULL来填充数据行。

右外连接:查询出JOIN右边表的全部数据与左表满足ON条件的部分,JOIN左边表不匹配的数据使用NULL来填充数据行。

 

语法格式:

select <selectList>

from table1 left/right [outer] join table2

on table1.列 = table2.列;

查询实例:

查询每种商品名称,分类的名称和包含的具体商品总数(storeNum)

 

SELECT p.product_name, pc.category_name, IFNULL(ps.store_num,0)

FROM product p

LEFT JOIN product_category pc

ON p.category_id = pc.id

LEFT JOIN product_stock ps

ON p.id = ps.product_id;

注:IFNULL(expr1,expr2)的使用是如果当前expr1为NULL,则显示expr2的值

 

1.6 自连接查询

在特定的查询场景下(商品分类、地区、权限),需要设计将表中的数据进行分类或二级关联时,可能会用到自连接查询的表设计方法。

自连接方式:

查询实例:

查询每个商品分类的名称和父分类名称

#隐式写法

SELECT pare.category_name, sub.category_name FROM

product_category sub, product_category pare

WHERE sub.id = pare.parent_id;

 

# 显示写法

SELECT pare.category_name, sub.category_name FROM

product_category sub JOIN product_category pare

ON sub.id = pare.parent_id;

查询结果:

 

1.7 子查询

子查询(嵌套查询):一个查询语句嵌套在另一个查询语句中,内层查询的结果可以作为外层查询条件。(相当于查询出来一个结果,然后把结果当着一张表在进行查询)

一般的,嵌套在WHERE或者FROM字句中。

 

子查询(嵌套查询)一般分为单行单列子查询和单行多列子查询。

 

查询实例:

① 单行单列子查询

# 单行单列子查询

# 查询零售价比罗技MX1100更高的所有商品信息

SELECT * FROM product WHERE

sale_price > ( SELECT sale_price FROM product WHERE product_name = '罗技MX1100' );

查询结果:

 

②单行多列子查询

# 查询分类编号和折扣与罗技M100相同的所有商品信息

SELECT * FROM product WHERE (category_id, cutoff) IN (SELECT category_id, cutoff FROM product WHERE product_name = '罗技MX1100');

 

SELECT * FROM product WHERE (category_id, cutoff) = (SELECT category_id, cutoff FROM product WHERE product_name = '罗技MX1100');

查询结果:

 

2.MySQL数据操作DML语句

2.1 插入语句(insert)

插入语句:一次只插入一行。

 

语法:

insert into table_name (column1,column2,column3...) values (value1, value2, value3…);

 

插入多行数据记录。

语法:

insert into table_name (column1,column2,column3...) values

(value1, value2, value3…), (value4, value5, value6…), (value7, value8, value9…);

实例:

# 插入一行

INSERT INTO mytable(id, name, age, sex) VALUES (1, '', 21, 0);

 

INSERT INTO mytable(sex, age, name, id) VALUES (1, 18, '小白', 2); #顺序可以打乱,只要插入的键值一一对应即可

 

# 插入多行-- MySQL特有
INSERT INTO mytable(id, name, age, sex) 
VALUES (3, 'test01', 26, 0), (4, 'test02', 27, 1), (5, 'test03', 28, 1);

 

2.2 修改语句(update)

修改语法:

update table_name set column1 = value1, column2 = value2, column3 = value3…

[where condition];

注意:如果省略了where语句,则是修改全表的数据。

 

修改实例:

# 修改数据
# 将零售价大于300的货品零售价上调0.2倍
UPDATE product SET sale_price = sale_price * 1.2 WHERE sale_price > 300;

# 将零售价大于300的有线鼠标的货品零售价上调0.1倍
UPDATE product p JOIN product_category pc ON p.category_id = pc.id 
SET sale_price = sale_price * 1.1 WHERE sale_price > 300 AND pc.category_name = '有线鼠标';

 

2.3 删除语句(delete)

语法:

delete from table_name [where condition];

 

注:如果省略了where,则会全表数据都进行删除

 

实例:

# 删除一条

DELETE FROM mytable WHERE id = 4;

# 删除多条

DELETE FROM mytable WHERE id >=3;

 

3. MySQL数据备份

MySQL数据备份有两种方式:通过Navicat工具的SQL导入/导出和使用命令行的方式导入/导出。这里主要说明使用命令行的方式。

 

语法:

导出:mysqldump -u账户 -p密码 数据库名称>脚本文件存储地

MySql自身的数据库维护

通过cmd命令进入dos窗口:

mysqldump -uroot -padmin jdbcdemo> C:/shop_bak.sql

导入:mysql -u账户 -p密码 数据库名称< 脚本文件存储地址

mysql -uroot -padmin jdbcdemo< C:/shop_bak.sql

 

Guess you like

Origin www.cnblogs.com/yif0118/p/11310979.html