MySQL命令与SQL语句 (6)

生产环境这个使用的多

#世界上小于100人的城市,在哪个省,属于哪个国家,人口数量是多少? select city.name,city.district,country.name,city.population -> from city join country -> on city.countrycode=country.code -> where city.population < 100; +-----------+----------+----------+------------+ | name | district | name | population | +-----------+----------+----------+------------+ | Adamstown | – | Pitcairn | 42 | +-----------+----------+----------+------------+ #世界上小于100人的城市,在哪个省,属于哪个国家,人口数量是多少,说的是什么语言? mysql> select city.name,city.district,country.name,city.population,countrylanguage.language -> from city join country -> on city.countrycode=country.code -> join countrylanguage -> on city.countrycode=countrylanguage.countrycode -> where city.population < 100; +-----------+----------+----------+------------+-------------+ | name | district | name | population | language | +-----------+----------+----------+------------+-------------+ | Adamstown | – | Pitcairn | 42 | Pitcairnese | +-----------+----------+----------+------------+-------------+

3.自连接

#世界上小于100人的城市说的什么语言? mysql> select city.name,countrylanguage.language,city.population -> from city natural join countrylanguage -> where city.population < 100; +-----------+-------------+------------+ | name | language | population | +-----------+-------------+------------+ | Adamstown | Pitcairnese | 42 | +-----------+-------------+------------+ #前提是两个表中必须有相同的列名字,并且数据一致

4.外连接(左外连接,右外连接)

#左外连接 mysql> select city.name,country.code,country.name from city left join country on city.countrycode=country.code and city.population<100 limit 10; +----------------+------+------+ | name | code | name | +----------------+------+------+ | Kabul | NULL | NULL | | Qandahar | NULL | NULL | | Herat | NULL | NULL | | Mazar-e-Sharif | NULL | NULL | | Amsterdam | NULL | NULL | | Rotterdam | NULL | NULL | | Haag | NULL | NULL | | Utrecht | NULL | NULL | | Eindhoven | NULL | NULL | | Tilburg | NULL | NULL | +----------------+------+------+ #右外连接 mysql> select city.name,country.code,country.name from city right join country on city.countrycode=country.code and city.population<100 limit 10; +------+------+----------------------+ | name | code | name | +------+------+----------------------+ | NULL | ABW | Aruba | | NULL | AFG | Afghanistan | | NULL | AGO | Angola | | NULL | AIA | Anguilla | | NULL | ALB | Albania | | NULL | AND | Andorra | | NULL | ANT | Netherlands Antilles | | NULL | ARE | United Arab Emirates | | NULL | ARG | Argentina | | NULL | ARM | Armenia | +------+------+----------------------+ 5.DTL (Data Transaction Language) 数据事务语言

commit 提交事务
rollback 回滚事务

[注意]只有DML语言才有事务性

ACID:指数据库事务正确执行的四个基本要素的缩写。 原子性(Atomicity): 原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。 一致性(Consistency): 事务前后数据的完整性必须保持一致。 隔离性(Isolation): 事务的隔离性是多个用户并发访问数据库时,数据库为每一个用户开启的事务,不能被其他事务的操作数据所干扰,多个并发事务之间要相互隔离。 持久性(Durability): 持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来即使数据库发生故障也不应该对其有任何影响

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwfzzx.html