1、mysql的用户管理及用户授权
用户定义:
user 主机范围
使用某个用户 从哪个(些)地址访问数据库
单ip:"10.0.0.200"
一个网段:"10.0.0.%"---->10.0.0.1----->10.0.0.254
一个地址范围"10.0.0.5%"---->10.0.0.50---->10.0.0.59
超级管理员(管理)用户:root
可以对该数据库做任何操作,相当于linux中的root用户
密码设置:在linux系统中
mysqladmin -u root -p password '123456'
更改密码:
(1)linux命令行
mysqladmin -u root -p123456 password 'ywx123456'
(2)在mysql命令行下
mysql> update user set password=password('123456') where user=root;
mysql>flush privileges;
root用户的权限范围建议为本地有效,及只能本地使用root登录
root@'localhost'`
普通用户: 由管理员用户root来创建及分配相应的权限,等同于linux中的普通用户
密码设置:进入root用户权限的数据库,由root用户来设置 create user zabbix@'10.0.0.%' identified by '123'
用户的功能:
(1)、用来登录数据库
(2)、用来管理数据库对象(库、表)用户的权限(功能):针对不同用户设置对不同对象的管理能力。
如:对库和表的select、update、delete、insert、create。。。操作
用户权限的范围:
*.* 全局范围:对所有库和所有库的表经行操作
ywx.* 单库级别:对ywx库的所有表经行操作
ywx.t1 单表级别:对ywx库的t1表经行操作
赋予用户权限命令结构:
grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456';
命令 权限(all:所有) 用户权限范围 用户范围 访问密码
ALL权限:
SELECT,INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES,
INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE,
REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE,
CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE
开发用户一般赋予的权限:
grant SELECT,INSERT, UPDATE, DELETE, CREATE, DROP on testdb.* to zabbix@'10.0.0.%';
创建用户并授权 grant all on *.* to root@'10.0.0.%' identified by '123';
查询用户权限
show grants for zabbix@'10.0.0.%';
show grants for root@'10.0.0.%';
收回权限
revoke create,drop on testdb.* from zabbix@'10.0.0.%';
revoke all on testdb.* from zabbix@'10.0.0.%';
思考:
grant select on *.* to zabbix@'10.0.0.%' ;
zabbix用户在10.0.0.0网段可有mysql数据库的所有权限
grant INSERT, UPDATE, DELETE, CREATE, DROP on testdb.* to zabbix@'10.0.0.%';
zabbix用户在10.0.0.0网段有对testdb库的所有表的INSERT, UPDATE, DELETE, CREATE, DROP权限
grant update on testdb.t1 to zabbix@'10.0.0.%';
zabbix用户在10.0.0.0网段有对testdb库的t1表仅有update权限
问:zabbix@'10.0.0.%'对T1表到底有什么权限?
结论,如果对某个用户在不同数据库级别都设置了权限,最终权限相叠加,及加起来的最大权限为准。
建议:尽量不要多范围授权。
2、mysql数据库接口程序的使用
mysql接口程序
可以在不进入数据库来执行数据的操作并显示操作结构
1、显示mysql中有那个数据库
mysql -u root -p123456 -e "show databases;"
============================显示的内容====================================
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| ywx |
| ywx3306 |
+--------------------+
===========================================================================
[root@mysql-1 ~]# mysql -u root -p123456 -e "show variables like '%server_id%';"
============================显示的内容====================================
Warning: Using a password on the command line interface can be insecure.
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 101 |
| server_id_bits | 32 |
+----------------+-------+
===========================================================================
2、mysql的接口自带的功能:
mysql>
(1)、\h或help或?
显示在接口下可以用的命令及解释
(2)、\G
执行命令,并显示其结果=====“;”
show databases;
#########################显示结果##################
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| ywx |
| ywx3306 |
+--------------------+
6 rows in set (0.00 sec)
###################################################
show databases\G
#########################显示结果##################
mysql> show databases\G
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: performance_schema
*************************** 4. row ***************************
Database: test
*************************** 5. row ***************************
Database: ywx
*************************** 6. row ***************************
Database: ywx3306
6 rows in set (0.00 sec)
###################################################
(3)、tee或\T
输入结果的同时,并保存一份在指定的文件中
如:
把输出的结果保存在/tmp/mysql.txt里面
在/tmp目录下创建mysql.txt文件
touch /tmp/mysql.txt
在mysql数据库中操作
#########################显示结果#######################
mysql> tee /tmp/mysql.txt
Logging to file '/tmp/mysql.txt'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| ywx |
| ywx3306 |
+--------------------+
6 rows in set (0.00 sec)
##########################################################
查看/tmp/mysql.txt文件
##################显示结果#################################
[root@mysql-1 tmp]# cat /tmp/mysql.txt
mysql> tee /tmp/mysql.txt
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| ywx |
| ywx3306 |
+--------------------+
6 rows in set (0.00 sec)
##########################################################
(4)\c或CTRL+c
不执行当前所敲命令CTRL+c仅5.6以上版本支持,5.6及以下版本为退出数据库。
##############################显示结果###############
mysql> show databases\c
mysql>
##########################################################
(5)\s或status
获取当前数据库的状态信息
(6)\.或source
执行外部SQL脚本:二进制日志截取、备份出来的SQL脚本
(7)\u或use
切换数据库
##############################显示结果#######################
mysql> use ywx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
##############################################################
3、mysql数据库服务器端命令(SQL)
(1)、SQL:结构化的查询语言,mysql接口程序只负责接收SQL,传送给SQL层
(2)、SQL的种类:
DDL:数据库(对象)定义语言
DCL:数据库控制语言(grant revoke)
DML: 数据(行)操作语言(update delete insert)
DQL: 数据库查询语言(show select)
DDL操作的对象是库和表
库:
定义:库名字和库的基本属性
如何定义:
create database ywx;
create schema ywx;
创建一个数据库
show databases;
查看一个数据库
create database ywx character set utf8;
创建一个数据库并设置其字符集
show create database ywx;
查看ywx数据库定义了什么属性
drop database ywx;
删除ywx数据库
help create database;
查看create database命令的用法
字符集: CHARACTER SET [=] charset_name
排序规则:COLLATE [=] collation_name
改库的字符集
alter database [db_name] character set character_name collate collation_name
mysql>alter database ywx charset utf8mb4;
mysql>show create database ywx;
###########################################显示结果#####################
mysql> show create database ywx;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| ywx | CREATE DATABASE `ywx` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter database ywx charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
mysql> show create database ywx;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| ywx | CREATE DATABASE `ywx` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
#################################################################################
DDL操作的对象是库和表
表:
表数据:数据行
表属性(元数据):表名、列名字、列定义(数据类型、约束、特殊列属性)、表的索引信息
定义表的属性
use ywx;
进入ywx数据库
create table t1(id int ,name varchar(20));
创建t1表,表有2列,分别为id和name
show tables;
查看ywx库的所有表
show create table t1;
查看t1表及表中的内容属性
desc t1;
查看t1表的列
drop table t1;
删除t1表
修改:
进入相应的数据库
use ywx;
(1)在表中添加一列(名字为age,类型为int)
alter table t1 add age int;
(2)在表中添加多列
alter table t1 add bridate datetime ,add gender enum('M','F');
(3)在指定列后添加一列(在id列后添加一列为stu_id)
alter table t1 add stu_id int after id;
(4)在表中的最前面添加一列(sid,int)
alter table t1 add sid int first;
(5)删除列(删除sid列)
alter table t1 drop sid;
(6)修改列名(把t1表name列的名称该为stu_name列)
alter table t1 change name stu_name varchar(20);
(7)修改属性(把stu_id的属性由int修改为varchar(20))
alter table t1 modify stu_id varchar(20);
(8)修改表名(把t1表的名字更改为student)
rename table t1 to student;
alter table t1 rename to student;
DML语句:数据库操作语言
字符用单引号('')括起来
use ywx;
进入ywx库
(1)create table t1(id int ,name varchar(20));
创建t1表,表有2列,分别为id和name
(2)insert into t1 values(1,'zhang3');
给t1表插入id=1,name=zhang3的值
(3)select * from t1;
查寻t1表的所有内容
(4)insert into t1 values (2,'li4'), (3,'wang5'), (4,'ma6');
对表t1连续插入多行的内容
###########################显示结果##########################
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | zhang3 |
| 2 | li4 |
| 3 | wang5 |
| 4 | ma6 |
+------+--------+
4 rows in set (0.00 sec)
##############################################################
(5)insert into t1(name) values ('king');
给t1表的name列插入一个为‘king’的值
###########################显示结果##########################
mysql> select * from t1;
+------+--------+
| id | name |
+------+--------+
| 1 | zhang3 |
| 2 | li4 |
| 3 | wang5 |
| 4 | ma6 |
| NULL | king |
+------+--------+
5 rows in set (0.00 sec)
##############################################################
(6)update t1 set name='zhang33' where id=1;
更新t1表中id=1的name的值改为zhang33
###########################显示结果##########################
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 1 | zhang33 |
| 2 | li4 |
| 3 | wang5 |
| 4 | ma6 |
| NULL | king |
+------+---------+
5 rows in set (0.00 sec)
##############################################################
(7)update t1 set name='zhang33'
更新t1表中的name列全部更改为zhang33(不建议使用)
###########################显示结果##########################
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 1 | zhang33 |
| 2 | zhang33 |
| 3 | zhang33 |
| 4 | zhang33 |
| NULL | zhang33 |
+------+---------+
5 rows in set (0.00 sec)
##############################################################
(8)delete from t1 where id=1;
删除t1表中id=1的行
############################显示结果###########################
mysql> select * from t1;
+------+---------+
| id | name |
+------+---------+
| 2 | zhang33 |
| 3 | zhang33 |
| 4 | zhang33 |
| NULL | zhang33 |
+------+---------+
4 rows in set (0.00 sec)
##############################################################
(9)delete from t1;
删除表中所有行,比较危险。一行一行删除表中数据
truncate table t1;
truncate为DDL语言
在物理上删除表数据,速度比较快
DQL:数据库查询语言select 语句
select user,host,password from mysql.user;
查询mysql数据库中的user表的user、host、password列的内容
使用source命令导入world.sql数据库
mysql>source world.sql;
############################显示结果###########################
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| world |
| ywx |
| ywx3306 |
+--------------------+
7 rows in set (0.00 sec)
##############################################################
use world;
进入world数据库
show tables;
显示该数据库中的所有表
############################显示结果###########################
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
##############################################################
(1)显示world.city表的结构(显示world.city表所包含的列)
mysql> desc world.city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(2)select id,name from world.city;
查询world.city表的id和name列的内容
mysql> select id,name from world.city;
+------+------------------------------------+
| id | name |
+------+------------------------------------+
| 1 | Kabul |
| 2 | Qandahar |
| 3 | Herat |
| 4 | Mazar-e-Sharif |
| 5 | Amsterdam |
| 6 | Rotterdam |
| 7 | Haag |
| 8 | Utrecht |
| 9 | Eindhoven |
| 10 | Tilburg |
| 11 | Groningen |
。。。。。
(3)select * from world.city;
查询world.city表中的所有列的内容
mysql> select * from world.city ;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 5 | Amsterdam | NLD | Noord-Holland | 731200 |
| 6 | Rotterdam | NLD | Zuid-Holland | 593321 |
| 7 | Haag | NLD | Zuid-Holland | 440900 |
| 8 | Utrecht | NLD | Utrecht | 234323 |
| 9 | Eindhoven | NLD | Noord-Brabant | 201843 |
| 10 | Tilburg | NLD | Noord-Brabant | 193238 |
........
(4)select条件查询where
查询中国(CHN)所有城市
mysql> select * from world.city where CountryCode='CHN';
+------+--------------------+-------------+--------------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------------+-------------+--------------+------------+
| 1890 | Shanghai | CHN | Shanghai | 9696300 |
| 1891 | Peking | CHN | Peking | 7472000 |
| 1892 | Chongqing | CHN | Chongqing | 6351600 |
| 1893 | Tianjin | CHN | Tianjin | 5286800 |
| 1894 | Wuhan | CHN | Hubei | 4344600 |
| 1895 | Harbin | CHN | Heilongjiang | 4289800 |
| 1896 | Shenyang | CHN | Liaoning | 4265200 |
| 1897 | Kanton [Guangzhou] | CHN | Guangdong | 4256300 |
| 1898 | Chengdu | CHN | Sichuan | 3361500 |
| 1899 | Nanking [Nanjing] | CHN | Jiangsu | 2870300 |
........
(5)select查询中国(CHN)安徽省所有城市信息
mysql> select * from world.city where countrycode='CHN' and district='anhui';
+------+------------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+------------+-------------+----------+------------+
| 1916 | Hefei | CHN | Anhui | 1369100 |
| 1937 | Huainan | CHN | Anhui | 700000 |
| 1961 | Bengbu | CHN | Anhui | 449245 |
| 1964 | Wuhu | CHN | Anhui | 425740 |
| 1981 | Huaibei | CHN | Anhui | 366549 |
| 2005 | Ma´anshan | CHN | Anhui | 305421 |
| 2031 | Anqing | CHN | Anhui | 250718 |
| 2043 | Tongling | CHN | Anhui | 228017 |
| 2097 | Fuyang | CHN | Anhui | 179572 |
| 2133 | Suzhou | CHN | Anhui | 151862 |
| 2144 | Liu´an | CHN | Anhui | 144248 |
| 2180 | Chuzhou | CHN | Anhui | 125341 |
| 2183 | Chaohu | CHN | Anhui | 123676 |
| 2204 | Xuangzhou | CHN | Anhui | 112673 |
| 2216 | Bozhou | CHN | Anhui | 106346 |
| 2226 | Huangshan | CHN | Anhui | 102628 |
+------+------------+-------------+----------+------------+
16 rows in set (0.00 sec)
(6)查询世界上人口数量在10W-20W城市信息
mysql> select * from world.city where population between 100000 and 200000;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 10 | Tilburg | NLD | Noord-Brabant | 193238 |
| 11 | Groningen | NLD | Groningen | 172701 |
| 12 | Breda | NLD | Noord-Brabant | 160398 |
| 13 | Apeldoorn | NLD | Gelderland | 153491 |
| 14 | Nijmegen | NLD | Gelderland | 152463 |
| 15 | Enschede | NLD | Overijssel | 149544 |
| 16 | Haarlem | NLD | Noord-Holland | 148772 |
| 17 | Almere | NLD | Flevoland | 142465 |
。。。。。。
(7)查询中国或者日本的所有城市的信息
select * from world.city where in ('CHN','JPN');
(8)模糊查询
select * from world.city where countrycode like 'ch%';
查询world.city表中countrycode列中'ch'开头的所有信息
select 排序并限制
(1)按照人口数量排序出中国的城市信息(ASC:从小到大排序/DESC:从大到小排序)
select * from world.city where countrycode='CHN' order by population ASC;
select * from world.city where countrycode='CHN' order by population DESC;
(2)按照多列排序,人口+省排序
select * from world.city where countrycode='CHN' order by id DESC;
从world.city表中选出countrycode=CHN的行,并以id号按照从大到小的顺序排列
slect * from world.city order by 5 DESC;
从world.city表中按照第5列从大到小的顺序排列
第5列如下所示为population,及按照总人口从大到小的顺序排列
mysql> desc world.city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(3)排序并限制
从world.city表中countrycode='CHN'按照population从大到小的顺序排列,并显示前20行的内容
1-20
SELECT * FROM world.`city` WHERE countrycode='CHN'
ORDER BY Population DESC LIMIT 20;
+------+--------------------+-------------+--------------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------------+-------------+--------------+------------+
| 1890 | Shanghai | CHN | Shanghai | 9696300 |
| 1891 | Peking | CHN | Peking | 7472000 |
| 1892 | Chongqing | CHN | Chongqing | 6351600 |
| 1893 | Tianjin | CHN | Tianjin | 5286800 |
| 1894 | Wuhan | CHN | Hubei | 4344600 |
| 1895 | Harbin | CHN | Heilongjiang | 4289800 |
| 1896 | Shenyang | CHN | Liaoning | 4265200 |
| 1897 | Kanton [Guangzhou] | CHN | Guangdong | 4256300 |
| 1898 | Chengdu | CHN | Sichuan | 3361500 |
| 1899 | Nanking [Nanjing] | CHN | Jiangsu | 2870300 |
| 1900 | Changchun | CHN | Jilin | 2812000 |
| 1901 | Xi´an | CHN | Shaanxi | 2761400 |
| 1902 | Dalian | CHN | Liaoning | 2697000 |
| 1903 | Qingdao | CHN | Shandong | 2596000 |
| 1904 | Jinan | CHN | Shandong | 2278100 |
| 1905 | Hangzhou | CHN | Zhejiang | 2190500 |
| 1906 | Zhengzhou | CHN | Henan | 2107200 |
| 1907 | Shijiazhuang | CHN | Hebei | 2041500 |
| 1908 | Taiyuan | CHN | Shanxi | 1968400 |
| 1909 | Kunming | CHN | Yunnan | 1829500 |
+------+--------------------+-------------+--------------+------------+
20 rows in set (0.00 sec)
11-20
从world.city表中countrycode='CHN'按照population从大到小的顺序排列,并显示11-20行的内容
SELECT * FROM world.`city` WHERE countrycode='CHN'
ORDER BY Population DESC LIMIT 10,10 ;
第一个10:表示从第10行开始
第二个10:表示后面连续的10行
SELECT * FROM world.`city` WHERE countrycode='CHN'
ORDER BY Population DESC LIMIT 10 OFFSET 10 ;
从offset 10 表示从第10行开始后面的几行
limit 10 表示10行的内容
从10行开始,后面连续10行的内容
+------+--------------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+----------+------------+
| 1900 | Changchun | CHN | Jilin | 2812000 |
| 1901 | Xi´an | CHN | Shaanxi | 2761400 |
| 1902 | Dalian | CHN | Liaoning | 2697000 |
| 1903 | Qingdao | CHN | Shandong | 2596000 |
| 1904 | Jinan | CHN | Shandong | 2278100 |
| 1905 | Hangzhou | CHN | Zhejiang | 2190500 |
| 1906 | Zhengzhou | CHN | Henan | 2107200 |
| 1907 | Shijiazhuang | CHN | Hebei | 2041500 |
| 1908 | Taiyuan | CHN | Shanxi | 1968400 |
| 1909 | Kunming | CHN | Yunnan | 1829500 |
+------+--------------+-------------+----------+------------+
10 rows in set (0.01 sec)
表连接查询:多表查询
use world
DESC city
mysql> DESC city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
DESC countrylanguage
mysql> DESC countrylanguage;
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| CountryCode | char(3) | NO | PRI | | |
| Language | char(30) | NO | PRI | | |
| IsOfficial | enum('T','F') | NO | | F | |
| Percentage | float(4,1) | NO | | 0.0 | |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
---- 中国所有城市信息+使用语言
(1)从world.city和world.countrylanguage2张表中查询countrycode ='CHN'的name、countrycode、
population、language的信息
SELECT ci.NAME ,ci.countrycode ,ci.population,cl.language
FROM
city AS ci , countrylanguage AS cl
WHERE ci.countrycode ='CHN'
AND
ci.CountryCode=cl.CountryCode;
相当于
SELECT city.NAME ,city.countrycode ,city.population,countrylanguage.language
FROM
city , countrylanguage
WHERE city.countrycode ='CHN'
AND
city.CountryCode=countrylanguage.CountryCode;
相当于
SELECT NAME,ci.countrycode ,cl.language ,ci.population
FROM city ci , countrylanguage cl
WHERE
ci.countrycode='chn' AND
ci.`CountryCode`=cl.countrycode;
(2) NATURAL JOIN子句 自动到两张表中查找所有同名同类型的列拿来做连接列,进行相等 连接
从world.city和world.countrylanguage2张表中查询population > 10000000的name、countrycode、
language、population的信息并按照从小到大的顺序排序
以city表和countrylanguage表中相同的countrycode列做连接
SELECT NAME,countrycode ,LANGUAGE ,population
FROM city NATURAL JOIN countrylanguage
WHERE population > 10000000
ORDER BY population;
(3)using()用于两张表的join查询,要求using()指定的列在两个表中均存在,并使用之用于join的条件。
SELECT NAME,countrycode ,LANGUAGE ,population
FROM city JOIN countrylanguage
USING(countrycode);
---- 查询青岛这个城市,所在的国家具体叫什么名字
DESC city
#######################################显示的结果#####################
mysql> DESC city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
#########################################################################
DESC country
#######################################显示的结果#####################
mysql> desc country;
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Code | char(3) | NO | PRI | | |
| Name | char(52) | NO | | | |
| Continent | enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') | NO | | Asia | |
| Region | char(26) | NO | | | |
| SurfaceArea | float(10,2) | NO | | 0.00 | |
| IndepYear | smallint(6) | YES | | NULL | |
| Population | int(11) | NO | | 0 | |
| LifeExpectancy | float(3,1) | YES | | NULL | |
| GNP | float(10,2) | YES | | NULL | |
| GNPOld | float(10,2) | YES | | NULL | |
| LocalName | char(45) | NO | | | |
| GovernmentForm | char(45) | NO | | | |
| HeadOfState | char(60) | YES | | NULL | |
| Capital | int(11) | YES | | NULL | |
| Code2 | char(2) | NO | | | |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
15 rows in set (0.00 sec)
#########################################################################
SELECT NAME,countrycode FROM city WHERE NAME='qingdao';
SELECT NAME FROM country WHERE CODE='CHN';
--------------------------------
SELECT ci.name ,ci.countrycode,ci.population ,co.name
FROM city AS ci
JOIN
country AS co
ON ci.countrycode=co.code
AND
ci.name='qingdao';
---------------------------------
group by +聚合函数(avg()、max()、min()、sum())
SELECT countrycode ,SUM(population) FROM city
WHERE countrycode = 'chn'
GROUP BY countrycode;
union
用来替换 or 、in()
SELECT * FROM world.city
WHERE countrycode IN ('CHN','JPN');
改写为:
SELECT * FROM world.city
WHERE countrycode ='CHN'
union
SELECT * FROM world.city
WHERE countrycode ='JPN';