常用sql資料庫語言基本語句

2021-09-24 04:12:22 字數 2890 閱讀 2253

資料定義語句(ddl)

常用函式

注意事項

sql語句不區分大小寫

各條sql語句用";"分號斷句

select 列名 from 表名 where 條件語句;
注意:

顯示全部列可以用*表示

select * from 表名;
返回列中不重複的值,

select distinct 列名 from 表名;
where條件語句

select 列名 from 表名 where 列名 運算子 限定條件的內容

//下面為查詢user表中name為liming的資料

//* 表示滿足條件的全部列

// "liming"表示name型別為字串

select * from user where name=

"liming"

;//查詢year大於1965

select * from user where year>

2022

;//錯誤演示:因為year型別為int如果寫成year>"2022"則錯誤

select * from user where yera>

"2022"

;

運算子:

=等於

<>不等於

>大於

《小於》=大於等於

<=小於等於

//例如
like 某種模式

//例如查詢不包含li以外的資料

select * from user where name not like '%li%'

and 表示where後的全部條件表示式均滿足條件則where整體狀態為滿足條件

or 表示where後的全部條件表示式有乙個滿足條件則where整體狀態為滿足條件

//查詢user表中滿足name="yibo"並且year=2022的記錄

select * from user where name=

"yibo" and year=

2022

;//查詢user表中滿足name="yibo"或者year=2022或者兩個條件都滿足的記錄

select * from user where name=

"yibo" or year=

2022

;

基本常識:

asc 順序排序(預設)

desc 倒序排序

例子:

//1 將user表中的記錄按照score列值順序排序

select * from user order by score

//2 將user表中的記錄先按照name中的字母倒敘排序,如果字母相同再按score順序排序

select * from user order by name desc,score asc

//3 將user表中的記錄按照score列值從小到大倒敘排序

select * from user order by score desc

update 表名 set 列名=新值 where 條件語句

update user set name="xiaoming" where name="liming"

delect from 表名 where 表示式

delect from user where name=

"xiaoming"

//小技巧:刪除全部資料保留資料表結構

delect * from user

//刪除響應表中滿足表示式的資料

insert into 表名 (列名1,列名2,列名3...) values (值1,值2,值3...)

//insert into user (name,year) values ("bob",2019);

//建立乙個admin表,包括id,name,password列

creat table admin

( id int

, name varchar

(255),

password varchar

(255

))

在user表中新增age列並且型別為int

alter table user add age int;
使用alter刪除user表中year列

alter table user drop column year;
刪除指定表中資料內容,保留資料表結構

truncate table 表名
完全刪除表,結構+資料都刪除

drop table 表名

//完全刪除user表

drop table user

完全刪除資料庫

drop database 資料庫名

drop database student
三角函式

sin()

cos()

絕對值函式

abs()

select abs(-1)

//返回值為1

查詢字串長度包含空格

length()

select length("1 23")

//返回值為4

大小寫字母轉換

資料庫基本sql語句

建立資料庫 1 如果不存在就新建資料庫,使用utf 8編碼格式,預設使用utf8排序。2create database ifnot exists test default character setutf8 collate utf8 general ci3 刪除資料庫 4drop database ...

資料庫基本的sql語句

到test這個 上增加指定的幾條資訊 sql insert into test number,name,gender,age,colleage,city,country values 在test上根據id刪除資訊 刪除id為3 sql delete from test where id 3 編輯te...

資料庫管理常用sql語句

一 creating a database 1 以系統管理員使用者登入。sqlplus as sysdba 2 啟動資料庫導nomount狀態。sql startup nomount 3 執行建立資料庫語句。sql create database invrep controlfile reuse m...