筆記 常用SQL語句

2021-07-15 03:29:06 字數 3007 閱讀 4371

建立表

create

table

ifnot

exists

"userinfo" ("tag"

integer

primary

keynot

null

unique , "userid" text not

null

unique , "password" text not

null , "name" text)

插入資料

insert

into

"userinfo" (userid,password, name) values ("1373814456","gdgh131","xupan")

刪除記錄

delete 語句用於刪除表中的記錄(行)。

delete

from table_name

where some_column=some_value;

請注意 sql delete 語句中的 where 子句!

where 子句規定哪條記錄或者哪些記錄需要刪除。如果您省略了 where 子句,所有的記錄都將被刪除!

delete

from userinfo where name = 'guanyu'

and userid = '2242352103'

刪除所有資料

也可以在不刪除表的情況下刪除表中的所有行。即表的結構、屬性、索引將保持不變:

delete

from table_name;

ordelete * from table_name;

在刪除記錄時要格外小心!因為您不能重來!!!!

改動記錄

注意:sql update語句中的where子句!

where子句規定哪條記錄或者哪些記錄需要更新。如果您省略了where子句,所有記錄都將被更新!!!!!!!

update userinfo set password = 'vvbb9393' ,name = '微塵'

where userid = '1373814735'

//  查詢全部記錄

select * from userinfo

// 查詢指定列

select userid from userinfo

select

distinct name from userinfo

// 返回所有name列包含「xupan」的記錄

// sql 使用單引號來環繞文字值(大部分資料庫系統也接受雙引號)。

// 這個例項中 'xupan' 文字字段使用了單引號。

// 如果是數值字段,請不要使用引號。

select * from userinfo where name = 'xupan'

where子語句中可使用的其它運算子:

運算子描述=等於

<>

不等於(在sql的一些版本中,該操作符可被寫成!=)

>

大於<

小於『>=』

大於等於

<=

小於等於

between

在某個範圍內

like

搜尋某種模式

in指定針對某個列的多個可能值

刪除表

drop table 語句用於刪除表:

drop

table table_name

and & or

如果第乙個條件和第二個條件都成立,則 and 運算子顯示一條記錄。

如果第乙個條件和第二個條件中只要有乙個成立,則 or 運算子顯示一條記錄。

select * from userinfo where name = 'xupan'

and tag > 2

select * from userinfo where name = 'xupan'

or name = '微塵'

也可以把 and 和 or 結合起來(使用圓括號來組成複雜的表示式)

order by

order by 關鍵字用於對結果集按照乙個列或者多個列進行排序。

order by 關鍵字預設按照公升序對記錄進行排序。如果需要按照降序對記錄進行排序,您可以使用 desc 關鍵字。

select column_name,column_name

from table_name

order

by column_name,column_name asc|desc;

//  根據tag列的值排序,「desc」表示降序排列,「asc」表示公升序(預設)

select * from userinfo order

by tag desc

// 現根據name排,再根據tag排

select * from userinfo order

by name, tag

常用SQL操作語句筆記

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...

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語句

t sql語句複製表的方法 我在sql server 2000中有現個資料庫datahr及demo,它們的結構是一樣,其它有乙個表名為 gbitem.現在我想將demo資料庫的表名 gbitem的全部內容複製到datahr資料庫的表名為 gbitem中。請問此t sql語句應該怎麼寫?謝謝高人指點!...