MySQL常用SQL語句

2021-10-01 11:43:00 字數 4636 閱讀 5243

-- 》ddl(data definition language) 資料定義語言,用來定義資料庫物件:庫、表、列等。

-- 1.資料庫操作

-- 建立資料庫 資料庫名字可以任意取 但是建立後資料庫名字會自動轉化為小寫

create

database test;

-- 刪除資料庫

drop

database test;

-- 修改資料庫編碼

alter

database test character

set utf8;

-- 檢視所有資料庫的名稱

show

databases

;-- 切換資料庫 use 資料庫名字

use test;

-- 2.資料庫表操作

-- int:整型

-- -- double:浮點型,例如double(5,2)表示最多5位,其中必須有2位小數,即最大值為999.99;

-- -- char:固定長度字串型別;

-- -- varchar:可變長度字串型別;

-- -- text:字串型別;

-- -- blob:位元組型別;

-- -- date:日期型別,格式為:yyyy-mm-dd;

-- -- time:時間型別,格式為:hh:mm:ss

-- 建立資料表 資料庫會自動將大寫轉為小寫字母 char固定大小需要指定 varchar不固定也需要指定大小。主鍵primary key 自增auto_increment 不為空not null;

create

table info1(

id int

primary

keyauto_increment

, username varchar

(255

)not

null

, *** char(4

),borndate date

, weight double);

-- 刪除表

drop

table info1;

-- 修改表

-- 新增列

alter

table info1 add

(school varchar

(255))

;-- 刪除列

alter

table info1 drop school;

-- 修改列型別

alter

table info1 modify *** varchar(2

)not

null

;-- 修改列名

alter

table info1 change *** gender char(2

);-- 修改表名

alter

table info1 rename

to tab_info1;

-- 檢視表

-- 檢視當前資料庫中所有的表

show

tables

;-- 檢視指定表的建立語句

show

create

table tab_info1;

-- 檢視表的結構

desc tab_info1;

-- 》dml(data manipulation language) 資料操作語言,用來定義資料庫記錄(資料)

-- 1.新增資料

insert

into tab_info1 (id,username,gender,borndate,weight)

values(9

,'twg'

,'男'

,'2019-12-14'

,55.2);

-- 2.刪除資料(低效率)

delete

from tab_info1;

-- 2.刪除資料(高效率)

truncate

table tab_info1;

-- 3.修改資料

update tab_info1 set username=

'唐微港'

,weight=

55.8

;-- 》dql(data query language) 資料查詢語言,用來查詢記錄.

-- 1.普通查詢

-- 查詢列

select name,group_id from tb_spec_param;

-- 帶別名的查詢列

select a.name as

'使用者名稱'

,a.group_id as

'組id'

from tb_spec_param a;

-- 帶條件查詢列(也可以加上別名)

select name,group_id,cid,generic from tb_spec_param where generic=

1and searching=1;

-- 分組查詢列

select a.parent_id,a.name from tb_category a group

by a.parent_id,a.name order

by parent_id asc

;-- 2.複雜查詢

-- 聯合查詢 union:去除重覆記錄,例如:select * from t1 union select * from t2;1、要求多條查詢語句的查詢列數是一致的!2、要求多條查詢語句的查詢的每一列的型別和順序最好一致 3、union關鍵字預設去重,如果使用union all 可以包含重複項

select a.title as

'手機'

from tb_sku a where spu_id=

3union

select b.name as

'機器名稱'

from tb_spec_param b;

-- 連線查詢 連線查詢就是求出多個表的乘積,例如t1連線t2,那麼查詢出的結果就是t1*t2

-- 內連線 內連線的特點:查詢結果必須滿足條件。例如我們向emp表中插入一條記錄: 其中deptno為50,而在dept表中只有10、20、30、40部門,那麼上面的查詢結果中就不會出現「張三」這條記錄,因為它不能滿足e.deptno=d.deptno這個條件

-- select * from tb_spec_group a,tb_spec_param b where a.cid=b.cid;

select

*from tb_spec_group a inner

join tb_spec_param b on a.cid=b.cid;

-- 左外連線 查詢出的結果存在不滿足條件的可能 我們還是用上面的例子來說明。其中emp表中「張三」這條記錄中,部門編號為50,而dept表中不存在部門編號為50的記錄,所以「張三」這條記錄,不能滿足e.deptno=d.deptno這條件。但在左連線中,因為emp表是左表,所以左表中的記錄都會查詢出來,即「張三」這條記錄也會查出,但相應的右表部分顯示null。

select

*from tb_spec_group a left

join tb_spec_param b on a.cid=b.cid;

-- 右外連線 右連線就是先把右表中所有記錄都查詢出來,然後左表滿足條件的顯示,不滿足顯示null。例如在dept表中的40部門並不存在員工,但在右連線中,如果dept表為右表,那麼還是會查出40部門,但相應的員工資訊為null。

select

*from tb_spec_group a right

join tb_spec_param b on a.cid=b.cid;

-- 自然連線(少用)

select

*from tb_spec_group a natural

join tb_spec_param b;

select

*from tb_spec_group a natural

right

join tb_spec_param b;

select

*from tb_spec_group a natural

left

join tb_spec_param b;

-- 子查詢

-- 子查詢就是巢狀查詢,即select中包含select,如果一條語句中存在兩個,或兩個以上select,那麼就是子查詢語句了。where後,作為條件的一部分;from後,作為被查詢的一條表。

-- 在where之後的 where後面的結果只能是唯一的列入這裡查詢出來的結果只能有乙個

select

*from tb_spec_param b where b.cid>

(select a.cid from tb_spec_group a where a.name=

'主體');

-- select * from emp where sal > all (select sal from emp where deptno=30)

-- 查詢條件:工作和工資與殷天正完全相同,這是子查詢

-- select * from emp where (job,sal) in (select job,sal from emp where ename='殷天正')

MySql 常用SQL語句

create database kali use kali show tables create table students sno varchar 10 primary key,sname varchar 10 not null,varchar 2 check in 男 女 age varcha...

常用sql語句(mysql)

給表新增列 sql alter table table name add column col name varchar 255 not null default default value 增加表列,指定格式 sql alter table table name add col name bool...

MySQL常用SQL語句

查詢一張表中的所有資料 sql view plain copy select from table name 查詢一張表中的指定列資料 sql view plain copy select column a,column b from table name 按條件查詢一張表中的所有資料 sql vi...