一、条件查询SELECT
SELECT用于从数据表中选取某些数据
DISTINCT在输出结果中去除重复数据
WHERE用于设定选取数据的条件,使选取的数据满足某个(些)条件
Order by用于对筛选结果排序,升序ASC,降序DESC,默认为升序
下面给出一个规则来将上面4个元素连在一起:
SELECT DISTINCT column_name(s)
FROM table_name WHERE column_name operator valueOrder by column_name(s) ASC|DESC
从表中选取满足某个条件的包含某些列的记录集,对筛选结果排序并且保证每条记录唯一(即去除了重复的记录)。
SQL使用单引号(single quotes) 将文本值括起(多数数据库系统也接受双引号);数字值不应该使用引号括起。
SELECT * FROM Persons WHERE FirstnName=”Tove”
SELECT * FROM Persons Where Year=1987
WHERE从句中使用的操作符:
Operator | Description |
= | Equal |
<> | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
BETWEEN | Between an inclusive range |
LIKE | Search for a pattern |
IN | If you know the exact value you want to return for at least one of the columns |
Note: In some versions of SQL the <> operator may be written as !=
二、 插入语句INSERT INTO
INSERT INTO语句用于向表中插入记录集
INSERT INTO语句语法规则
1) 语句中只指定表名,不指定列名
INSERT INTO table_name
VALUES (value1, value2, value3,…)
2) 插入语句中同时指明列名,用于仅向指定列中插入数据
INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)
三、更新表中记录UPDATE
UPDATE用于更新表中的某些记录集
UPDATE语句语法规则:
UPDATE table_name
SET column1=value, column2=value2,... WHERE some_column=some_value如果未指定WHERE条件,所有的记录都将被更新
四、删除表中记录DELETE
DELETE FROM table_name
WHERE some_column=some_value如果想仅清空某个表中的所有记录,但保留该表,也就是说保留标的结构,属性,索引等。
DELETE FROM table_name
或者 DELETE * FROM table_name在做删除时一定要小心,因为此操作不可逆。
参考资料: