一 SQL基本語法

2022-03-11 01:16:26 字數 3164 閱讀 7220

1:select 列名稱 from 表名稱  select用法

2:select distinct 列名稱 from 表名稱 distinct用法

3:select * from persons where city='beijing' where用法

4:select * from persons where firstname='thomas' and lastname='carter' and or 用法

5:select company, ordernumber from orders order by company order by 語句

6:insert into persons values ('gates', 'bill', 'xuanwumen 10', 'beijing') 插入語句

7:update person set address = 'zhongshan 23', city = 'nanjing'

where lastname = 'wilson' // update用法

8:delete from person where lastname = 'wilson' delect刪除命令

9:top

select *

from persons

where rownum <= 5

select * from student where rownum < 5

10:sql like 操作符

select * from persons

where city not like '%lon%' //從 "persons" 表中選取居住在不包含 "lon" 的城市裡的人:

11:sql 萬用字元

12:in 操作符

select * from persons

where lastname in ('adams','carter')

13:between

select * from persons

where lastname

between 'adams' and 'carter'

14:alias(別名)

select sname as '姓名' , sage from student;

//oracle as 別名會報錯

在oracle中as關鍵字不能用於指定表的別名,在oracle中指定表的別名時只需在原有表名和表的別名之間用空格分隔即可,

select sname 姓名,sage 出生時間 from student; //oracle直接用空格分開

15:join

• join: 如果表中有至少乙個匹配,則返回行

• left join: 即使右表中沒有匹配,也從左表返回所有的行

• right join: 即使左表中沒有匹配,也從右表返回所有的行

• full join: 只要其中乙個表中存在匹配,就返回行

16: union 操作符用於合併兩個或多個 select 語句的結果集。

請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。

17: sql select into 語句可用於建立表的備份復件。

18: create database database_name

19: 建立表

create table person(

id_p int primary key,

lastname varchar(20),

fristname varchar(20),

adress varchar(20),

city varchar(20)

)20:sql 約束

not null

unique unique 約束唯一標識資料庫表中的每條記錄。

primary key primary key 約束唯一標識資料庫表中的每條記錄。

foreign key

check check 約束用於限制列中的值的範圍。

default default 約束用於向列中插入預設值。

21:索引

create index 語句用於在表中建立索引。

create index personindex on person (lastname)

22:drop

通過使用 drop 語句,可以輕鬆地刪除索引、表和資料庫。

我們可以使用 drop index 命令刪除**中的索引。

drop index index_name //oracle

23:alter table 語句

alter table 語句用於在已有的表中新增、修改或刪除列。

24: auto increment

//您必須通過 sequence 對建立 auto-increment 字段(該物件生成數字序列)。

create sequence seq_person

minvalue 1

start with 1

increment by 1

cache 10

insert into persons (p_id,firstname,lastname)

values (seq_person.nextval,'lars','monsen')

25:sql view(檢視)

(1) 建立檢視 create view student_view as select * from student;

(2) 查詢檢視 select * from student_view

(3) sql 更新檢視

create view [current product list] as

select productid,productname,category

from products

where discontinued=no

(4) sql 撤銷檢視

sql drop view syntax

drop view view_name

26:null 值

27:oracle 沒有 isnull() 函式。不過,我們可以使用 nvl() 函式達到相同的結果:

select productname,unitprice*(unitsinstock+nvl(unitsonorder,0))

from products

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...