連接數(shù)據(jù)庫(kù)
>mysql -u用戶名 -p密碼
>mysql -u用戶名 -p
Enter password: ******
數(shù)據(jù)庫(kù)管理
mysql> show databases;
- 創(chuàng)建數(shù)據(jù)庫(kù)
mysql> create database 數(shù)據(jù)庫(kù)名稱(chēng);
- 創(chuàng)建指定字符集數(shù)據(jù)庫(kù)
mysql> create database 數(shù)據(jù)庫(kù)名稱(chēng);
-> default character set gbk;
mysql> show create database 數(shù)據(jù)庫(kù)名稱(chēng);
mysql> drop database 數(shù)據(jù)庫(kù)名稱(chēng);
表管理
mysql> use 數(shù)據(jù)庫(kù)名稱(chēng);
mysql> show tables;
mysql> create table 表名(列 列的類(lèi)型料饥,列 列的類(lèi)型);
mysql> desc 表名;
mysql> drop table 表名;
mysql> alter table 表名 add column 列 列的類(lèi)型;
mysql> alter table 表名 drop column 列名;
mysql> alter table 表名 modify column 列名 新的類(lèi)型;
mysql> alter table 表名 change column 列名 新的列名 新的類(lèi)型;
增刪改數(shù)據(jù)
mysql> insert into 表名(列名,列名) values(值,值);
- 開(kāi)啟事務(wù)的情況下,delete刪除的數(shù)據(jù)可以恢復(fù),truncate刪除的數(shù)據(jù)不能恢復(fù)
- 刪除表中全部數(shù)據(jù)
mysql> delete from 表名;
mysql> delete from 表名 where 列名=值 and|or 列名=值;
mysql> update 表名 set 列=值 where 列=值;
查詢數(shù)據(jù)
mysql> select 列名 from 表名;
mysql> select 列名 as 別名 from 表名;
mysql> select distinct 列名 from 表名;
mysql> select * from 表名 where 列名=值;
mysql> select * from 表名 where 列名=值 and 列名=值;
mysql> select * from 表名 where 列名 is null;
mysql> select * from 表名 where 列名 is not null;
>瞧毙、<瓮顽、>=居触、<=叠穆、=瘦材、<>期奔、between and
mysql> select 列名+列名 from 表名;
mysql> select 列名+列名 as 別名 from 表名;
mysql> select * from 表名 where 列名 like '%模糊字段%';
mysql> select * from 表名 where 列名 like '模糊字段_';
通過(guò)聚合函數(shù)查詢
mysql> select count(*) from 表名;
- 如果列沒(méi)有數(shù)據(jù)不計(jì)算
mysql> select count(列名) from 表名;
mysql> select avg(列名) from 表名;
mysql> select max(列名) from 表名;
mysql> select min(列名) from 表名;
mysql> select sum(列名) from 表名;
mysql> select * from 表名 order by 列名;
mysql> select * from 表名 where 列名=值 order by 列名;
mysql> select * from 表名 order by 列名 asc;
mysql> select * from 表名 order by 列名 desc;
mysql> select * from 表名 order by 列名 asc,列名 desc;
mysql> select 列名,count(*) from 表名 group by 列名;
mysql> select 列名,count(*) from 表名 where 篩選條件 group by 列名 order 列名;
mysql> select 列名,count(*) from 表名 where 列名=值 group by 列名 having count(*)>1 order by 列名;
mysql> select * from 表名 limit 開(kāi)始位置,數(shù)據(jù)條數(shù);