基本SQL語句使用方法

2022-04-03 06:07:43 字數 1819 閱讀 5799

結構:

增: create database 庫名 charset 字符集;

create table 表名稱(欄位名 型別 約束 ,欄位名 型別 約束)

not null 非空

primary key 主鍵 唯一且非空

unque 唯一約束 唯一可為空

foreign key 外來鍵約束 可為空,非空值與主鍵對應

刪: drop database 庫名;

drop table 表名;

改: alter table 表名 add 欄位名 型別 約束;

drop 欄位名;

change 舊欄位名 新欄位名 型別;

modify 欄位名 型別 約束;

remove 新錶名

查: desc 表名;檢視表結構

show tables;檢視所有表

show datebases;檢視所有庫

show create datebase 庫名稱; 檢視指定庫屬性

show create table 表名稱;檢視指定表屬性

增 insert into 表名 values(「」,「」~~)

刪 delete from 表名 where ~~

改 update 表名 set 字段=新值 where ~~

查 select * from * where ~~

跳x行取y行

select * from * limit(x,y)

select * from * limit y offset x

內連線:

select * from 表a inner join 表b on(條件)

左連線 select * from 表a left outer join 表b on(條件)

右連線 select * from 表a right outer join 表b on(條件)

聯合查

select concat(str ,str) 直接

select concat_ws(sub,str ,str) sub為隔離符號

select group_concat(str,str) 逗號隔離

常用查詢

select database(); 查詢當前資料庫;

select user(); 查詢當前使用者;

select version 或@@ version 查詢資料庫版本;

字元處理

select left(str,x);左邊取x位

select right(str,x); 右邊取x位

select lower (str );轉小寫

select upper (str ); 轉大寫

select length(str);輸出長度

substr (str,x,y); str 的x位開始取y位

ltrim rtrim trim 去空格

reverse(str); 翻轉

取日期currdata();當前日期

currtime (); 當前時間

date()取日期

time() 取時間

hour()取小時

minute()取分鐘

month()取月份

now()當前日期

dayofweek() 取星期幾

資料查詢語言 dql

資料操縱語言 dml

資料定義語言 ddl

資料控制語言 dcl

注釋:#~ 單行注釋 /* */多行注釋 -- 單行注釋 必須—後加空格

sql語句like的使用方法

在sql結構化查詢語言中,like語句有著至關關鍵的資料。like語句的語法格式是 select from 表名 where 欄位名 like 相應值 子串 它主要是針對字元型字段的,它的作用是在乙個字元型字段列中檢索包括相應子串的。如果有乙個資料庫中有個表table1,在table1中有兩個字段,...

SQL語句distinct去重使用方法

很多時候資料庫查出來的資料會出現重複的情況。不過這些情況用distinct就可以解決。表a id name1a 2b3c 4b1a 表b province city 河北省石家莊市 河北省石家莊市 select distinct name from a 執行之後的結果 nameab c例項1 sel...

ifdef 語句使用方法

ifdef expression 標識 execution1 待執行語句1 elif else 語句可以省略 execution2 待執行語句2 else execution3 待執行語句3 endif ifndef expression 標識 expression4 endif 下面舉幾個例子 例...