数据库大讲解第十二讲
基本命令使用 作者:董哥
前期比较乱主要是为了熟练有一定基础的,只需跟着文档来就可以,不懂的地方欢迎留言
创建数据库,及各数据库表
主要采用的是Mysql数据库,其中会有提到其他数据库:
- 前期·准备
前期准备创建数据库employe并创建多个表
前期是随便练习后边知识由浅入深
前边几页是练习代码所需要的各种表
里面有中文符号最好自己动手敲
创建数据库
Host: 28.5.8.60 (Version 5.7.17)
Date: 2019-09-2
Writer: 煮不烂的鸡蛋 (Build 1.26)
mysql> select e.emp_id,e.last_name,p.position
-> from employe_tbl e,
-> employe_pay_tbl p;
±----------±----------±--------------+
| emp_id | last_name | position |
±----------±----------±--------------+
| 213764555 | glass | sales manager |
| 220984332 | wallace | sales manager |
| 311549902 | stephens | sales manager |
| 313782439 | glass | sales manager |
| 442346889 | plew | sales manager |
| 443679021 | spurgeon | sales manager |
| 213764555 | glass | shipper |
| 220984332 | wallace | shipper |
| 311549902 | stephens | shipper |
| 313782439 | glass | shipper |
| 442346889 | plew | shipper |
| 443679021 | spurgeon | shipper |
| 213764555 | glass | marketing |
| 220984332 | wallace | marketing |
| 311549902 | stephens | marketing |
| 313782439 | glass | marketing |
| 442346889 | plew | marketing |
| 443679021 | spurgeon | marketing |
| 213764555 | glass | salesman |
| 220984332 | wallace | salesman |
| 311549902 | stephens | salesman |
| 313782439 | glass | salesman |
| 442346889 | plew | salesman |
| 443679021 | spurgeon | salesman |
| 213764555 | glass | team leader |
| 220984332 | wallace | team leader |
| 311549902 | stephens | team leader |
| 313782439 | glass | team leader |
| 442346889 | plew | team leader |
| 443679021 | spurgeon | team leader |
| 213764555 | glass | shipper |
| 220984332 | wallace | shipper |
| 311549902 | stephens | shipper |
| 313782439 | glass | shipper |
| 442346889 | plew | shipper |
| 443679021 | spurgeon | shipper |
±---- -----±- --------±--------------+
36 rows in set (0.00 sec)
CREATE TABLE customer_tbl
(
cust_id
varchar(10) NOT NULL DEFAULT ‘’,
cust_name
varchar(30) NOT NULL DEFAULT ‘’,
cust_address
varchar(20) NOT NULL DEFAULT ‘’,
cust_city
varchar(15) NOT NULL DEFAULT ‘’,
cust_state
char(2) NOT NULL DEFAULT ‘’,
cust_zip
bigint(20) DEFAULT NULL,
cust_phone
char(11) DEFAULT NULL,
cust_fax
varchar(255) DEFAULT NULL,
PRIMARY KEY (cust_id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
子查询必须遵守的规则:
子查询必须位于圆括号内
除非主查询里有多个字段让子查询进行比较,否则子查询里的select字句里只有一个字段
子查询不能使用order by字句,在子查询里面我们可以利用group by子句实现order by
返回多条记录的子查询只能用多值操作符比如in 配合使用
Select 列表里不能引用任何blob array clob nclob类型的值
子查询不能被包含在函数里
操作符bettween 不能用于子查询,但子查询内可以使用
mysql> select a.emp_id,a.last_name,a.first_name,b.pay_rate
-> from employe_tbl a,employe_pay_tbl b
-> where a.emp_id=b.emp_id
-> and b.pay_rate<(select pay_rate from employe_pay_tbl
-> where emp_id=‘443679021’);
±----------±----------±-----------±---------+
| emp_id | last_name | first_name | pay_rate |
±----------±----------±-----------±---------+
| 220984332 | wallace | marian | 11.00 |
| 442346889 | plew | tina | 14.75 |
±----------±----------±-----------±---------+
2 rows in set (0.00 sec)
Mysql: select pay_rate
from employe_pay_tbl b
where emp_id=‘220984332’;
±---------+
| pay_rate |
±---------+
| 11.00 |
±---------+
1 row in set (0.00 sec)
mysql> select a.emp_id,a.last_name,a.first_name,b.pay_rate
-> from employe_tbl a,employe_pay_tbl b
-> where a.emp_id=b.emp_id
-> and b.pay_rate>(select pay_rate from employe_pay_tbl
-> where emp_id=‘220984332’);
±----------±----------±-----------±---------+
| emp_id | last_name | first_name | pay_rate |
±----------±----------±-----------±---------+
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
±----------±----------±-----------±---------+
2 rows in set (0.00 sec)
mysql> insert into rich_employees
-> select a.emp_id,a.last_name,a.first_name,b.pay_rate
-> from employe_tbl a,employe_pay_tbl b
-> where a.emp_id=b.emp_id
-> and b.pay_rate>(select pay_rate from employe_pay_tbl
-> where emp_id=‘220984332’);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select *from rich_employees;
±----------±----------±-----------±---------+
| emp_id | last_time | first_name | pay_rate |
±----------±----------±-----------±---------+
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
| 442346889 | plew | tina | 14.75 |
| 443679021 | spurgeon | tiffany | 15.00 |
±----------±----------±-----------±---------+
10 rows in set (0.00 sec)
mysql>
mysql>select emp_id from employe_tbl
->where city=‘indianapolis’;
±----------+
| emp_id |
±----------+
| 220984332 |
| 313782439 |
| 442346889 |
| 443679021 |
±----------+
4 rows in set (0.00 sec)
mysql> update employe_pay_tbl
-> set pay_rate=pay_rate *1.1
-> where emp_id in (select emp_id from employe_tbl
-> where city=‘indianapolis’);
Query OK, 3 rows affected, 1 warning (0.05 sec)
Rows matched: 4 Changed: 3 Warnings: 1
mysql> select * from employe_pay_tbl;
±----------±--------------±-----------±---------±----------------±---------±--------+
| emp_id | position | date_hire | pay_rate | date_last_raise | salary | bonus |
±----------±--------------±-----------±---------±----------------±---------±--------+
| 213764555 | sales manager | 2004-08-14 | NULL | 2009-08-01 | 30000.00 | 2000.00 |
| 220984332 | shipper | 2006-07-22 | 13.31 | 1999-07-01 | NULL | NULL |
| 311549902 | marketing | 1999-05-23 | NULL | 2009-05-01 | 40000.00 | NULL |
| 313782439 | salesman | 2007-06-28 | NULL | 1879-01-12 | 20000.00 | 1000.00 |
| 442346889 | team leader | 2000-06-17 | 17.85 | 2009-06-01 | NULL | NULL |
| 443679021 | shipper | 2001-01-14 | 18.15 | 1999-01-14 | NULL | NULL |
±----------±--------------±-----------±---------±----------------±---------±--------+
6 rows in set (0.00 sec)
mysql> select * from employe_tbl;
±----------±----------±-----------±------------±-----------------------±-------------±------±------±-----------±-----------+
| emp_id | last_name | first_name | middle_name | address | city | state | zip | phone | pager |
±----------±----------±-----------±------------±-----------------------±-------------±------±------±-----------±-----------+
| 213764555 | glass | brandon | scott | 1710 main st | whiteland | in | 47885 | 3178984432 | 3175709980 |
| 220984332 | wallace | marian | NULL | 7889 keystone ave | indianapolis | in | 46741 | 3178989266 | NULL |
| 311549902 | stephens | tina | dawn | rr 3 box 17 a | greenwood | in | 47890 | 3178784465 | NULL |
| 313782439 | glass | jacob | NULL | 3789 white river blvd | indianapolis | in | 45734 | 1235112311 | 8887345678 |
| 442346889 | plew | tina | carol | 3301 beacon | indianapolis | in | 46624 | 3178886665 | NULL |
| 443679021 | spurgeon | tiffany | NULL | 5 george court | indianapolis | in | 46234 | 3178552233 | NULL |
±----------±----------±-----------±------------±-----------------------±-------------±------±------±-----------±-----------+
6 rows in set (0.00 sec)
mysql> delete from employe_pay_tbl
-> where emp_id=(select emp_id from employe_tbl
-> where last_name='glass’and first_name=‘brandon’);
Query OK, 1 row affected (0.03 sec)
mysql> select*from employe_pay_tbl;
±----------±------------±-----------±---------±----------------±---------±--------+
| emp_id | position | date_hire | pay_rate | date_last_raise | salary | bonus |
±----------±------------±-----------±---------±----------------±---------±--------+
| 220984332 | shipper | 2006-07-22 | 13.31 | 1999-07-01 | NULL | NULL |
| 311549902 | marketing | 1999-05-23 | NULL | 2009-05-01 | 40000.00 | NULL |
| 313782439 | salesman | 2007-06-28 | NULL | 1879-01-12 | 20000.00 | 1000.00 |
| 442346889 | team leader | 2000-06-17 | 17.85 | 2009-06-01 | NULL | NULL |
| 443679021 | shipper | 2001-01-14 | 18.15 | 1999-01-14 | NULL | NULL |
±----------±------------±-----------±---------±----------------±---------±--------+
mysql> select cust_id,cust_name
-> from customer_tbl
-> where cust_id in (select o.cust_id
-> from orders_tbl o,products_tbl p
-> where o.prod_id=p.prod_id
-> and o.qty+p.cost <(select sum(cost)
-> from products_tbl));
±--------±----------+
| cust_id | cust_name |
±--------±----------+
| 090 | gyui |
| 109 | nanweg |
| 345 | sadf |
| 232 | sgah nka |
±--------±----------+
4 rows in set (0.00 sec)
mysql> select sum(cost) from products_tbl;
±----------+
| sum(cost) |
±----------+
| 64.69 |
±----------+
1 row in se