INSERT
, DELETE
, UPDATE
๊ตฌ๋ฌธ
์์ฑ์์์ NOT NULL
์ ์ฝ ์กฐ๊ฑด์ ๊ทธ ์์ฑ์ ๋ํด NULL ๊ฐ์ด ํ๋ฝ๋์ง ์์์ ๋ช
์ํ๋ค. ๋ฌ๋ฆฌ ๋งํ๋ฉด, ์ ์ฝ์กฐ๊ฑด์ ๊ทธ ์์ฑ์ ๋๋ฉ์ธ์์ NULL ๊ฐ์ ์ ์ธ์ํจ๋ค.
NOT NULL
์ ์ฝ์กฐ๊ฑด์ ์ง์ ํ์ฌ ์ ํธ๋ฆฌ๋ทฐํธ์ NULL ๊ฐ์ ํ์ฉํ์ง ์์ ์๋ ์์
CREATE TABLE DEPARTMENT (DNUMBER INT NOT NULL, ... );
DEFAULT
๋ฌธ์ ์ด์ฉํ์ฌ ์ ํธ๋ฆฌ๋ทฐํธ์ ๋ํดํธ ๊ฐ์ ๋ช
์
CREATE TABLE EMPLOYEE (DNO INT DEFAULT 1, ... );
SQL ํํ์ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ SELECT, FROM, WHERE
์ ์ธ ๊ฐ์ ์ ๋ก ์ด๋ฃจ์ด์ง๋ค. ์ง์๋ FROM
์ ์ ๋์ด๋ ๋ฆด๋ ์ด์
๋ค์ ์
๋ ฅ์ผ๋ก ๋ฐ๊ณ , WHERE
๊ณผ SELECT
์ ์ ๋ช
์๋ ๋์์ ์ํํด ๊ฒฐ๊ณผ๋ก ๋ฆด๋ ์ด์
์ ๋ง๋ค์ด ๋ธ๋ค.
SELECT
๋ค์ DISTINCT
๋ผ๋ ํค์๋๋ฅผ ์ฝ์
ํ๋ค.DISTINCT
๋ ์ง์ ๊ฒฐ๊ณผ์์ ์ ์ผํ ํํ๋ค๋ง ๋จ๊ธฐ๋ผ๋ ์๋ฏธ์ ํค์๋์
SELECT DISTINCT DEPT_NAME, SALARY
๊ตฌ๋ฌธ์ด๋ฉด, SALARY๋ DISTINCT
ํจ๊ณผ๋ฅผ ๋ฐ์ง ์๊ณ , DEPT_NAME์์ ๊ฒน์น๋ ๊ฒ๋ค์ค ๋จผ์ ๋์จ ํํ๋ง ์ถ๋ ฅ๋๋ค.JOIN (์กฐ์ธ)์ด๋?
๋ ๊ฐ ์ด์์ ํ ์ด๋ธ์ด๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐ๊ฒฐํ์ฌ ํ๋๋ก ๋ง๋ค๊ณ , ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ
SELECT <select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key
SELECT * FROM EMPLOYEE
INNER JOIN DEPARTMENT
ON EMPLOYEE.DepartmentID = DEPARTMENT.DepartmentID;
,
)๋ฅผ ์ฌ์ฉํ์ฌ ๋จ์ํ ์กฐ์ธ์ ์ํ ์ฌ๋ฌ ํ
์ด๋ธ์ ๋์ดํ๋ค.SELECT * FROM EMPLOYEE, DEPARTMENT
WHERE EMPLOYEE.DepartmentID = DEPARTMENT.DepartmentID;
Outer Join์ด๋ ์กฐ์ธ ์กฐ๊ฑด์์ ๋์ผํ ๊ฐ์ด ์๋ ํ๋ ๋ฐํํ ๋ ์ฌ์ฉํ๋ค.
SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
SELECT
A.NAME, B.AGE
FROM TABLE_A A, TABLE_A B
NOT IN
์ด๋ NOT EXISTS
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.SELECT *
FROM employee a, department b WHERE a.dno = b.dnumber AND a.dno
NOT IN
( SELECT dnumber
FROM department
WHERE mgr_ssn IS NULL) ;
IN
์ด๋ EXISTS
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.SELECT *
FROM employee a, department b WHERE a.dno = b.dnumber AND a.dno
IN
( SELECT dnumber
FROM department
WHERE mgr_ssn IS NOT NULL ) ;
UNION
), ์ฐจ์งํฉ(EXCEPT
), ๊ต์งํฉ(INTERSECT
)
UNION
์ฐ์ฐ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ณ ์ ํ(distinct) ๊ฐ๋ง ์ ํํ๋ค.
์ค๋ณต๋ ๊ฐ์ ํ์ฉํ๋ ค๋ฉด UNION ALL
์ ์ฌ์ฉํด์ผํ๋ค.UNION ALL
, EXCEPT ALL
, INTERSECT ALL
LIKE
๋น๊ต ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด(ํน์ ๋ถ๋ถ ๋ฌธ์์ด)์ ๋ํด ๋น๊ต์กฐ๊ฑด์ ์ ์ฉํ ์ ์๋ค.%
๋ ์์์ ๊ฐ์์ ๋ฌธ์๋ฅผ ์๋ฏธํ๊ณ , _
๋ ์์์ ํ ๋ฌธ์๋ฅผ ์๋ฏธํ๋ค.ORDER BY
์
ORDER BY DNAME DESC, LNAME ASC, FNAME ASC
DESC
: ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ โ DescendingASC
: ์ค๋ฆ์ฐจ์ ์ ๋ ฌ โ AscendingINSERT
, DELETE
, UPDATE
๊ตฌ๋ฌธINSERT INTO VALUE
INSERT INTO EMPLOYEE
VALUES (โRichardโ,โKโ,โMariniโ,โ653298653โ,โ30-DEC-52โ,
โ98 Oak Forest, Katy, TXโ,โMโ,37000,โ987654321โ,4) ;
INSERT INTO EMPLOYEE (FNAME, LNAME, DNO, SSN)
VALUES (FNAME=โRichardโ, LNAME=โMariniโ,DNO= 4, SSN=โ653298653โ) ;
SELECT์
๊ฒฐํฉ๋ INSERT
๋ช
๋ นCREATE TABLE WORKS_ON_INFO
( EMP_NAME VARCHAR(15),
PROJ_NAME VARCHAR(15),
HOURS_PER_WEEK DECIMAL(3,1) );
INSERT INTO WORKS_ON_INFO (EMP_NAME, PROJ_NAME, HOURS_PER_WEEK)
SELECT E.LNAME, P.PNAME, W.HOURS
FROM PROJECT P, WORKS_ON W, EMPLOYEE E
WHERE P.PNUMBER=W.PNO AND W.ESSN=E.SSN;
DELETE
๋ฌธ DELETE FROM EMPLOYEE;
DELETE FROM EMPLOYEE WHERE LNAME=โBrownโ ;
DELETE FROM EMPLOYEE WHERE SSN=โ123456789โ ;
DELETE FROM EMPLOYEE WHERE DNO=5;
UPDATE
๋ฌธmysql> use company;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| DEPARTMENT |
| DEPENDENT |
| DEPT_LOCATIONS |
| EMPLOYEE |
| PROJECT |
| WORKS_ON |
+-------------------+
6 rows in set (0.00 sec)
mysql> desc department;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Dname | varchar(15) | NO | UNI | NULL | |
| Dnumber | int | NO | PRI | NULL | |
| Mgr_ssn | char(9) | NO | MUL | NULL | |
| Mgr_start_date | date | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc dependent;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Essn | char(9) | NO | PRI | NULL | |
| Dependent_name | varchar(15) | NO | PRI | NULL | |
| Sex | char(1) | YES | | NULL | |
| Bdate | date | YES | | NULL | |
| Relationship | varchar(8) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc dept_locations;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Dnumber | int | NO | PRI | NULL | |
| Dlocation | varchar(15) | NO | PRI | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc employee;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Fname | varchar(10) | NO | | NULL | |
| Minit | char(1) | YES | | NULL | |
| Lname | varchar(20) | NO | | NULL | |
| Ssn | char(9) | NO | PRI | NULL | |
| Bdate | date | YES | | NULL | |
| Address | varchar(30) | YES | | NULL | |
| Sex | char(1) | YES | | NULL | |
| Salary | decimal(5,0) | YES | | NULL | |
| Super_ssn | char(9) | YES | MUL | NULL | |
| Dno | int | NO | MUL | NULL | |
+-----------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)
mysql> desc project;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Pname | varchar(15) | NO | UNI | NULL | |
| Pnumber | int | NO | PRI | NULL | |
| Plocation | varchar(15) | YES | | NULL | |
| Dnum | int | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc works_on;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| Essn | char(9) | NO | PRI | NULL | |
| Pno | int | NO | PRI | NULL | |
| Hours | decimal(3,1) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from department;
+----------------+---------+-----------+----------------+
| Dname | Dnumber | Mgr_ssn | Mgr_start_date |
+----------------+---------+-----------+----------------+
| Headquarters | 1 | 888665555 | 1981-06-19 |
| Administration | 4 | 987654321 | 1995-01-01 |
| Research | 5 | 333445555 | 1988-05-22 |
+----------------+---------+-----------+----------------+
3 rows in set (0.01 sec)
mysql> select * from dependent;
+-----------+----------------+------+------------+--------------+
| Essn | Dependent_name | Sex | Bdate | Relationship |
+-----------+----------------+------+------------+--------------+
| 123456789 | Alice | F | 1988-12-30 | Daughter |
| 123456789 | Elizabeth | F | 1967-05-05 | Spouse |
| 123456789 | Michael | M | 1988-01-04 | Son |
| 333445555 | Alice | F | 1986-04-04 | Daughter |
| 333445555 | Joy | F | 1958-05-03 | Spouse |
| 333445555 | Theodore | M | 1983-10-25 | Son |
| 987654321 | Abner | M | 1942-02-28 | Spouse |
+-----------+----------------+------+------------+--------------+
7 rows in set (0.00 sec)
mysql> select * from dept_locations;
+---------+-----------+
| Dnumber | Dlocation |
+---------+-----------+
| 1 | Houston |
| 4 | Stafford |
| 5 | Bellaire |
| 5 | Houston |
| 5 | Sugarland |
+---------+-----------+
5 rows in set (0.00 sec)
mysql> select * from employee;
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
| Fname | Minit | Lname | Ssn | Bdate | Address | Sex | Salary | Super_ssn | Dno |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M | 30000 | 333445555 | 5 |
| Franklin | T | Wong | 333445555 | 1965-12-08 | 638 Voss, Houston TX | M | 40000 | 888665555 | 5 |
| Joyce | A | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX | F | 25000 | 333445555 | 5 |
| Ramesh | K | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M | 38000 | 333445555 | 5 |
| James | E | Borg | 888665555 | 1937-11-10 | 450 Stone, Houston TX | M | 55000 | NULL | 1 |
| Jennifer | S | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX | F | 43000 | 888665555 | 4 |
| Ahmad | V | Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston TX | M | 25000 | 987654321 | 4 |
| Alicia | J | Zelaya | 999887777 | 1968-01-19 | 3321 Castle, Spring TX | F | 25000 | 987654321 | 4 |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
8 rows in set (0.01 sec)
mysql> select * from project;
+-----------------+---------+-----------+------+
| Pname | Pnumber | Plocation | Dnum |
+-----------------+---------+-----------+------+
| ProductX | 1 | Bellaire | 5 |
| ProductY | 2 | Sugarland | 5 |
| ProductZ | 3 | Houston | 5 |
| Computerization | 10 | Stafford | 4 |
| Reorganization | 20 | Houston | 1 |
| Newbenefits | 30 | Stafford | 4 |
+-----------------+---------+-----------+------+
6 rows in set (0.00 sec)
mysql> select * from works_on;
+-----------+-----+-------+
| Essn | Pno | Hours |
+-----------+-----+-------+
| 123456789 | 1 | 32.5 |
| 123456789 | 2 | 7.5 |
| 333445555 | 2 | 10.0 |
| 333445555 | 3 | 10.0 |
| 333445555 | 10 | 10.0 |
| 333445555 | 20 | 10.0 |
| 453453453 | 1 | 20.0 |
| 453453453 | 2 | 20.0 |
| 666884444 | 3 | 40.0 |
| 888665555 | 20 | 16.0 |
| 987654321 | 20 | 15.0 |
| 987654321 | 30 | 20.0 |
| 987987987 | 10 | 35.0 |
| 987987987 | 30 | 5.0 |
| 999887777 | 10 | 10.0 |
| 999887777 | 30 | 30.0 |
+-----------+-----+-------+
16 rows in set (0.00 sec)
Fname
, Minit
, Lname
)๊ณผ ์๋
์์ผ(Bdate
)๊ณผ ์ฃผ์(Address
)๋ฅผ ๊ฒ์ํ๋ผ.
mysql> select Fname, Minit, Lname, Bdate, Address from employee WHERE Lname='Borg';
+-------+-------+-------+------------+-----------------------+
| Fname | Minit | Lname | Bdate | Address |
+-------+-------+-------+------------+-----------------------+
| James | E | Borg | 1937-11-10 | 450 Stone, Houston TX |
+-------+-------+-------+------------+-----------------------+
1 row in set (0.00 sec)
EMPLOYEE
์ DEPARTMENT
์ ์นดํฐ์
๊ณฑ์ ๊ฒ์ํ๋ผ.
mysql> select * from Employee, Department;
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
| Fname | Minit | Lname | Ssn | Bdate | Address | Sex | Salary | Super_ssn | Dno | Dname | Dnumber | Mgr_ssn | Mgr_start_date |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M | 30000 | 333445555 | 5 | Research | 5 | 333445555 | 1988-05-22 |
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M | 30000 | 333445555 | 5 | Administration | 4 | 987654321 | 1995-01-01 |
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M | 30000 | 333445555 | 5 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Franklin | T | Wong | 333445555 | 1965-12-08 | 638 Voss, Houston TX | M | 40000 | 888665555 | 5 | Research | 5 | 333445555 | 1988-05-22 |
| Franklin | T | Wong | 333445555 | 1965-12-08 | 638 Voss, Houston TX | M | 40000 | 888665555 | 5 | Administration | 4 | 987654321 | 1995-01-01 |
| Franklin | T | Wong | 333445555 | 1965-12-08 | 638 Voss, Houston TX | M | 40000 | 888665555 | 5 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Joyce | A | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX | F | 25000 | 333445555 | 5 | Research | 5 | 333445555 | 1988-05-22 |
| Joyce | A | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX | F | 25000 | 333445555 | 5 | Administration | 4 | 987654321 | 1995-01-01 |
| Joyce | A | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX | F | 25000 | 333445555 | 5 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Ramesh | K | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M | 38000 | 333445555 | 5 | Research | 5 | 333445555 | 1988-05-22 |
| Ramesh | K | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M | 38000 | 333445555 | 5 | Administration | 4 | 987654321 | 1995-01-01 |
| Ramesh | K | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M | 38000 | 333445555 | 5 | Headquarters | 1 | 888665555 | 1981-06-19 |
| James | E | Borg | 888665555 | 1937-11-10 | 450 Stone, Houston TX | M | 55000 | NULL | 1 | Research | 5 | 333445555 | 1988-05-22 |
| James | E | Borg | 888665555 | 1937-11-10 | 450 Stone, Houston TX | M | 55000 | NULL | 1 | Administration | 4 | 987654321 | 1995-01-01 |
| James | E | Borg | 888665555 | 1937-11-10 | 450 Stone, Houston TX | M | 55000 | NULL | 1 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Jennifer | S | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX | F | 43000 | 888665555 | 4 | Research | 5 | 333445555 | 1988-05-22 |
| Jennifer | S | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX | F | 43000 | 888665555 | 4 | Administration | 4 | 987654321 | 1995-01-01 |
| Jennifer | S | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX | F | 43000 | 888665555 | 4 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Ahmad | V | Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston TX | M | 25000 | 987654321 | 4 | Research | 5 | 333445555 | 1988-05-22 |
| Ahmad | V | Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston TX | M | 25000 | 987654321 | 4 | Administration | 4 | 987654321 | 1995-01-01 |
| Ahmad | V | Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston TX | M | 25000 | 987654321 | 4 | Headquarters | 1 | 888665555 | 1981-06-19 |
| Alicia | J | Zelaya | 999887777 | 1968-01-19 | 3321 Castle, Spring TX | F | 25000 | 987654321 | 4 | Research | 5 | 333445555 | 1988-05-22 |
| Alicia | J | Zelaya | 999887777 | 1968-01-19 | 3321 Castle, Spring TX | F | 25000 | 987654321 | 4 | Administration | 4 | 987654321 | 1995-01-01 |
| Alicia | J | Zelaya | 999887777 | 1968-01-19 | 3321 Castle, Spring TX | F | 25000 | 987654321 | 4 | Headquarters | 1 | 888665555 | 1981-06-19 |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
24 rows in set (0.00 sec)
SSN
)๊ณผ ๊ทธ๋ค์ด ์ํ ์์ ๋ถ์(DNAME
)๋ค์ ๋ชจ๋ ๊ฒ์ํ๋ผ.
mysql> select Ssn, Dname from employee, department WHERE Dno=Dnumber;
+-----------+----------------+
| Ssn | Dname |
+-----------+----------------+
| 987654321 | Administration |
| 987987987 | Administration |
| 999887777 | Administration |
| 888665555 | Headquarters |
| 123456789 | Research |
| 333445555 | Research |
| 453453453 | Research |
| 666884444 | Research |
+-----------+----------------+
8 rows in set (0.00 sec)
Headquarters
๋ถ์์์ ๊ทผ๋ฌดํ๋ ๋ชจ๋ ์ฌ์์ ์ด๋ฆ(Fname
, Lname
)๊ณผ ์ฃผ์(Address
)๋ฅผ ๊ฒ์ํ๋ผ.
mysql> select Fname, Lname, Address from employee, department WHERE Dno=Dnumber AND Dname='Headquarters';
+-------+-------+-----------------------+
| Fname | Lname | Address |
+-------+-------+-----------------------+
| James | Borg | 450 Stone, Houston TX |
+-------+-------+-----------------------+
1 row in set (0.00 sec)
Administration
๋ถ์์์ ์ผํ๋ ์ฌ์๋ค์ ์ฑ(Lname
)๊ณผ ์ด๋ฆ(Fname
), ๊ทธ๋ฆฌ๊ณ ๊ด๋ฆฌ์๋ฅผ ์์ํ ๋ (mgr_start_date
)์ ๊ฒ์ํ๋ผ.
mysql> select Lname, Fname, Mgr_start_date from employee, department WHERE Dno=Dnumber AND Dname='Administration';
+---------+----------+----------------+
| Lname | Fname | Mgr_start_date |
+---------+----------+----------------+
| Wallace | Jennifer | 1995-01-01 |
| Jabbar | Ahmad | 1995-01-01 |
| Zelaya | Alicia | 1995-01-01 |
+---------+----------+----------------+
3 rows in set (0.00 sec)
๊ฐ ์ฌ์์ ๋ํด ์ฌ์์ ์ด๋ฆ(Fname
)๊ณผ ์ฑ(Lname
), ์ง์ ์์ฌ์ ์ด๋ฆ (Fname
)๊ณผ ์ฑ(Lname
)์ ๊ฒ์ํ๋ผ.
mysql> select E.Fname, E.Lname, S.Fname, S.Lname from Employee AS E, Employee AS S WHERE E.Super_ssn = S.Ssn;
+----------+---------+----------+---------+
| Fname | Lname | Fname | Lname |
+----------+---------+----------+---------+
| John | Smith | Franklin | Wong |
| Franklin | Wong | James | Borg |
| Joyce | English | Franklin | Wong |
| Ramesh | Narayan | Franklin | Wong |
| Jennifer | Wallace | James | Borg |
| Ahmad | Jabbar | Jennifer | Wallace |
| Alicia | Zelaya | Jennifer | Wallace |
+----------+---------+----------+---------+
7 rows in set (0.01 sec)
mysql> select E.Fname AS Employee_Fname, E.Lname AS Employee_Lname, S.Fname AS Super_Fname, S.Lname AS Super_Lname from Employee AS E, Employee AS S WHERE E.Super_ssn = S.Ssn;
+----------------+----------------+-------------+-------------+
| Employee_Fname | Employee_Lname | Super_Fname | Super_Lname |
+----------------+----------------+-------------+-------------+
| John | Smith | Franklin | Wong |
| Franklin | Wong | James | Borg |
| Joyce | English | Franklin | Wong |
| Ramesh | Narayan | Franklin | Wong |
| Jennifer | Wallace | James | Borg |
| Ahmad | Jabbar | Jennifer | Wallace |
| Alicia | Zelaya | Jennifer | Wallace |
+----------------+----------------+-------------+-------------+
7 rows in set (0.00 sec)
Pnumber
), ๋ด๋น ๋ถ์ ๋ฒํธ(Dnum
), ๋ถ์ ๊ด๋ฆฌ์์ ์ฑ(Lname
), ์ฃผ์(Address
), ์๋
์์ผ (Bdate
)์ ๊ฒ์ํ๋ผ.
mysql> select P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate from project as P, employee as E, department as D WHERE P.Plocation='Houston' AND P.Dnum=D.Dnumber AND D.Mgr_ssn = E.Ssn;
+---------+------+-------+-----------------------+------------+
| Pnumber | Dnum | Lname | Address | Bdate |
+---------+------+-------+-----------------------+------------+
| 3 | 5 | Wong | 638 Voss, Houston TX | 1965-12-08 |
| 20 | 1 | Borg | 450 Stone, Houston TX | 1937-11-10 |
+---------+------+-------+-----------------------+------------+
2 rows in set (0.00 sec)
Lname
)์ด โSmithโ์ธ ์ฌ์์ด ์ํํ๋ ํ๋ก์ ํธ ๋ฒํธ(Pnumber
)์ ๋ฆฌ์คํธ๋ฅผ ๊ฒ์ํ๋ผ. (hint : UNION
)
mysql> (select Pnumber from project, works_on, employee where Pnumber=Pno and Essn=Ssn and Lname='Smith')
-> UNION
-> (select Pnumber from project, department, employee where Dnum=Dnumber and Mgr_ssn=Ssn and Lname='Smith');
+---------+
| Pnumber |
+---------+
| 1 |
| 2 |
+---------+
2 rows in set (0.00 sec)
์ฃผ์์ โHouston TXโ๊ฐ ํฌํจ๋๋ ๋ชจ๋ ์ฌ์(Fname
, Lname
)์ ๊ฒ์ํ๋ผ.
mysql> select Fname, Lname from Employee where Address LIKE '%Houston TX%';
+----------+---------+
| Fname | Lname |
+----------+---------+
| John | Smith |
| Franklin | Wong |
| Joyce | English |
| James | Borg |
| Ahmad | Jabbar |
+----------+---------+
5 rows in set (0.00 sec)
mysql> -- ํ์ธ ๋ฐ ๊ฒ์ฆํ๊ธฐ --
mysql> select Fname, Lname, Address from Employee where Address LIKE '%Houston TX%';
+----------+---------+-------------------------+
| Fname | Lname | Address |
+----------+---------+-------------------------+
| John | Smith | 731 Fondren, Houston TX |
| Franklin | Wong | 638 Voss, Houston TX |
| Joyce | English | 5631 Rice, Houston TX |
| James | Borg | 450 Stone, Houston TX |
| Ahmad | Jabbar | 980 Dallas, Houston TX |
+----------+---------+-------------------------+
5 rows in set (0.00 sec)
1960๋
๋ ํ์ด๋ ๋ชจ๋ ์ฌ์์ ์ ์ฒด ์ด๋ฆ(Fname
, Lname
)์ ๊ฒ์ํ๋ผ.
mysql> select Fname, Lname from employee where Bdate LIKE '196%';
+----------+---------+
| Fname | Lname |
+----------+---------+
| John | Smith |
| Franklin | Wong |
| Ramesh | Narayan |
| Ahmad | Jabbar |
| Alicia | Zelaya |
+----------+---------+
5 rows in set (0.00 sec)
mysql> -- ํ์ธ ๋ฐ ๊ฒ์ฆํ๊ธฐ --
mysql> select Fname, Lname, Bdate from employee where Bdate LIKE '196%';
+----------+---------+------------+
| Fname | Lname | Bdate |
+----------+---------+------------+
| John | Smith | 1965-01-09 |
| Franklin | Wong | 1965-12-08 |
| Ramesh | Narayan | 1962-09-15 |
| Ahmad | Jabbar | 1969-03-29 |
| Alicia | Zelaya | 1968-01-19 |
+----------+---------+------------+
5 rows in set (0.01 sec)
[gyoogle GitHub | tech-interview-for-developer/Computer Science/Database/[Database SQL] JOIN.md](https://github.com/gyoogle/tech-interview-for-developer/blob/master/Computer%20Science/Database/%5BDatabase%20SQL%5D%20JOIN.md) |
[carrot.log | [Database] JOIN์ ์ข ๋ฅ : INNER, OUTER, CROSS, SELF](https://velog.io/@yanghl98/Database-JOIN) |
[FRANKโS BLOG | ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์กฐ์ธ(join)์ด๋?](https://blog.advenoh.pe.kr/database/%EA%B4%80%EA%B3%84%ED%98%95-%EB%8D%B0%EC%9D%B4%ED%84%B0%EB%B2%A0%EC%9D%B4%EC%8A%A4%EC%97%90%EC%84%9C-%EC%A1%B0%EC%9D%B8-join%EC%9D%B4%EB%9E%80/) |
[C.L. Moffatt | Visual Representation of SQL Joins](https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins) |
[๊ฑฐ๊พธ๋ก ๋ฐ๋ผ๋ณธ ์ธ์ | [SQL] Join(์กฐ์ธ)](https://clairdelunes.tistory.com/22) |