SQL是什么?
官方解释:SQL (Structured Query Language:结构化查询语言) 是用于管理关系数据库管理系统(RDBMS)。
SQL能用来干什么?
通俗的讲:让您可以访问和处理数据库,包括数据插入、查询、更新和删除。
SQL 对于现在的互联网公司生产研发等岗位几乎是一个必备技能,如果不会 SQL 的话,可能什么都做不了。可以把 SQL 当做是一种工具,利用它可以帮助你完成你的工作,创造价值。
下面让我们看看小美是如何零基础学习SQL的:
使用这个SQL语句可以查看数据库当前时间,当然也可以把 SYSDATE 换成任意东西。
例如:
计算器:SELECT 365 * 24 FROM dual;
这个SQL我们在开发中会经常用到,作为入门第一个SQL轻松有趣。小美,你觉得难吗?
CREATE TABLE bookshelf
(
BOOK_ID NUMBER,
BOOK_NAME VARCHAR2(100),
BOOK_TYPE VARCHAR2(100),
AUTHOR VARCHAR2(100),
INTIME DATE
);
表名为:bookshelf
,有列:图书id,图书名称,图书类型,作者,入库时间。通过上面学习的 SELECT
语法,来查询一下这张表:
SELECT * FROM bookshelf;
可以发现,新建的bookshelf表没有任何记录。现在,图书馆里已经增加一个空的书架,是不是需要将书放入书架上呢?这时就需要用到 增 操作了。
INSERT INTO bookshelf
(book_id,
book_name,
book_type,
author,
intime)
VALUES
(1,
'飘',
'长篇小说',
'玛格丽特·米切尔',
SYSDATE);
COMMIT;
增 的基本语法:
insert into 表名 (需要插入的列名,用逗号隔开) values (对应列名的值);
通过sql查询发现,这本书《飘》已经放入了书架上,可供大家借用和查看。
改 的基本语法:
UPDATE 表名 SET 列名 = 新的值;
删 的基本语法:
DELETE FROM 表名;
现在来模拟一下场景:
1、修改作者名:
UPDATE bookshelf SET author='Margaret Mitchell';
COMMIT;
2、下架图书:
DELETE FROM bookshelf;
COMMIT;
通过以上两个场景,演示了 改 和 删 两种操作。
先上架3本书:
INSERT INTO bookshelf (book_id,book_name,book_type,author,intime) VALUES (1,'飘','长篇小说','玛格丽特·米切尔',SYSDATE);
INSERT INTO bookshelf (book_id,book_name,book_type,author,intime) VALUES (2,'倾城之恋','爱情小说','张爱玲',SYSDATE);
INSERT INTO bookshelf (book_id,book_name,book_type,author,intime) VALUES (3,'从你的全世界路过','短篇小说','张嘉佳',SYSDATE);
COMMIT;
查看《倾城之恋》:
SELECT * FROM bookshelf WHERE BOOK_NAME = '倾城之恋';
更新《飘》:
UPDATE bookshelf SET author='Margaret Mitchell' WHERE book_name = '飘';
COMMIT;
删除《从你的全世界路过》:
DELETE FROM bookshelf WHERE book_name = '从你的全世界路过';
COMMIT;
通过上面的几个栗子,应该能很好的理解 WHERE 查询条件的使用了。
文末,赠送给各位看官几个一句SQL画图的趣味小SQL:
⭐️ 五角星:
WITH a AS
(SELECT DISTINCT round(SUM(x) over(ORDER BY n)) x,
round(SUM(y) over(ORDER BY n)) y
FROM (SELECT n,
cos(trunc(n / 20) * (1 - 1 / 5) * 3.1415926) * 2 x,
sin(trunc(n / 20) * (1 - 1 / 5) * 3.1415926) y
FROM (SELECT rownum - 1 n
FROM all_objects
WHERE rownum <= 20 * 5)))
SELECT REPLACE(sys_connect_by_path(point,
'/'),
'/',
NULL) star
FROM (SELECT b.y,
b.x,
decode(a.x,
NULL,
' ',
'*') point
FROM a,
(SELECT *
FROM (SELECT rownum - 1 + (SELECT MIN(x)
FROM a) x
FROM all_objects
WHERE rownum <= (SELECT MAX(x) - MIN(x) + 1
FROM a)),
(SELECT rownum - 1 + (SELECT MIN(y)
FROM a) y
FROM all_objects
WHERE rownum <= (SELECT MAX(y) - MIN(y) + 1
FROM a))) b
WHERE a.x(+) = b.x
AND a.y(+) = b.y)
WHERE x = (SELECT MAX(x)
FROM a)
START WITH x = (SELECT MIN(x)
FROM a)
CONNECT BY y = PRIOR y
AND x = PRIOR x + 1;
注意: 调整期中数字 5,你还可以输出 7 角星,9 角星!
奥运五环:
WITH a AS
(SELECT DISTINCT round(a.x + b.x) x,
round(a.y + b.y) y
FROM (SELECT (SUM(x) over(ORDER BY n)) x,
round(SUM(y) over(ORDER BY n)) y
FROM (SELECT n,
cos(n / 30 * 3.1415926) * 2 x,
sin(n / 30 * 3.1415926) y
FROM (SELECT rownum - 1 n
FROM all_objects
WHERE rownum <= 30 + 30))) a,
(SELECT n,
(SUM(x) over(ORDER BY n)) x,
round(SUM(y) over(ORDER BY n)) y
FROM (SELECT n,
cos(m / 3 * 3.1415926) * 2 * 15 x,
sin(m / 3 * 3.1415926) * 15 y
FROM (SELECT CASE
WHEN rownum <= 2 THEN
3
WHEN rownum = 3 THEN
-2
ELSE
-6
END m,
rownum - 1 n
FROM all_objects
WHERE rownum <= 5))) b)
SELECT REPLACE(sys_connect_by_path(point,
'/'),
'/',
NULL) star
FROM (SELECT b.y,
b.x,
decode(a.x,
NULL,
' ',
'*') point
FROM a,
(SELECT *
FROM (SELECT rownum - 1 + (SELECT MIN(x)
FROM a) x
FROM all_objects
WHERE rownum <= (SELECT MAX(x) - MIN(x) + 1
FROM a)),
(SELECT rownum - 1 + (SELECT MIN(y)
FROM a) y
FROM all_objects
WHERE rownum <= (SELECT MAX(y) - MIN(y) + 1
FROM a))) b
WHERE a.x(+) = b.x
AND a.y(+) = b.y)
WHERE x = (SELECT MAX(x)
FROM a)
START WITH x = (SELECT MIN(x)
FROM a)
CONNECT BY y = PRIOR y
AND x = PRIOR x + 1;
打印当月日历:
SELECT MAX(decode(dow,
1,
d,
NULL)) sun,
MAX(decode(dow,
2,
d,
NULL)) mon,
MAX(decode(dow,
3,
d,
NULL)) tue,
MAX(decode(dow,
4,
d,
NULL)) wed,
MAX(decode(dow,
5,
d,
NULL)) thu,
MAX(decode(dow,
6,
d,
NULL)) fri,
MAX(decode(dow,
7,
d,
NULL)) sat
FROM (SELECT rownum d,
rownum - 2 + to_number(to_char(trunc(SYSDATE,
'MM'),
'D')) p,
to_char(trunc(SYSDATE,
'MM') - 1 + rownum,
'D') dow
FROM all_objects
WHERE rownum <=
to_number(to_char(last_day(to_date(SYSDATE)),
'DD')))
GROUP BY trunc(p / 7)
ORDER BY sun NULLS FIRST;
本文以轻松愉快的情景对话方式介绍 SQL 的 增删改查
操作!希望能给读者不一样的体验~
玩转 Linux: 《玩转 Vagrant 系列》
玩转 Oracle: 《Oracle 一键安装脚本》
安装 Oracle: 《Oracle 零基础安装》
运维小知识: 《每天一个DBA小知识》