Codecademy_SQL_Tutorial-Notes-Database
# SQL Tutorial
Tags: #Database #SQL #SQLite
- This course use SQLite
复习材料:
下面只叙述CheatSheet里面没有的内容
# Clauses
CREATE TABLE
is a clause. Clauses perform specific tasks in SQL. By convention, clauses are written in capital letters. Clauses can also be referred to as commands.
INSERT
, SELECT
etc. are also clauses.
中文: 子句?
# INSERT
- insert attribute 的顺序可以交换, 只要Tuple里面的顺序一一对应即可.
|
|
- Multiple tuples: Quick way:
|
|
# ALTER
Order:
# Question
In the context of this exercise 89, can we add a column at a specific position to a table?
# Answer
No, unfortunately, you cannot specify what position to add a column to a table.
By default, a new column will always be added at the end of the table. For most intents and purposes, this should not affect much, since you can always select the columns in any order, for instance, like
|
|
If column order is very important, then an alternative is to create a new table and add the columns in the specific order they should appear.
# DELETE
|
|
- 这里的
IS NULL
可以换成= NULL
吗?- 不可以, 没法删除
# SQLite
- In SQLite, a database is stored in a single file — a trait that distinguishes it from other database engines.
- Drawbacks
- Security and Data Integrity issue
- No Data-Type Verification
- Less advanced features
- SQLite’s maintainers consider it to be among the most replicated pieces of software in the world.
# Part 2 Queries
# AS
- AS不改变原来的relation
- 你可以用逗号分隔两个查询重命名的项:
|
|
# WHERE
- WHERE里面的相等是一个等号:
=
# LIKE
- wildcard:
_
-> a single character %
is a wildcard character that matches zero or more missing letters in the pattern.LIKE
is not case sensitive.- 如果要查找
_
或%
, 利用反斜杠表示转义字符:\_
\%
# Question
Can we apply the
LIKE
operator to values other thanTEXT
?# Answer
Yes, you can apply the
LIKE
operator to numerical values as well.Whenever you use
LIKE
however, you must always wrap the pattern within a pair of quotations, whether for matching a number or a string.# Example
1 2 3 4 5 6 7
/* This will select movies where the id number starts with 2 and is followed by any two numbers. */ SELECT * FROM movies WHERE id LIKE '2__';
# BETWEEN
|
|
这个范围是 [1990, 1999], 包括了1999,
但是如果是字符串的话, 就变得稍微有一点复杂: 看这个:
|
|
‘Jaw’ 会包括在里面吗? -> 不会 但是’J’会包括在里面
因为其实是这个顺序: ‘A’, ‘Aa’ …… ‘J’, ‘Ja’,……….. 到’J’那里就停下来了.
# CASE
|
|
- 注意第一行最后有一个逗号! ELSE不是必须的
|
|
CASE是从上向下匹配的, 所以第二行的不用再说明要小于4.5:
# Multiple Tables
# Inner Join
# LEFT JOIN
# 学完了
有三个网站来回顾学习内容: