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 1\G ID: 1 Name: Kabul CountryCode: AFG District: Kabol Population: 1780000 mysql> select * from world.country limit 1\G 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 1\G 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;