SQL簡單基本語法

2021-06-04 02:14:52 字數 2414 閱讀 7784

一、select子句

select語句用於從表中選取資料,結果被儲存在乙個結果表中(稱為結果集),其一些基本語法如下:

select 列名稱 from 表名稱

表示從指定的表中選取指定列的資料。

select * from 表名稱

)是選取所有列的快捷方式。

二、distinct關鍵字

用於表示返回唯一不同的值,即不重複的值。使用語法:

select distinct 列名稱 from 表名稱

表示從指定表中選取指定列的值,並且重複的值歸結成乙個。

三、where子句

如需有條件地從表中選取資料,可將where

子句新增到

select

語句。語法如下:

select 列名稱 from 表名稱 where 列 運算子 值

下面的運算子可在where

子句中使用:

操作符描述等於

不等於大於

小於大於等於

小於等於

between

在某個範圍內

like

搜尋某種模式

注釋:在某些版本的sql

中,操作符 

<> 

可以寫為 !=。

如果只希望選取居住在城市 「beijing」 

中的人,我們需要向

select

語句新增

where

子句:

select * from persons where city=』beijing』

注釋:sql

使用單引號來環繞文字值(大部分資料庫系統也接受雙引號)。如果是數值,請不要使用引號。 

select * from persons where age>30

四、and 和 or 運算子

and 和 

or 可在 

where 

子語句中把兩個或多個條件結合起來。以兩個條件作為例子:

select * from 表名稱 where 條件一 and 條件二

select * from 表名稱 where 條件一 or 條件二

五、order by 語句

order by 語句用於根據指定的列,對結果集進行排序,預設按照公升序對記錄進行排序。例項如下:

1、以字母順序顯示公司名稱:

select company from orders order by company

2、以逆字母順序顯示公司名稱,並以數字順序顯示順序號:(desc

表示逆序,

asc表示順序)

select company, ordernumber from orders order by company desc, ordernumber asc

六、insert into 語句

insert into 語句用於向**中插入新的一行。

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

我們也可以指定所要插入資料的若干列:

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

七、update 語句

update 語句用於修改表中的資料。

update 表名稱 set 列名稱 = 新值 where 列名稱 = 某值

更新某一行中的若干列:

update 表名稱 set 列1 = '值1', 列2 = '值2' where 列3 = '值3'

八、delete 語句

delete 語句用於刪除表中的行。

delete from 表名稱 where 列名稱 = 值

刪除所有的行:可以在不刪除表的情況下刪除所有的行。這意味著表的結構、屬性和索引都是完整的。

delete * from 表名稱

SQL基本語法

update 有關update,急!在oracle資料庫中 表 a id firstname,lastname 表 b id,lastname 表 a 中原來id,firstname兩個欄位的資料是完整的 表 b中原來id,lastname兩個欄位的資料是完整的 現在要把表 b中的lastname欄...

SQL基本語法

1.select列名稱 from 表名稱 2.selectdistinct 列名稱 from 表名稱 3.select列名稱 from 表名稱 where 列 運算子值 eg select frompersons where city beijing select from persons wher...

SQL基本語法

重要的ddl語句 create database 建立新資料庫 alter database 修改資料庫 create table 建立新錶 alter table 變更 改變 資料庫表 drop table 刪除表 create index 建立索引 搜尋鍵 drop index 刪除索引 1.s...