一. MySQL基本操作
1.打開數(shù)據(jù)庫
打開cmd,跳轉(zhuǎn)到MySQL安裝路徑下的bin文件夾
(如D:-->cd EnglishPathSoft/MySQL/bin
或者分步驟打開cd EnglishSoft-->cd mysql
)
執(zhí)行打開數(shù)據(jù)庫操作mysql -u root -p
然后輸入密碼
2.創(chuàng)建數(shù)據(jù)庫
create database mydb;
3.轉(zhuǎn)到當(dāng)前數(shù)據(jù)庫
use mydb;
4.刪除數(shù)據(jù)庫
drop database mydb;
5.創(chuàng)建時修改數(shù)據(jù)庫編碼
create database mydb character set utf8;
6.修改數(shù)據(jù)庫編碼
alter database mydb character set utf8;
7.修改數(shù)據(jù)庫排序規(guī)則
alter database mydb character set collate utf8-general-ci;
二. MySQL查詢語句
1. 查詢字符集
show variables like "char%";
2. 查看數(shù)據(jù)庫所有的表
use mydb;
show tables;
3. 查看表中字段,類型等信息
show keys from mytable;
4. 查看表結(jié)構(gòu)
desc mytable;
5. 查看所有列
select * from mytable;
6. 查詢制定列
select column1,column2 from mytable;
7. 列運算(+,-,*,/)
select column1,column2*12 from mytable;/*isnull()函數(shù):任何值或列與NULL運算后都得NULL.*/
select column1-isnull(column2); /*ifnull(val1,val2)函數(shù):有兩個參數(shù),如果值1位NULL則返回值2.*/
select column1,column2+ifnull(column,0) from mytable;/*去重函數(shù):*/
select distinct column form mytable;/*distinct返回唯一性狈网,不能有重復(fù)字段*/
8. 給列起別名
select column1 'id',column*12 income mytable;
9. 四種連接查詢
a表:
id | name |
---|---|
1 | 張三 |
2 | 李四 |
3 | 王武 |
b表:
id | job | parent_id |
---|---|---|
1 | 23 | 1 |
2 | 34 | 2 |
3 | 34 | 4 |
注:a.id同parent_id 存在關(guān)系
(1) 內(nèi)連接
select a.*,b.* from a inner join b on a.id=b.parent_id
結(jié)果是 :
? | ? | ? | ? | ? |
---|---|---|---|---|
1 | 張三 | 1 | 23 | 1 |
2 | 李四 | 2 | 34 | 2 |
(2) 左連接
select a.*,b.* from a left join b on a.id=b.parent_id
結(jié)果是 :
? | ? | ? | ? | ? |
---|---|---|---|---|
1 | 張3 | 1 | 23 | 1 |
2 | 李四 | 2 | 34 | 2 |
3 | 王武 | null |
(3) 右連接
select a.*,b.* from a right join b on a.id=b.parent_id
結(jié)果是
? | ? | ? | ? | ? |
---|---|---|---|---|
1 | 張3 | 1 | 23 | 1 |
2 | 李四 | 2 | 34 | 2 |
null | 3 | 34 | 4 |
(4)完全連接
select a.*,b.* from a full join b on a.id=b.parent_id
結(jié)果是:
? | ? | ? | ? | ? |
---|---|---|---|---|
1 | 張3 | 1 | 23 | 1 |
2 | 李四 | 2 | 34 | 2 |
3 | null | 3 | 34 | 4 |
4 | 王武 | null |
三. MySQL增加語句
1. 創(chuàng)建表
create table mytable(m_id int(11)); /*需要至少一個字段(列)*/
create table mytable2 like mytable; /*使用舊表創(chuàng)建新表*/
create table mytable3(
m_id int(11) comment 'id',
m_name varchar(20) comment 'name',
primary key(m_id));
create table mytable(
m_id int(11) not null auto-increment comment'key',
m_name varchar(11) not null,
primary key(m_id)
)charset=utf8 collect=utf-8-ci;
2. 在表中添加一個字段
alter table mytable add m_id int(11) not null auto_increment comment 'key'
primary key; /*添加主鍵,普通字段減去劃線部分*/
四. MySQL刪除語句
1. 刪除表
drop table mytable;
2. 刪除字段
alter table mytable drop column m_id;
3. 刪除主鍵
alter table mytable drop primary key;
五. MySQL修改語句
1. 修改字段屬性(加索引)
alter table mytable
charge m_id m_id
int(11) not null auto_increment comment 'key',
add primary key(m_id); /*增加屬性(索引)*/
```SQL
alter table mytable change m_id m_id int(11) null; /去除屬性/
```SQL
alter table mytable drop index m_id; /*去除索引*/
alter table mytable change m_id m_id varchar(11); /*int改varchar*/
2. 修改字段名
alter table mytable change m_no m_num int(11) not null; /*記得保持屬性與索引一致 */