SQL Comment:: - (2 Dash and A Space) - This Is A Comment
SQL Comment:: - (2 Dash and A Space) - This Is A Comment
SQL Comment: - -
(2 dash and a space)
- - This is a comment
一些最重要的 SQL 命令
SELECT - 从数据库中提取数据
SELECT column1, column2, ...FROM table_name;
SELECT * FROM table_name;
SELECT DISTINCT - 如果有 91 个国家但有重复的名字 用于去掉重复的
SELECT DISTINCT column1, column2, ... FROM table_name;
WHERE - SELECT column1, column2, ...FROM table_name WHERE condition;
The WHERE clause is not only used in SELECT statements, it is also used
in UPDATE, DELETE, etc.!
AND - 如果由 AND 分隔的所有条件都为真,AND 运算符将显示一条记录
SELECT column1, column2, ...FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR - 如果由 OR 分隔的任何条件为 TRUE,则 OR 运算符显示一条记录
SELECT column1, column2, ... FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT - 如果条件不为真,则 NOT 运算符显示一条记录
SELECT column1, column2, ... FROM table_name
WHERE NOT condition;
ORDER BY - SELECT column1, column2, ... FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
(升序 a-z 也可以直接不写 ASC;降序 z-a——DESC)
BY Several Columns
SELECT * FROM Customers
ORDER BY Country, CustomerName;
UPDATE - 更新数据库中的数据
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE - 从数据库中删除数据
DELETE FROM customers WHERE Name = 'Jane';
INSERT INTO - 将新数据插入数据库
1. INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
2. INSERT INTO table_name
VALUES (value1, value2, value3, ...);
NULL - SELECT column_names FROM table_name
W HERE column_name IS (NOT) NULL;
MIN/MAX - SELECT MIN/MAX(column_name)
FROM table_name
WHERE condition;
COUNT, AVG ,SUM - SELECT COUNT/AVG/SUM(column_name)
FROM table_name
WHERE condition;
LIKE - 在 WHERE 子句中用于搜索列中的指定模式
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
* Represents zero or more characters bl* finds bl, black, blue, and blob
[] Represents any single character within the brackets h[oa]t finds hot and hat, but not hit
! Represents any character not in the brackets h[!oa]t finds hit, but not hot and hat
- Represents any single character within the specified range c[a-b]t finds cat and cbt
# Represents any single numeric character 2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295
% Represents zero or more characters bl% finds bl, black, blue, and blob
[] Represents any single character within the brackets h[oa]t finds hot and hat, but not hit
^ Represents any character not in the brackets h[^oa]t finds hit, but not hot and hat
- Represents any single character within the specified range c[a-b]t finds cat and cbt
Cr eating Tables:
CREATE TABLE customers (ID,Name,Gender,DOB);
INSERT INTO customers VALUES (01, 'Mary', 'F', '01/01/01');
INSERT INTO customers VALUES (02, 'Jane', 'F', '02/02/02');
INSERT INTO customers VALUES (03, 'Joe', 'M', '03/03/03');
SELECT * FROM customers
Execute:
1|Mar y|F|01/01/01
2|J ane|F|02/02/02
3|J oe|M|03/03/03
Change:
SELECT ID FROM customers;
Execute:
1
2
3
Get the second customer ’s infor mation:
Change SELECT * FROM customers;
to: SELECT * FROM customers WHERE ID=02;
To get distinct data (some customer s’ infor mation can be the same with gender , name,
DOB, etc:
CREATE TABLE customers (ID,Name,Gender,DOB);
INSERT INTO customers VALUES (01, 'Mary', 'F', '01/01/01');
INSERT INTO customers VALUES (02, 'Jane', 'F', '02/02/02');
INSERT INTO customers VALUES (03, 'Joe', 'M', '03/03/03');
INSERT INTO customers VALUES (04, 'Jane', 'M', '03/03/03');
SELECT * FROM customers WHERE Name='Jane' AND Gender='M';
-- Q2. Show all the employee info if his/her salary is greater or equal to 100000.
SELECT*FROM Employee WHERE SALARY >= 100000;
-- Q3. Show all the employee info if his/her first name’s initial is ‘V’.
SELECT*FROM Employee WHERE FIRST_NAME LIKE 'V%';
-- Q4. Show all the employee info if he/she joined the company before 2018-01-01.
SELECT*FROM Employee WHERE STARTING_DATE < '2018-01-01';
-- Q5. Display only the names of employee who is in the Account Department.
SELECT FIRST_NAME, LAST_NAME FROM Employee WHERE DEPARTMENT =
'Account';
-- Q6. Display the average salary among all the Admin employees.
SELECT AVG(SALARY) FROM Employee WHERE DEPARTMENT = 'Admin';
-- Q7. Display the total salary for Admin Department.
SELECT SUM(SALARY) FROM Employee WHERE DEPARTMENT = 'Admin';
-- Q8. Display the whole table using ascending order by employee’s last name (A - Z).
SELECT * FROM Employee ORDER BY LAST_NAME ASC;
-- Q9. Update the staring date of the employee who named Johnson to 2020-06-11.
UPDATE EMPLOYEE SET STARTING_DATE = '2020-06-11' WHERE FIRST_NAME =
'Johnson';
-- Q10. Remove all HR department employees from the table.
DELETE FROM Employee WHERE DEPARTMENT