PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK - 保证列中的值符合指定的条件。
DEFAULT - 规定没有给列赋值时的默认值。
自增字段
AUTO INCREMENT
1 2 3 4 5 6 7 8 9
CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (ID) )
--type1 CASE <表达式> WHEN <值1> THEN <操作> WHEN <值2> THEN <操作> ... ELSE <操作> END --type2 CASE WHEN <条件1> THEN <命令> WHEN <条件2> THEN <命令> ... ELSE commands END
# Write your MySQL query statement below select t.business_id from (select *, avg(E1.occurences ) over(partition by E1.event_type ) as ave_occu from Events E1 ) t where t.occurences > t.ave_occu group by t.business_id having count(t.business_id)>=2
4 判断
1 in
(E1.id , E1.month) in ((1,8))
2 BETWEEN AND
BETWEEN ‘2019-06-28’ AND ‘2019-07-27’
3 EXISTS
用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False。
1 2 3 4
SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
WITH expression_name (column_list) AS ( -- Anchor member initial_query UNION ALL -- Recursive member that references expression_name. recursive_query ) -- references expression name SELECT * FROM expression_name
SELECT UserID,ManagerID,Name,Name AS ManagerName FROM dbo.Employee WHERE ManagerID=-1
userid
managerid
name
managername
1
-1
boss
boss
R1:
1 2 3
SELECT c.UserID,c.ManagerID,c.Name,p.Name AS ManagerName FROM CTE P INNERJOIN dbo.Employee c ON p.UserID=c.ManagerID
此时Employee为完整的,cte为
userid
managerid
name
managername
1
-1
boss
boss
得到结果为
c.userid
c.managerid
c.name
c.managername
p.userid
p.managerid
p.name
p.managername
11
1
A1
A1
1
-1
boss
boss
12
1
A2
A2
1
-1
boss
boss
13
1
A3
A3
1
-1
boss
boss
1
SELECT c.UserID,c.ManagerID,c.Name,p.Name AS ManagerName
userid
managerid
name
name
11
1
A1
boss
12
1
A2
boss
13
1
A3
boss
R2,R3…
最后union all R0,R1,R2,。。。
16 select 常数
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT 1,-1,N'Boss' UNION ALL SELECT 11,1,N'A1' UNION ALL SELECT 12,1,N'A2' UNION ALL SELECT 13,1,N'A3' UNION ALL SELECT 111,11,N'B1' UNION ALL SELECT 112,11,N'B2' UNION ALL SELECT 121,12,N'C1'
sum(frequency) over(partiton by num ) --分组累加 sum(frequency) over() --total_frequency 全部累加 sum(frequency) over(order by num desc) --desc_frequency, 逆序累加 sum(frequency) over(order by num asc) --asc_frequency,正序累加 SUM(Salary) OVER (PARTITION BY Id ORDER BY Month asc range 2 PRECEDING) --range 2 PRECEDING 当前以及前面2行
avg(frequency) over() --total_frequency ,全部平均 avg(frequency) over(order by num desc) --desc_frequency, 逆序平均 avg(frequency) over(order by num asc) --asc_frequency, 正序平均
select distinct C1.seat_id as seat_id from Cinema C1 join Cinema C2 on C1.free=1 and C2.free=1 and abs(C1.seat_id-C2.seat_id)=1 order by seat_id
power
1 2 3 4
# Write your MySQL query statement below select round(min(Power(Power(P1.x-P2.x,2)+Power(P1.y-P2.y,2),0.5)),2) as shortest from Point2D P1 join Point2D P2 on P1.x!=P2.x or P1.y!=P2.y