常用的sql語句

2021-10-02 22:18:24 字數 2070 閱讀 7334

1.建立資料庫

create database 資料庫名稱
2.在資料庫下面,建立資料庫表

create table student

(userid int not null primary key,

lastname varchar

(255),

firstname varchar

(255)

)(**建立student資料庫表且設定userid列為主鍵**)

3.select 從資料庫表中獲取資料

select * from 表名
select 欄位名稱1,欄位名稱2 from 表名
4.update 更新資料庫表中的資料

update 表名 set 欄位名稱=「值」
5.delete(drop) 刪除資料庫表中的資料(刪除資料庫表)

delete from 表名 where (範圍)
刪除資料庫中的表

drop table 表名
刪除資料庫

drop database 資料庫名稱
6.insert into 向資料庫中插入資料

針對表中某些字段進行插入

insert into 表名 (欄位名稱1,欄位名稱2) values

(值1,值2)

整體向表中插入一行資料

insert into 表名 values(1,liwen,13718134001)(各列的資料都得寫)
7.distinct語句

當查詢結果出現重複資料時,保留不重複的資料,可以採用這個。單列 單列

select distinct 欄位名稱 from 表名
8.where 語句

a.將username為李文的所有資料查出來

select * from 表名 where username=「李文」
b.結果只顯示值為「李文」和「張三」的資料

select * from 表名 where age between 30 and 60
c.查詢表中username包含「文」的資料

select * from 表名 where username like "%文%"
d.查詢表中不包含「文」的資料

select * from 表名 where username not like "%文%"
9.order by(desc) 排序(最後加上desc是倒序)

以createtime(建立時間)進行排序

select * from 表名 order by createdate
以createtime(建立時間)進行倒序排序

select * from 表名 order by createtime desc
10.萬用字元(%:乙個或多個字元;_:僅乙個字元;[charlist]:字元列中任何單一字元)

select * from 表名 where username like '%文'
查詢username以a開頭以b結尾的資料

select * from 表名 where username like 'a_b'
查詢username首字母以a或者b開頭的資料

select * from  表名 where username like '[ab]%'
11.in(in操作符允許where語句規定有多個值)

select * from 表名 where username in (

'張三','李四'

)

sql常用sql語句

1 查詢某個庫中所有的表名字 select name from sysobjects where xtype u and name dtproperties order by name 2 得到資料庫中所有使用者檢視 select name from sysobjects where xtype v...

sql 常用的語句

說明 複製表 只複製結構,源表名 a 新錶名 b sql select into b from a where 1 1 說明 拷貝表 拷貝資料,源表名 a 目標表名 b sql insert into b a,b,c select d,e,f from b sql select a.title,a....

常用的SQL語句

1.select語句語法 select語句的基本語法如下 select column1,column2,columnn from table name 這裡列1,列2.想獲取其值表的字段。如果想獲取在該字段的所有可用字段,那麼可以使用下面的語法 select from table name 2.in...