MYSQL 操作數(shù)據(jù)庫、表等操作

數(shù)據(jù)庫基本操作

1.數(shù)據(jù)庫

(DDL create/show/alter/drop)

創(chuàng)建數(shù)據(jù)庫

語法:
create database 表名 character set 字符集 collate 校驗規(guī)則;
示例: 創(chuàng)建一個名字為person,并且設(shè)置該數(shù)據(jù)庫字符集為utf8,校驗規(guī)則為utf8_bin的數(shù)據(jù)庫。
create database person character set utf8 collate utf8_bin;

查看數(shù)據(jù)庫

// 查看數(shù)據(jù)庫創(chuàng)建信息
show create database person;
// 查看數(shù)據(jù)庫列表
show databases;
// 修改數(shù)據(jù)庫
alter database person character set utf8;
// 使用創(chuàng)建的數(shù)據(jù)庫
use person;
// 查看正在使用的數(shù)據(jù)庫
select database();

刪除數(shù)據(jù)庫

drop database person;

2. 表

創(chuàng)建表

語法:
create table 表名(
    列名1 列名類型 約束,
    列名2 列名類型 約束,
)
注:約束就是約束字段,值有主鍵約束(primary key)、唯一約束(unique)、非空約束(not null)

示例:
create table student(
    id int primary key,
    name varchar(12),
    sex int,
    age int
);

查看表

show tables;
// 查看表的定義
show create table student;
// 查看表的結(jié)構(gòu)
desc student;

修改表

添加列(add)、修改列(modify)、修改列名(change)、刪除列(drop)、修改表名(rename)、修改表字符集

//添加列(add)
alter table 表名 add 列名 列類型 列約束;
alter table student add grade int not null;

// 修改列(modify)
alter table student modify sex varchar(2);

//修改列名(change)
alter table student change sex gender varchar(2);

//刪除列(drop)
alter table student drop grade;

//修改表名(rename)
rename table student to teacher;

//修改表字符集
alter table student character set gbk;

//刪除表
drop table student;

3.sql完成對表中數(shù)據(jù)CRUD的操作

(DML>>>>>> insert、update、delete)

插入數(shù)據(jù)

語法: insert into 表名(列1,列2,列3) values(值1,值2,值3);
示例: insert into student(id,name,age,gender) values(1,'張三',12,1);
簡單寫法: insert into student values(2,'李四',15,1);
注: 簡單寫法適用于全部列都插入情況,如果只插入部分列,表名后面列名是不能省略的。

// 批量插入,values 后面值集合用逗號隔開
insert into student values(3,'李小花',15,2),(4,'李五',16,1),(5,'李哈哈',25,1);

刪除數(shù)據(jù)

語法: delete from 表名 where條件
示例: delete from student where id=5;
       delete from student; //這會將表中數(shù)據(jù)全部清除;
       
注: delete與truncate有什么區(qū)別?
delete DML   是刪除表中數(shù)據(jù)
truncate DDL  是先刪除表,再新建表

更新數(shù)據(jù)

語法: update 表名 set 列1=值1,列2=值2 where條件
示例: update student set name='修改李四名',gender=2 where id=3;
       update student set name='修改李四名',gender=2; // 不加條件會修改表中所有條數(shù)據(jù)

查詢數(shù)據(jù)

select [distinct] [*] [列1,列2] from 表名 where條件  //distinct 為去除重復(fù)數(shù)據(jù)

//查看所有數(shù)據(jù)
select * from student;

//只查看表中name列
select name from student; 

//別名查詢---表別名,用在多表查詢
select s.name from student as s;

//別名查詢---列別名(可以省略as關(guān)鍵字)
select name as 名字 from student;

//查詢?nèi)サ糁貜?fù)的值,去掉age中重復(fù)的值
select distinct age from student;

//運算查詢,只是在運算結(jié)果上做了運算處理,并沒有改表表的結(jié)構(gòu)和表的值
select *,age+1 as 一年后年齡 from student;

//條件查詢
select * from student where age > 15;
注: where后面關(guān)系運算符有:<> 不等于(標準)、!= 不等于(非標準)、 > 、>= 、<、<=、 =
     where后面邏輯運算符有:and、or、not
     like模糊查詢: _ 代表一個字符,% 代表多個字符
     in在某個范圍中查詢
例:查詢不是15歲的學(xué)生
    select * from student where age <> 15;
    查詢12到15歲的學(xué)生
    select * from student where age >12 and age < 15;
    select * from student where age between 12 and 15;
    查詢小于13歲或者大于16歲的學(xué)生
    select * from student where age < 13 or age > 16;
    查詢學(xué)生名字中帶有李的學(xué)生
    select * from student where name like '李%';
    查詢學(xué)生中年齡為12歲、15歲、18歲的學(xué)生
    select * from student where age in (12,15,18);
    
// 排序查詢 order by(asc升序、desc降序,默認生序)
例:不寫排序默認生序,按照年齡從小到大排序查詢
    select * from student order by age; 
    按照年齡降序查詢
    select * from student order by age desc;
    學(xué)生名中有‘李’字,然后按照升序查詢
    select * from student where name like '李%' order by age asc;
    
//聚合函數(shù)
sum()求和、avg()求平均、count()統(tǒng)計數(shù)量、max()最大值、min()最小值
例:求學(xué)生年齡總和
    select sum(age) from student;
    獲取所有學(xué)生總數(shù)
    select count(*) from student;
    查出所有學(xué)生中年齡大于平均年齡的學(xué)生
    select * from student where age > (select avg(age) from student);
