SQL語句簡介
SQL的基本概念:支持SQL的RDBMS同樣支持關係數據庫三級模式結構,外模式(視圖、部分基本表),模式(基本表),內模式(存儲文件)。
數據定義:
- 定義模式:create schema <模式名> authorization <用戶名> (create schema “S-T” authorization wang 創建模式S-T並授權給管理員wang)
- 刪除模式:drop schema <模式名> < cascade | restrict > (drop schema "S-T" cascade 級聯刪除模式S-T) cascade表示在刪除模式的同時把該模式中的所有的數據庫對象全部一起刪除 restrict表示如果該模式中已經定義了下屬的數據庫對象則拒絕刪除語句的執行。
- 定義基本表:create table <表名> (<列明> <數據類型>[列級完整性約束條件],<列明> <數據類型>[列級完整性約束條件],...);建表的同時還可以定義與該表有關的完整性約束條件。例
create table Student (Sno char(9) primary key,Sname char(20) unique,Sage int);
- 修改基本表:alter table <表名> [add <新列名><數據類型>[完整性約束]][drop <完整性約束名>][alter cloumn<列名><數據類型>]
向Student表增加“入學時間”列,其數據類型爲日期型alter table Student add S_entrance date;
- 刪除基本表:drop table <表名> [ restrict | cascade ]
drop table Student cascade;
- 索引的建立及刪除:create [unique][cluster] index <索引名> on <表名> ( <表名> [ <次序> ],[ <列名> [ <次序> ] ], ...); drop index <索引名>
create unique index SCno on SC (Sno ASC,Cno DESC);
drop index SCno;
數據查詢:create [all|distinct] <目標表達式> [,<目標表達式>]
from <表名或視圖名> [,<表名或視圖名>]
[where <條件表達式>]
[group by <列名1> [,having <條件表達式>]]
[order by <列名2> [ASC|DESC]];
數據更新:insert
into <表名>[(<屬性列1> [,<屬性列2>...])]
values(<常量>[,<常量2>]...);
數據修改:update <表名>
set <列名>=<表達式>[,<列名>=<表達式>]...
[where <> ];
刪除數據:delete
from <表名>
[where <條件>];