mysql客户端命令 管理: help ? h : 查看帮助,查看mysql的管理命令 G:格式化查询,结果以key:value形式展示 c:结束当前的sql语句 status s:查看mysql状态信息 source .:导入sql文件 use u:切换数据库 mysqladmin客户端管理命令
#查看MySQL进程是否存活 [root@db01 ~]# mysqladmin ping mysqld is alive #查看mysql信息 [root@db01 ~]# mysqladmin status #关闭mysql进程 [root@db01 ~]# mysqladmin shutdown #查看MySQL当前参数 [root@db01 ~]# mysqladmin variables #库外创建数据库 [root@db01 ~]# mysqladmin create aaa #库外删除数据库 [root@db01 ~]# mysqladmin drop aaa #刷新binlog日志 [root@db01 ~]# mysqladmin flush-log #修改密码 [root@db01 ~]# mysqladmin password '123' SQL语句 DDL 数据定义语言 开发规范:库名,表名 小写 1.库 create(创建) Syntax: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name create_specification: #避免库已存在 报错 mysql> create database if not exists zls; #规范创建数据库 mysql> create database if not exists test1 default character set utf8 default collate utf8_general_ci; mysql> create database if not exists test1 charset utf8 collate utf8_general_ci; drop(删除) mysql> drop database zls; alter(修改) mysql> show create database zls1; +----------+---------------------------------------------------------------+ | Database | Create Database | +----------+---------------------------------------------------------------+ | zls1 | CREATE DATABASE `zls1` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+---------------------------------------------------------------+ 1 row in set (0.00 sec) #修改字符集 mysql> alter database zls1 charset gbk; Query OK, 1 row affected (0.00 sec) mysql> show create database zls1; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | zls1 | CREATE DATABASE `zls1` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+--------------------------------------------------------------+ #修改校验规则 mysql> alter database test1 collate utf8_bin; 2.表 创建表create create table tlbb( aid int, name varchar(12), gender enum('nan','nv'), age tinyint, phone int); 数据类型 int: 整数 -2^31 ~ 2^31 -1 varchar:字符类型 (变长) char: 字符类型 (定长) tinyint: 最小整数 -128 ~ 128 enum: 枚举类型 datetime: 时间类型 年月日时分秒 学生表:student sid sname sage sgender cometime create table student2( sid int not null primary key auto_increment comment '学号', sname varchar(10) not null comment '学生姓名', sage tinyint unsigned comment '学生年龄', sgender enum('m','f') not null default 'm' comment '学生性别', cometime datetime not null default NOW() comment '入学时间'); 约束: not null:非空 -------------------------------------------------------------------------------- primary key:主键(唯一,且非空) unique key:唯一键(可以为空) pk = uk + not null -------------------------------------------------------------------------------- auto_increment:自增 unsigned:无符号,和数字结合用,就是非负数 default:默认值 unique key comment:注释 删除表drop mysql> drop table student; 修改表 #修改表名 mysql> alter table tlbb rename student; #增加字段 mysql> alter table stu add gsb varchar(10); #将字段插入到最前面 mysql> alter table stu add youfeng int first; #将字段插入到某个字段的后面 mysql> alter table stu add xmg int after ljk; #删除某个字段 mysql> alter table stu drop ljk; #修改字段的属性 mysql> alter table stu modify qls char(10); #修改字段名和属性 mysql> alter table stu change qls haoda int; DCL数据控制语言 grant grant all on *.* to root@'%' identified by '1'; grant all privileges on *.* to pri2@'%' identified by '1'; grant all on *.* to root@'%' identified by '1' with max_user_connections 1; revoke mysql> revoke select on *.* from pri1@'%'; DML 数据操作语言 增:insert #注意:所有值必须一一对应,如果没有就给null mysql> insert into student2 values(null,'qls',18,'m',now()); #注意:只需要给前面的key添加value,前面key值的顺序可以随意,后面value必须对应 mysql> insert into student2(sname,sage,sgender) values('zls',18,'m'); mysql> insert into student2(sage,sname,sgender) values(18,'zls','m'); #插入多条数据 mysql> insert into student2(sname,sage,sgender) values('zls',18,'m'),('qls',18,'f'); 改:update mysql> update student2 set sgender='f'; #规范用法 必须接where条件 mysql> update student2 set sgender='f' where sid=1; mysql> update student2 set sage=20 where 1=1; 删:delete # 必须接条件 mysql> delete from student2 where sid=2; mysql> delete from student2 where sid>3 and sid<9; 使用update代替delete做伪删除 1.添加一个状态列 mysql> alter table student2 add state enum('1','0') default '1'; 2.使用update删除 mysql> update student2 set state='0' where sid=9; 3.查询的时候接条件 mysql> select * from student2 where state=1; DQL数据查询语言 select 基础用法 #查询city表中的所有内容 mysql> select * from city; #查询指定列的内容 mysql> select name,countrycode from city; #指定条件查询 mysql> select * from city where name='afuhan'; #limit(翻页功能) mysql> select * from city limit 10; mysql> select * from city limit 10,10; #多条件查询> 、< 、>=、<=、<>(!=) mysql> select * from city where countrycode='chn' and population>999999; #模糊查询 mysql> select * from city where countrycode like 'H%'; mysql> select * from city where countrycode like '%H'; mysql> select * from city where countrycode like '%H%'; #排序(顺序) mysql> select id,name,population,countrycode from city order by population limit 0,60; #排序(倒叙) mysql> select id,name,population,countrycode from city order by population desc limit 0,60; #group by + 聚合函数 #聚合函数种类: #max() #min() #avg() #sum() #count() #distinct() #password() #now() #database() +------------+ | database() | +------------+ | world | +------------+ #此时此刻,我想吟诗一首 1.遇到统计想函数 2.形容词前group by 3.函数中央是名词 4.列名select后添加 #统计世界上每个国家的总人口数 select countrycode,sum(population) from city group by countrycode; #统计中国各个省的人口数量(练习) 不加别名: mysql> select District,sum(population) from city where countrycode='CHN' group by District order by sum(population); 别名: mysql> select District as 省,sum(population) as 人口 from city where countrycode='CHN' group by 省 order by 人口; #统每个国家的城市数量(练习) select countrycode,count(name) from city group by countrycode order by count(name); mysql> select countrycode,count(name) from city where countrycode='chn' group by countrycode order by count(name); #and mysql> select * from city where countrycode='CHN' and id>500; #or mysql> select * from city where countrycode='CHN' or countrycode='USA'; #in mysql> select * from city where countrycode in ('CHN','USA'); ####### 联合查询 效率比in和or高 mysql> select * from city where countrycode='CHN' union all select * from city where countrycode='USA'; select 高级用法 1.多表联查(传统连接) 集合: A: 1 2 3 B: 2 3 4 交集:23 并集:1234 差集:14 id:1 2 3 name: qls haoda zhang3 id: 1 2 3 mark:80 90 120 +-------+-----+ |name | mark| +-------+-----+ |haoda | 90 | +-------+-----+ mysql> create table st(id int ,name varchar(10)); Query OK, 0 rows affected (0.02 sec) mysql> create table score(id int ,mark int); Query OK, 0 rows affected (0.01 sec) mysql> insert st values(1,'qls'),(2,'haoda'),(3,'zhang3'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> insert score values(1,80),(2,90),(3,120); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 #查世界上人口数量小于100的城市在哪个国家,城市和国家人口数量分别是多少? 城市名 国家名 城市人口数量 国家人口数量 city.name country.name city.population country.population select city.name,country.name,city.population,country.population from city,country where city.countrycode=country.code and city.population<100; #世界上人口数量小于100的城市在哪个国家,说的什么语言? select city.population,city.name,country.name,countrylanguage.language from city,country,countrylanguage where city.countrycode=country.code and countrylanguage.countrycode=country.code and city.population < 100; mysql> select * from world.city limit 1G ID: 1 Name: Kabul CountryCode: AFG District: Kabol Population: 1780000 mysql> select * from world.country limit 1G Code: ABW Name: Aruba Continent: North America Region: Caribbean SurfaceArea: 193.00 IndepYear: NULL Population: 103000 LifeExpectancy: 78.4 GNP: 828.00 GNPOld: 793.00 LocalName: Aruba GovernmentForm: Nonmetropolitan Territory of The Netherlands HeadOfState: Beatrix Capital: 129 Code2: AW mysql> select * from world.countrylanguage limit 1G CountryCode: ABW Language: Dutch IsOfficial: T Percentage: 5.3
2.内连接 join on(企业常用) #查世界上人口数量小于100的城市在哪个国家,城市和国家人口数量分别是多少? select city.name,city.population,country.name,country.population from city,country where city.countrycode=country.code and city.population<100; select city.name,city.population,country.name,country.population from city join country on city.countrycode=country.code where city.population<100; #世界上人口数量小于100的城市在哪个国家,说的什么语言? ·A join B on 1 join C on 2 join D on 3· select city.population,city.name,country.name,countrylanguage.language from city,country,countrylanguage where city.countrycode=country.code and countrylanguage.countrycode=country.code and city.population < 100; select city.population,city.name,country.name,countrylanguage.language from city join country on city.countrycode=country.code join countrylanguage on countrylanguage.countrycode=country.code where city.population < 100; 建议:小表在前大表在后 3.自连接 natural join # 人口数量大于1000000的城市所在的国家,他们都说什么语言? city.population,city.name,city.countrycode,countrylanguage.language select city.population,city.name,city.countrycode,countrylanguage.language from city,countrylanguage where city.countrycode=countrylanguage.countrycode and city.population > 1000000; # 人口数量大于1000000的城市所在的国家,他们都说什么语言? (自连接) select city.population,city.name,city.countrycode,countrylanguage.language from city natural join countrylanguage where city.population > 1000000; 前提条件:一定要有相同的列名字,并且列中的数据一致 4.外连接(左外连接,右外连接) #左外连接 mysql> select city.name as 城市名称,country.code as 国家代码,country.name as 国家名称 from city left join country on city.countrycodde=country.code and city.population<100 limit 10; #右外连接 mysql> select city.name as 城市名称,country.code as 国家代码,country.name as 国家名称 from city right join country on city.countrycodde=country.code and city.population<100 limit 10; 字符集 字符集:是一个系统支持的所有抽象字符的集合。字符是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。 字符集设置 系统层: #C6: vim /etc/sysconfig/i18n LANG="en US.UTF-8 " #C7: [root@db01 ~]# vim /etc/locale.conf LANG="en_US.UTF-8" 工具 xshell: MySQL: #永久 #修改配置文件/etc/my.cnf [mysqld] character-set-server=utf8 #临时 mysql> set character_set_server=utf8; 从规范保证字符集 #建库 create database db_name charset utf8 collate utf8_general_ci; #建表 create table tb_name(id int) charset utf8 collate utf8_general_ci; gbk 500-60000 utf8 1-90000 gb2312 2-5000 修改数据库的字符集 mysql> alter database zls charset utf8; 修改表的字符集 mysql> alter table zls charset gbk; 企业中修改某个库中的所有表字符集: # mysqldump -uroot -p123 -B xx > /tmp/xx.sql # vim /tmp/xx.sql # :%s#gbk#utf8#g # mysql -uroot -p123 < /tmp/xx.sql update t_char set moneyyb=9999999 where aid=150;