Mysql資料庫常規操作 建表 查詢

2022-08-16 12:36:11 字數 4326 閱讀 9950

一、表單操作

1-1.建立表

create table tb_name( id in primary key auto_increment); 

1-2.檢視表

desc table_name;

show create table tb_name \g

1-3.修改表名

alter table old_name rename new_name;

1-4.增加字段

4-1.在表的最後乙個位置增加字段

alter table tb_name add 欄位名 資料型別等;

4-2.在表的第乙個位置增加字段

alter table tb_name add 欄位名 資料型別等 first;

4-3.在表的指定位置增加字段

alter table tb_name add 欄位名 資料型別等 after 指定字段;

1-5.刪除字段

alter table tb_name drop 欄位名;

1-6.修改字段

a.修改欄位的資料型別

alter table tb_name modify 欄位名 資料型別;

b.修改欄位的名字

alter table tb_name change 舊欄位名 新欄位名 新資料型別;

二、mysql-子查詢

a、帶in的子查詢

當主查詢的條件在子查詢的結果中時

b、帶any的子查詢

1、=any:功能與in效果一樣

2、>any:比子查詢返貨的資料記錄最小值,還要大

3、c、帶all的子查詢

>all:比子查詢返回記錄中最大的還要大

mysql> select name,year_wage from tb_player where year_wage > all(selec t name,year_wage from tb_player where name="卡特");

查詢,位置和月獎金,與「卡特」一樣的球員

mysql> select name,position,month_bonus from tb_player where (position,month_bon

us)=(select position,month_bonus from tb_player where name="卡特");

d、exist查詢,tb_player中 tb_team 沒有的球隊

select name from tb_team as t

where not exists(select team from tb_player where team=t.name);

e、from查詢

select tmp.team,max(tmp.year_wage) from (select team,year_wage from tb_player group by team,name) as tmp group by tmp.team;

三、mysql-左連線和右連線(left join和right join)

表a記錄如下: 

aid        anum 

1           a20050111 

2           a20050112 

3           a20050113 

4           a20050114 

5           a20050115 

表b記錄如下: 

bid        bname 

1           2006032401 

2           2006032402 

3           2006032403 

4           2006032404 

8           2006032408

實驗如下: 

1.left join 

sql語句如下:

select * 

from a 

left join b 

on a.aid = b.bid

結果如下: 

aid        anum                   bid           bname 

1            a20050111         1               2006032401 

2            a20050112         2              2006032402 

3            a20050113         3              2006032403 

4            a20050114         4              2006032404 

5            a20050115         null       null 

(所影響的行數為 5 行)

結果說明: 

left join是以a表的記錄為基礎的,a可以看成左表,b可以看成右表,left join是以左表為準的.換句話說,左表(a)的記錄將會全部表示出來,而右表(b)只會顯示符合搜尋條件的記錄(例子中為: a.aid = b.bid). 

b表記錄不足的地方均為null.

2.right join 

sql語句如下:

select * 

from a 

right join b 

on a.aid = b.bid

結果如下: 

aid        anum                   bid           bname 

1            a20050111         1               2006032401 

2            a20050112         2              2006032402 

3            a20050113         3              2006032403 

4            a20050114         4              2006032404 

null    null                8              2006032408 

(所影響的行數為 5 行)

結果說明: 

仔細觀察一下,就會發現,和left join的結果剛好相反,這次是以右表(b)為基礎的,a表不足的地方用null填充.

3.inner join 

sql語句如下:

select * 

from a 

inner join b 

on a.aid = b.bid

結果如下: 

aid        anum                   bid           bname 

1            a20050111         1              2006032401 

2            a20050112         2              2006032402 

3            a20050113         3              2006032403 

4            a20050114         4              2006032404

結果說明: 

很明顯,這裡只顯示出了 a.aid = b.bid的記錄.這說明inner join並不以誰為基礎,它只顯示符合條件的記錄.

-----------------[以下為網上的一點資料]------------------ 

left join操作用於在任何的 from 子句中,組合**表的記錄。使用 left join 運算來建立乙個左邊外部聯接。左邊外部聯接將包含了從第乙個(左邊)開始的兩個表中的全部記錄,即使在第二個(右邊)表中並沒有相符值的記錄。

語法: 

from table1 left join table2 on table1.field1 compopr table2.field2

說明: 

① table1, table2引數用於指定要將記錄組合的表的名稱。 

② field1, field2引數指定被聯接的字段的名稱。且這些字段必須有相同的資料型別及包含相同型別的資料,但它們不需要有相同的名稱。 

③ compopr引數指定關係比較運算子:"=", "<", ">", "<=", ">=" 或 "<>"。 

④ 如果在inner join操作中要聯接包含memo 資料型別或 ole object 資料型別資料的字段,將會發生錯誤。

MySQL資料庫建庫建表以及外來鍵級聯操作

mysql資料庫 建庫語句 create database 資料庫名 使用資料庫 use 資料庫名 create database robowolf material use robowolf material 刪表語句 drop table 表名 建表語句 create table 資料名稱 資料...

mysql資料庫基礎建庫與建表

新建資料庫 create database name 有分號,name為資料庫名,不能重名,首字母不能為數字和 create database name charset utf8mb4 clooate utf8mb4 general ci 查詢和選擇資料庫 show databases use na...

資料庫(3) MySQL建庫 建表

show databases use database名稱 例如 use mysql 如果沒有進入某一庫,在對庫中的資料進行訪問時,會提示 no database selected 檢視當前已進入的資料庫 show tables 語法 drop database 庫名 例如 drop databas...