注:where 后不能夠接聚合函數(shù),出現(xiàn)在分組關(guān)鍵字之前
    
// 分組
group by
例:查詢按照性別分組后,男女的數(shù)量總和
   select gender, count(*) from student group by gender;
   查詢按照性別分組,分組統(tǒng)計每組學(xué)生的平均年齡,并且平均年齡 > 14的;
   select gender, avg(age) from student group by age having avg(age) > 14;
注:having 后能夠接聚合函數(shù),出現(xiàn)在分組關(guān)鍵字后面

// sql 編寫順序:select...from...where...group by...having...order by
        執(zhí)行順序:from...where...group by...having...select...order by 

連表查詢

準備兩張表

mysql> select * from profession;

+------+------------+
| id   | profession |
+------+------------+
|  200 | 教師        |
|  201 | 學(xué)生        |
|  202 | 程序員      |
+------+------------+

mysql> select * from game_person;

+------+--------+--------+--------+
| id   | name   | sex    | dep_id |
+------+--------+--------+--------+
|    1 | 魯班   | male   |    200 |
|    2 | 小喬   | female |    200 |
|    3 | 李白   | male   |    201 |
+------+--------+--------+--------+
// 交叉連接 不適用任何匹配條件,生成笛卡爾積
select * from 表-, 表二;

例子:
select * from profession, game_person;

+------+------------+------+--------+--------+--------+
| id   | profession | id   | name   | sex    | dep_id |
+------+------------+------+--------+--------+--------+
|  200 | 教師       |    1 | 魯班   | male   |    200 |
|  201 | 學(xué)生       |    1 | 魯班   | male   |    200 |
|  202 | 程序員     |    1 | 魯班   | male   |    200 |
|  200 | 教師       |    2 | 小喬   | female |    200 |
|  201 | 學(xué)生       |    2 | 小喬   | female |    200 |
|  202 | 程序員     |    2 | 小喬   | female |    200 |
|  200 | 教師       |    3 | 李白   | male   |    201 |
|  201 | 學(xué)生       |    3 | 李白   | male   |    201 |
|  202 | 程序員     |    3 | 李白   | male   |    201 |
+------+------------+------+--------+--------+--------+

// 內(nèi)鏈接 (常用) 只連接匹配的行
select * from staff inner join department on 條件(表1.字段=表2.字段);

例子:
select * from game_person inner join profession on profession.id=game_person. dep_id;

+------+--------+--------+--------+------+------------+
| id   | name   | sex    | dep_id | id   | profession |
+------+--------+--------+--------+------+------------+
|    1 | 魯班   | male   |    200 |  200 | 教師       |
|    2 | 小喬   | female |    200 |  200 | 教師       |
|    3 | 李白   | male   |    201 |  201 | 學(xué)生       |
+------+--------+--------+--------+------+------------+

// 左外連接(常用) 優(yōu)先顯示左表全部記錄 left join
// 右外連接(常用) 優(yōu)先顯示左表全部記錄 right join
select * from staff left join department on 條件(表1.字段=表2.字段)
select * from staff right join department on 條件(表1.字段=表2.字段)

例子:
select * from game_person left join profession on profession.id=game_person. dep_id;

+------+--------+--------+--------+------+------------+
| id   | name   | sex    | dep_id | id   | profession |
+------+--------+--------+--------+------+------------+
|    1 | 魯班   | male   |    200 |  200 | 教師       |
|    2 | 小喬   | female |    200 |  200 | 教師       |
|    3 | 李白   | male   |    201 |  201 | 學(xué)生       |
+------+--------+--------+--------+------+------------+

例子:
select * from game_person right join profession on profession.id=game_person. dep_id;

+------+--------+--------+--------+------+------------+
| id   | name   | sex    | dep_id | id   | profession |
+------+--------+--------+--------+------+------------+
|    1 | 魯班   | male   |    200 |  200 | 教師       |
|    2 | 小喬   | female |    200 |  200 | 教師       |
|    3 | 李白   | male   |    201 |  201 | 學(xué)生       |
| NULL | NULL   | NULL   |   NULL |  202 | 程序員     |
+------+--------+--------+--------+------+------------+

// 全外連接 顯示左右兩個表全部記錄

select * from 表1  left join 表2 on 條件((表1.字段=表2.字段))
union
select * from 表1  right join 表2 on 條件((表1.字段=表2.字段));

例子:
select * from game_person left join profession on profession.id=game_person. dep_id
union
select * from game_person right join profession on profession.id=game_person. dep_id;

+------+--------+--------+--------+------+------------+
| id   | name   | sex    | dep_id | id   | profession |
+------+--------+--------+--------+------+------------+
|    1 | 魯班   | male   |    200 |  200 | 教師       |
|    2 | 小喬   | female |    200 |  200 | 教師       |
|    3 | 李白   | male   |    201 |  201 | 學(xué)生       |
| NULL | NULL   | NULL   |   NULL |  202 | 程序員     |
+------+--------+--------+--------+------+------------+

注意:
#全外連接:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒有的和右邊有左邊沒有的結(jié)果
#注意:mysql不支持全外連接 full join
#強調(diào):mysql可以使用此種方式間接實現(xiàn)全外連接
#注意 union與union all的區(qū)別:union會去掉相同的紀錄

本文作者原創(chuàng),僅供學(xué)習(xí)交流使用,轉(zhuǎn)載需注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容