티스토리 뷰
Referential Actions
ON DELETE CASCADE
ON DELETE NO ACTION
ON DELETE RESTRICT
ON DELETE SET NULL
ON DELETE DEFAULT
ON DELETE CASCADE
ON DELETE SET NULL
Referential Actions은 referencing하는 테이블에서 건다
CREATE TABLE employee(
empno int NOT NULL,
empname varchar(45),
title varchar(45) DEFAULT '사원',
manager int,
salary int,
dno int,
CONSTRAINT UQ_empname UNIQUE (empname),
CONSTRAINT CH_salary CHECK (salary < 6000000),
CONSTRAINT CH_dno CHECK (dno IN (1, 2, 3, 4, 5, 6)),
CONSTRAINT PK_Employee PRIMARY KEY (empno),
CONSTRAINT FK_Employee_Manager
FOREIGN KEY (manager) REFERENCES employee(empno),
CONSTRAINT FK_Department_Employee
FOREIGN KEY (dno) REFERENCES department(deptno) ON DELETE CASCADE
);
예시
employee의 dno가 department의 deptno를 referencing한다
여기다가 referential action을 쓴다
department의 detpno에 쓰진않음
Join Query
SELECT *
FROM employee, department
WHERE department.deptno = employee.dno;
Join Query(Example)
Q1: Show the countries and their cities
SELECT C.Name, T.Name
FROM Country AS C, City AS T
WHERE C.Code=T.CountryCode;
Q2: Show the countries located in Europe and their cities
SELECT C.Name, T.Name
FROM Country AS C, City AS T
WHERE C.Code=T.CountryCode AND C.Continent='Europe';
Q3: Show countries that speak at least 50% in Korean
SELECT C.Name, CL.Percentage
FROM Country AS C, CountryLanguage AS CL
WHERE C.Code=CL.CountryCode AND CL.Percentage>=50
AND CL.Language='Korean';
- Find the most popular official languages in the world
- Output result in a descending order of popularity
- Report the 5 most popular ones
SELECT Language, COUNT(CountryCode)
FROM countrylanguage
WHERE IsOfficial='T'
GROUP BY Language
ORDER BY COUNT(CountryCode) DESC
LIMIT 5;
Nested Query
When a scalar value(single value) is returned
Q1: Countries that have the same independence year with South Korea
SELECT Name, IndepYear
FROM Country
WHERE IndepYear =
(SELECT IndepYear
FROM Country
WHERE Name = 'South Korea');
Q2: Where does a city called Berlin located (Use NESTED query)?
SELECT C.Name
FROM Country C
WHERE C.code =
(SELECT T.CountryCode
FROM City T
WHERE T.name = 'Berlin');
Q3: Show which countries are between China and Japan in terms of population. Order the result by population in descending order
When a relation with one attribute is returned
4: Find countries in Asia that have cities with population of more than 5 million
SELECT C.Name
FROM Country C
WHERE C.Continent = 'Asia'
AND C.Code IN (SELECT T.CountryCode
FROM City T
WHERE T.Population > 5000000);
Q5: Show the name and population of the country with the max population in Asia.
SELECT Name, Population
FROM Country
WHERE Continent='Asia'
ORDER BY Population DESC
LIMIT 1;
Nested Query – When a relation with multiple attribute is returned
'개념 > 데이터베이스시스템' 카테고리의 다른 글
Partition BY vs GROUP BY (0) | 2024.08.22 |
---|---|
Indexing (0) | 2022.11.11 |
Intermediate SQL QUIZ (0) | 2022.10.26 |
Basic SQL (0) | 2022.10.26 |
Relational Model (0) | 2022.10.26 |
- Total
- Today
- Yesterday
- oauth
- oauth2.0
- html
- bfs
- 데이터 3법
- git commit 수정
- 리눅스
- 데이터베이스
- git 예전 커밋 수정
- SpringBoot
- CSS
- infcon 2024
- authorization_code
- 클로아
- authorization code
- 프로그래머스
- 우분투
- 로스트아크 캐릭터
- SQL
- DDL
- git
- DML
- kloa
- javascript
- 오픈소스
- 2024인프콘
- html #웹 #웹사이트 #플레이리스트
- 인프콘2024
- 데이터3법
- Android Studio
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |