Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

SQL Comment:: - (2 Dash and A Space) - This Is A Comment

SQL is a language used to manage databases and retrieve data from databases. Some key things SQL is used for include executing queries against a database, retrieving data from a database, inserting, updating, and deleting records from a database. Popular SQL commands include SELECT, UPDATE, DELETE, INSERT INTO, WHERE, ORDER BY, GROUP BY and JOIN. SQL is commonly used with relational database management systems like MySQL, SQL Server, Oracle, and PostgreSQL.

Uploaded by

skylarzhang66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

SQL Comment:: - (2 Dash and A Space) - This Is A Comment

SQL is a language used to manage databases and retrieve data from databases. Some key things SQL is used for include executing queries against a database, retrieving data from a database, inserting, updating, and deleting records from a database. Popular SQL commands include SELECT, UPDATE, DELETE, INSERT INTO, WHERE, ORDER BY, GROUP BY and JOIN. SQL is commonly used with relational database management systems like MySQL, SQL Server, Oracle, and PostgreSQL.

Uploaded by

skylarzhang66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL → Structured Query Language (Sequal)

● Used for database management 用于数据库管理


● Connects front-end to back-end 连接前端和后端
● Execute queries against a database 对数据库执行查询
● Retrieve data from a database 从数据库中检索数据
● Insert records in a database 在数据库中插入记录
● Update records in a database 更新数据库中的记录
● Delete records from a database 从数据库中删除记录
● Create new databases 创建新数据库
● Create new tables in a database 在数据库中创建新表
● Create stored procedures in a database 在数据库中创建存储过程
● Create views in a database 在数据库中创建视图
● Set permissions on tables, procedures, and views 设置对表、过程和视图的权限

Client end (front) → server side (back)


2 types of developer
- Front-end developer (UI/UX)
- Back-end developer
People who knows both end is the → Full-Stack developer

Popular database: MySQL, SQL Server, MS Access,


Oracle, Sybase

SQL keywords are NOT case sensitive

GDPR - Gener al Data Pr otection Regulation


Database - PHP or ASP
Syntax - SELECT * FROM Customer s; * --> univer sal selector (select ever ything) Customer
→ The table’s name

— Ever y line is considered as one quer y 每一行都被视为一个查询


— Each query ends with a semicolon; 以分号结束
— Don’t have to capital the select (this language is not case-sensitive) 不必大写
— Although SQL is an ANSI/ISO standar d, there are differ ent

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 a single character h?t finds hot, hat, and hit

[] 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 a single character h_t finds hot, hat, and hit

[] 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

1. SELECT * FROM Customers WHERE City LIKE ‘[a-f]%’;


2. SELECT * FROM Customers WHERE City LIKE ‘[!acf]%’;
3. SELECT * FROM Customers WHERE City LIKE ‘[acf]%’;
IN - 允许您在 WHERE 子句中指定多个值, 是多个 OR 条件的简写(either " " or " ".
SELECT column_name(s)
FROM table_name
WHERE column_name (NOT) IN (value1, value2, ...);
BETWEEN - 运算符选择给定范围内的值, 这些值可以是数字、文本或日期
SELECT column_name(s)
FROM table_name
WHERE column_name (NOT) BETWEEN value1 AND value2;
AS - SQL 别名用于为表或表中的列提供一个临时名称,别名仅在该查询期间存在,
使用 AS 关键字创建别名
1. SELECT column_name AS alias_name
FROM table_name;
2. SELECT column_name(s)
FROM table_name AS alias_name;
JOIN - 用于根据它们之间的相关列组合来自两个或多个表的行
(INNER) J OIN: Returns records that have matching values in both tables 返回在两个表中都具有
匹配值的记录
SELECT column_name(s) FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
LEFT (OUTER) J OIN: Returns all records from the left table, and the matched records from the
right table 返回左表中的所有记录,以及右表中匹配的记录
SELECT column_name(s) FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
RIGHT (OUTER) J OIN: Returns all records from the right table, and the matched records from
the left table 返回右表中的所有记录,以及左表中匹配的记录
SELECT column_name(s) FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
FULL (OUTER) J OIN: Returns all records when there is a match in either left or right table 当左
表或右表中存在匹配项时返回所有记录
SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name WHERE condition;

UNION - 运算符用于组合两个或多个 SELECT 语句的结果集(ALL:允许重复


SELECT column_name(s) FROM table1
UNION (ALL)
SELECT column_name(s) FROM table2;
GROUP BY - 语句将具有相同值的行分组到汇总行中,例如“查找每个国家/地区的客户数
量”。通常与聚合函数(COUNT()、MAX()、MIN()、SUM()、AVG())一起
使用,以按一列或多列对结果集进行分组。
SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

CREATE DATABASE - 创建一个新的数据库


ALTER DATABASE - 修改数据库
CREATE TABLE - 创建一个新表
ALTER TABLE - 修改表
DROP TABLE - 删除一个表
CREATE INDEX - 创建一个索引(搜索键)
DROP INDEX - 删除索引

Select all the different values


从 Customers 表的 Country 列中选择所有不同的值。
SELECT DISTINCT Country FROM Customers;

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';

SELECT City,PostalCode FROM Customers;


→ grouping selector (select multiple things together)

Cr eate Inser t Fetch


-- create a table
CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL,
gender TEXT NOT NULL);
-- insert some values
INSERT INTO students VALUES (1, 'Ryan', 'M');
INSERT INTO students VALUES (2, 'Joanna', 'F');
-- fetch some values
SELECT * FROM students WHERE gender = 'F';
Execute: 2|J oanna|F

Delete data fr om the table:


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');
DELETE FROM customers WHERE Name = 'Jane';
SELECT * FROM customers;
Execute:
1|Mar y|F|01/01/01
3|J oe|M|03/03/03

Only delete the male J ane:


Change DELETE FROM customers WHERE Name = 'Jane';
DELETE FROM customers WHERE Name = 'Jane' AND Gender='M';
-- Q1. Create a database table as shown above.
CREATE TABLE Employee (
WORKER_ID INT,
FIRST_NAME CHAR(20),
LAST_NAME CHAR(20),
SALARY INT,
STARTING_DATE datetime,
DEPARTMENT CHAR(20)
);

INSERT INTO Employee (WORKER_ID, FIRST_NAME, LAST_NAME, SALARY,


STARTING_DATE, DEPARTMENT) VALUES
(001, 'Monika', 'Arora', 100000, '2011-02-20', 'HR'),
(002, 'Johnson', 'Verma', 80000, '2019-06-11', 'Admin'),
(003, 'Vis', 'Singhal', 300000, '2018-02-20', 'HR'),
(004, 'Sally', 'Sima', 500000, '2014-02-20', 'Admin'),
(005, 'Vivek', 'Bhati', 500000, '2016-06-11', 'Admin'),
(006, 'Shane', 'Diwan', 200000, '2014-06-11', 'Account'),
(007, 'Santo', 'Kumar', 75000, '2014-01-20', 'Account'),
(008, 'Giann', 'Chauhan', 90000, '2015-04-11', 'Admin');

-- 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

You might also like