一、结构定义
1、CREATE TABLE
创建新表。
语法:
1
2
3
4
5
6
7
|
sql-command ::=
CREATE
[
TEMP
|
TEMPORARY
]
TABLE
table
-
name
(
column
-def [,
column
-def] * [,
constraint
] *)
sql-command ::=
CREATE
[
TEMP
|
TEMPORARY
]
TABLE
[
database
-
name
.]
table
-
name
AS
select
-statement
column
-def ::=
name
[type] [[
CONSTRAINT
name
]
column
-
constraint
]*
type ::= typename | typename (number) | typename (number,number)
column
-
constraint
::=
NOT
NULL
[conflict-clause] |
PRIMARY
KEY
[sort-
order
] [conflict-clause] |
UNIQUE
[conflict-clause] |
CHECK
(expr) [conflict-clause] |
DEFAULT
value |
COLLATE
collation-
name
constraint
::=
PRIMARY
KEY
(
column
-list) [conflict-clause] |
UNIQUE
(
column
-list) [conflict-clause] |
CHECK
(expr) [conflict-clause]
conflict-clause ::=
ON
CONFLICT conflict
|
例子:
1
|
create
table
film(title,length,
year
,starring);
|
说明:
建立了一个名叫film的资料表,里面有name、length、year、starring四个字段。
2、CREATE VIEW
创建一个视图(虚拟表),该表以另一种方式表示一个或多个表中的数据。
语法:
1
|
sql-command ::=
CREATE
[
TEMP
|
TEMPORARY
]
VIEW
[
database
-
name
.]
view
-
name
AS
select
-statement
|
例子:
1
|
|
说明:
创建一个名为master_view的视图,其中包括sqlite_master这个表中的所有视图表。
3、CREATE TRIGGER
创建触发器,触发器是一种特殊的存储过程,在用户试图对指定的表执行指定的数据修改语句时自动执行。
语法:
1
2
3
4
5
6
7
8
|
sql-statement ::=
CREATE
[
TEMP
|
TEMPORARY
]
TRIGGER
trigger
-
name
[BEFORE |
AFTER
]
database
-event
ON
[
database
-
name
.]
table
-
name
trigger
-
action
sql-statement ::=
CREATE
[
TEMP
|
TEMPORARY
]
TRIGGER
trigger
-
name
INSTEAD
OF
database
-event
ON
[
database
-
name
.]
view
-
name
trigger
-
action
database
-event ::=
DELETE
|
INSERT
|
UPDATE
|
UPDATE
OF
column
-list
trigger
-
action
::= [
FOR
EACH ROW |
FOR
EACH STATEMENT] [
WHEN
expression]
BEGIN
trigger
-step; [
trigger
-step;]*
END
trigger
-step ::=
update
-statement |
insert
-statement |
delete
-statement |
select
-statement
|
例子:
1
2
3
4
|
CREATE
TRIGGER
update_customer_address
UPDATE
OF
address
ON
customers
BEGIN
UPDATE
orders
SET
address = new.address
WHERE
customer_name = old.
name
;
END
;
|
说明:
创建了一个名为update_customer_address的触发器,当用户更新customers表中的address字段时,将触发并更新orders表中的address字段为新的值。
比如执行如下一条语句:
1
|
UPDATE
customers
SET
address =
'1 Main St.'
WHERE
name
=
'Jack Jones'
;
|
1
|
UPDATE
orders
SET
address =
'1 Main St.'
WHERE
customer_name =
'Jack Jones'
;
|
4、CREATE INDEX
为给定表或视图创建索引。
语法:
1
2
|
sql-statement ::=
CREATE
[
UNIQUE
]
INDEX
index
-
name
ON
[
database
-
name
.]
table
-
name
(
column
-
name
[,
column
-
name
]*) [
ON
CONFLICT conflict-algorithm]
column
-
name
::=
name
[
COLLATE
collation-
name
] [
ASC
|
DESC
]
|
例子:
1
|
CREATE
INDEX
idx_email
ON
customers (email);
|
说明:
为customers表中的email创建一个名为idx_email的索引。
二、结构删除
1、DROP TABLE
删除表定义及该表的所有索引。
语法:
1
|
|
例子:
1
|
DROP
TABLE
customers;
|
2、DROP VIEW
删除一个视图。
语法:
1
|
|
例子:
1
|
DROP
VIEW
master_view;
|
3、DROP TRIGGER
删除一个触发器。
语法:
1
|
|
例子:
1
|
DROP
TRIGGER
update_customer_address;
|
4、DROP INDEX
删除一个索引。
语法:
1
|
|
例子:
1
|
DROP
INDEX
idx_email;
|
三、数据操作
1、INSERT
将新行插入到表。
语法:
1
|
sql-statement ::=
INSERT
[
OR
conflict-algorithm]
INTO
[
database
-
name
.]
table
-
name
[(
column
-list)]
VALUES
(value-list) |
INSERT
[
OR
conflict-algorithm]
INTO
[
database
-
name
.]
table
-
name
[(
column
-list)]
select
-statement
|
2、UPDATE
更新表中的现有数据。
语法:
1
2
|
sql-statement ::=
UPDATE
[
OR
conflict-algorithm] [
database
-
name
.]
table
-
name
SET
assignment [,assignment]* [
WHERE
expr]
assignment ::=
column
-
name
= expr
|
3、DELETE
从表中删除行。
语法:
1
|
|
4、SELECT
从表中检索数据。
语法:
1
2
3
4
5
6
7
8
9
10
|
sql-statement ::=
SELECT
[
ALL
|
DISTINCT
] result [
FROM
table
-list] [
WHERE
expr] [
GROUP
BY
expr-list] [
HAVING
expr] [compound-op
select
]* [
ORDER
BY
sort-expr-list] [LIMIT
integer
[(OFFSET |,)
integer
]]
result ::= result-
column
[,result-
column
]*
result-
column
::= * |
table
-
name
.* | expr [[
AS
] string]
table
-list ::=
table
[
join
-op
table
join
-args]*
table
::=
table
-
name
[
AS
alias] | (
select
) [
AS
alias]
join
-op ::=,| [NATURAL] [
LEFT
|
RIGHT
|
FULL
] [
OUTER
|
INNER
|
CROSS
]
JOIN
join
-args ::= [
ON
expr] [USING (id-list)]
sort-expr-list ::= expr [sort-
order
] [,expr [sort-
order
]]*
sort-
order
::= [
COLLATE
collation-
name
] [
ASC
|
DESC
]
compound_op ::=
UNION
|
UNION
ALL
|
INTERSECT
|
EXCEPT
|
5、REPLACE
用于替代INSERT的“INSERT OR REPLACE”变体,以更好的兼容MysqL。
语法:
1
|
sql-statement ::=
REPLACE
INTO
[
database
-
name
.]
table
-
name
[(
column
-list)]
VALUES
(value-list) |
REPLACE
INTO
[
database
-
name
.]
table
-
name
[(
column
-list)]
select
-statement
|
四、事务处理
1、BEGIN TRANSACTION
标记一个事务的起始点。
语法:
1
|
|
2、END TRANSACTION
标记一个事务的终止。
语法:
1
|
|
3、COMMIT TRANSACTION
标志一个事务的结束。
语法:
1
|
|
4、ROLLBACK TRANSACTION
将事务回滚到事务的起点。
语法:
1
|
|
五、其他操作
1、COPY
主要用于导入大量的数据。
语法: