数据库基本操作
作者:互联网
Websites数据表如下
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SELECT 语句用于从数据库中选取数据
SELECT column_name,column_name FROM table_name;
SELECT * FROM table_name;
SELECT DISTINCT 语句用于返回唯一不同的值
SELECT DISTINCT column_name, column_name FROM table_name;
WHERE 子句用于过滤记录
SELECT column_name,column_name FROM table_name WHERE column_name <operator> value;
# 例
SELECT * FROM Websites WHERE country='CN';
WHERE 子句中的运算符
运算符 | 描述 |
---|---|
= | 等于 |
<> | 不等于。注释:在 SQL 的一些版本中,该操作符可被写成 != |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
BETWEEN | 在某个范围内 |
LIKE | 搜索某种模式 |
IN | 指定针对某个列的多个可能值 |
AND & OR 运算符用于基于一个以上的条件对记录进行过滤
SELECT * FROM Websites WHERE country='CN' AND alexa > 50;
SELECT * FROM Websites WHERE country='USA' OR country='CN';
SELECT * FROM Websites WHERE alexa > 15 AND (country='CN' OR country='USA');
ORDER BY 关键字用于对结果进行排序
默认升序排列,可以使用 DESC 关键字进行降序排列
SELECT column_name,column_name FROM table_name ORDER BY column_name;
# 例
SELECT * FROM Websites ORDER BY alexa;
SELECT * FROM Websites ORDER BY alexa DESC;
INSERT INTO 语句用于向表中插入新纪录
INSERT INTO table_name (column1,column2,...)
VALUES (value1,value2,...);
# 例
INSERT INTO Websites (name, url, alexa, country)
VALUES ('百度', 'https://www.baidu.com/', '4', 'CN');
UPDATE 语句用于更新表中的记录
UPDATE table_name
SET column1=value1, column2=value2,...
WHERE some_column=some_value;
# 例
UPDATE Websites
SET alexa='5000',country='USA'
WHERE name='菜鸟教程'
更新记录时需要格外小心!如果省略了WHERE语句,所有的记录都会被更新。
DELETE 语句用于删除表中的记录
DELETE FROM table_name
WHERE some_column=some_value;
# 例
DELETE FROM Websites
WHERE name='Facebook' AND country='USA';
删除记录时需要格外小心!如果省略了WHERE语句,所有的记录都会被删除。
标签:name,column,country,数据库,Websites,基本操作,WHERE,SELECT 来源: https://www.cnblogs.com/augustine0654/p/15040304.html