ContentsClass #03: SQL °ú »ó¿ë µ¥ÀÌŸº£À̽º ¼Ò°³
in place March 16, 2002, lasted modified March 16, 2002, working...
SQL (Structured Query Language)
SQL °³¿ä
SQL(Structured Query Langueage) 1970³â IBM San Jose ¿¬±¸¼Ò¿¡¼ °³¹ßµÇ¾î 1987 ISO Ç¥Áر¹¿¡¼ Ç¥ÁØÈµÇ¾î ´ëºÎºÐÀÇ »ó¿ë ½Ã½ºÅÛ¿¡ ä¿ëµÇ¾î ÀÖ´Ù.
- ´ëÈ½Ä ¾ð¾îÀÌ¸ç ³»Àå ¾ð¾îÀÌ´Ù.
- ÁýÇÕ´ÜÀ§·Î ¿¬»êÇÑ´Ù.
- ºñÀýÂ÷Àû ¾ð¾îÀÌ´Ù.
- Ç¥Çö·ÂÀÌ Ç³ºÎÇÏ°í ±¸Á¶°¡ °£´ÜÇÏ´Ù.
SQLÀÇ ¿ëµµ
- ´ëÈ½Ä Query ½Ã½ºÅÛÀÇ ¾ð¾î - Ad hoc Query
- DB Application °³¹ß½Ã Host language¿¡ Embeded SQL ÇüÅ·Π»ç¿ë
±âº»ÁúÀÇ
- SELCT attributes FROM tables [ WHERE conditions]
- attributes´Â Target Table¿¡ ¼ÓÇØ ÀÖ´Â ¿øÇÏ´Â Attribute list
- tables ´Â ÁúÀÇÀÇ ´ë»óÀÌ ÀÖ´Â Target Table
- WHERE Àý(WHERE clause)´Â Á¶°Ç½ÄÀ» Á¤ÀÇÇϸç ÁúÀÇ´Â Á¶°Ç½Ä¿¡ ¸Â´Â Tuple ¸¸ °Ë»ö
SQL ÀÚ·áÁ¤Àǹ®
- SQL ÀÇ ÀÚ·áÇü
ÀÚ·áÇü ³»¿ª integer 4 ¹ÙÀÌÆ® Á¤¼ö smallint 2 ¹ÙÀÌÆ® Á¤¼ö float(n) ºÎµ¿ ¼Ò½ºÁ¡ ¼ö decimal(i,j) °íÁ¤ ¼Ò¼öÁ¡À» Æ÷ÇÔÇÏ´Â 10Áø¼ö char(n) ¹®ÀÚ¼ö°¡ nÀÎ ¹®ÀÚ¿ varchar(n) ¹®ÀÚÀÇ ÃÖ´ë¼ö°¡ nÀÎ ¹®ÀÚ¿ date ÀÏÀÚ(yyyymmdd) time ½Ã°£(hhmmss) - SQL ÀÇ Table µé
ÀÚ·áÇü ³»¿ª ±âº» Å×À̺í ÀڷḦ ±â¾ïÇÏ´Â ÆÄÀÏ Á¤ÀÇ, ¿°ú ÇàÀ¸·Î ÀÌ·ç¾îÁü ºä Å×ÀÌºí »ç¿ëÀÚ°¡ ÀÓÀÇ·Î ±âº» Å×À̺íÀ̳ª ºä Å×ÀÌºí¿¡¼ À¯µµ ÇÏ¿© ¸¸µç Å×ÀÌºí »öÀÎ Å×ÀÌºí ¼Ó¼º ´ÜÀ§·Î ÀڷḦ Ž»öÇϱâ À§ÇØ ¸¸µç Å×À̺í - SQLÀÇ ÀÚ·á Á¤Àǹ®
ÀÚ·áÇü ³»¿ª create table ±âº» Å×À̺íÀ» »ý¼ºÇÑ´Ù. create view ºä Å×À̺íÀ» ¸¸µç´Ù. create index »öÀÎ Å×À̺íÀ» ¸¸µç´Ù. drop Å×À̺íÀ» »èÁ¦ÇÑ´Ù. alter ±âÁ¸ Å×À̺íÀÇ ±¸Á¶¸¦ º¯°æÇÑ´Ù. - ±âº» Å×ÀÌºí »ý¼º/Á¦°Å
Å×À̺íÀÇ À̸§°ú ¼Ó¼ºÀ» Á¤ÀÇÇÏ°í ±âº»Å°¸¦ Á¤ÀÇÇÑ´Ù.
CREATE TABLE customer ( c_name char(15), bod char(8), //birth of date address char(30), b-type char(10), // business type primary key (c_name)); CREATE TABLE order ( order_no int, c_name char(15), date date, product char(8), address char(30), amount char(10), primary key (order_no));Å×À̺íÀ» »èÁ¦ÇÑ´Ù.
DROP TABLE customer;- ±âº» Å×ÀÌºí º¯°æ
Ãß°¡µÇ´Â ¼Ó¼ºÀÇ À̸§°ú ÀÚ·áÇü ±×¸®°í ÀÚ·á±æÀ̸¦ ÁöÁ¤ÇÑ´Ù.
ALTER TABLE customer add telephone char(15);- »öÀÎ Å×ÀÌºí »ý¼º/Á¦°Å
»¡¸®°Ô °Ë»öÇÒ ±âº»Å×À̺í°ú ÇØ´ç ¼Ó¼ºÀ» Á¤ÀÇÇÑ´Ù.
CREATE UNIQUE INDEX c_name_inx ON customer c_name asc;Å×À̺íÀ» »èÁ¦ÇÑ´Ù.
DROP INDEX c_name_inx;- View Å×ÀÌºí »ý¼º/Á¦°Å
View´Â À¯µµµÈ Å×À̺í·Î ½ÇÇà½Ã°£¿¡ Query¿¡ ÀÇÇÏ¿© »ý¼ºµÈ´Ù. ±×·¯¹Ç·Î View¿¡¼ »ç¿ëÇÏ´Â SQL ¹®Àº ÀÏ¹Ý SQL ¹®°ú °°À¸³ª UNION À̳ª ORDER BY ¸¦ »ç¿ëÇÏÁö ¸øÇÏ°í »ðÀÔ/»èÁ¦/°»½Åµî¿¡ ¸¹Àº Á¦ÇÑÀ» °®°í ÀÖ´Ù.
CREATE VIEW customer_view (c_name, address) as select c_name, address from customer where b-type="WHOLESALE"Å×À̺íÀ» »èÁ¦ÇÑ´Ù.
DROP VIEW customer_view;SQL ÀÚ·áÁ¶ÀÛ¹®
ÀÚ·áÇü ³»¿ª select ÀڷḦ °Ë»öÇÑ´Ù. insert »õ·Î¿î ÀڷḦ Å×À̺íÀÇ Çà¿¡ »ðÀÔÇÑ´Ù. delete ƯÁ¤ÇÑ Á¶°Ç¿¡ ¸Â´Â ÇàÀ» Å×ÀÌºí¿¡¼ »èÁ¦ÇÑ´Ù. update ƯÁ¤ÇÑ Á¶°Ç¿¡ ¸Â´Â ÇàÀÇ ¼Ó¼ºµéÀÇ ÀÚ·á°ªÀ» °»½ÅÇÑ´Ù.
- ±âº» ÀÚ·á °Ë»ö
SELECT [ all | distinct ] {attribute_list} FROM table_list [ where caluse ] [ group by attribute [having condition]] [ order by attribute [asc | desc] ]
- attribute_list¿¡ ¸í½ÃµÈ ¸ðµç ¼Ó¼º°ªÀ» ã¾Æ ÁØ´Ù. distinct¸¦ Àû¿ëÇϸé Áߺ¹µÈ °á°ú¸¦ Ãâ·ÂÇÏÁö ¾Ê´Â´Ù.
- FROM Àý¿¡¼ µÎ°³ÀÌ»óÀÇ Å×À̺íÀ» Á¶ÀÎÇÒ ¶§´Â Å×À̺í À̸§À» ' ·Î ±¸ºÐÇÏ¿© ¸í½ÃÇÑ´Ù.
- WHERE ¿¡´Â °ü°è ¿¬»êÀÚ (relational operator)¸¦ ÀÌ¿ëÇÏ¿© ¼±ÅÃÁ¶°ÇÀ» ±âÀçÇÑ´Ù.
¿¬»êÀÚ ³»¿ª ÀÌÁø ¿¬»êÀÚ >, <, <=,>=, =, <> ´ÜÇ× ¿¬»êÀÚ in, not in Ư¼ö ¿¬»êÀÚ like null, exists, union
- group ÀýÀº ÀÚ·áµéÀ» ÁýÇÕÀ¸·Î ¹¾î¼ ó¸®ÇÒ °á¿ì Having Àý¿¡ ¼±ÅÃÁ¶°Ç°ú ÇÔ²² ±âÀç
- order´Â °á°ú Ãâ·Â½Ã Á¤·ÄÇÑ ÇüŸ¦ Á¤ÀÇÇÒ ¼ö ÀÖ´Ù.
- select ¹®À» ºÎ¼Ó ÁúÀǾî·Î »ç¿ëÇÒ ¼ö ÀÖ´Ù. À̶§ in, not in, unionÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù.- Áý´ÜÇÔ¼ö(Aggregate Function)À» ÀÌ¿ëÇÑ °Ë»ö
ÇÔ¼öÀ̸§ ³»¿ª COUNT ÀÚ·á°ªÀÇ ¼ö SUM ÀÚ·á°ªÀÇ ÇÕ°è AVG Æò±Õ°ª MAX ÃÖ´ë°ª MIN ÃÖ¼Ò°ª
SELECT count(*) FROM customer c; SELECT avg(o.amount) FROM order o WHERE o.date >= 20000101- Group By Àý°ú Having Àý
SELECT "AVERAGE =", avg(o.amount) FROM order o GROUP BY O.c_name having count(*) > 49- ±âŸ ÀÚ·á °Ë»ö
LIKE ¿Í is NULL
SELECT c.name FROM customer c WHERE c.c_name LIKE 'µµ%' SELECT c.name FROM customer c WHERE c.address IS NULLSQL ÁýÇÕ ¹× ³»Æ÷ÁúÀÇ
- ÁýÇÕ ¿¬»êÀÚ (Union, Intersect, minus)´Â µÎ°³ÀÇ ±âº» Query °á°ú¿¡ ´ëÇÏ¿© ÁýÇÕ ¿¬»ê(ÇÕÁýÇÕ, ±³ÁýÇÕ, Â÷ÁýÇÕ)À» ½ÇÇàÇÑ´Ù.
- ³»Æ÷ÁúÀÇ(Nested Query)¶õ ÁúÀǾȿ¡ ¶Ç ´Ù¸¥ ÁúÀÇ°¡ Æ÷ÇÔµÈ ÇüÅÂÀÇ ÁúÀÇÀÌ´Ù. ÁÖ·Î Where Àý¿¡ ³»Æ÷ ÁúÀÇ°¡ Æ÷ÇԵȴÙ.
SELECT c.name FROM customer c WHERE c.c_number NOT IN (SELECT R.c_number FROM order o WHERE o.product = '»ç°ú)SQL ±âŸÁúÀÇ
- UPDATE ¹®Àå
UPDATE customer c SET address = "ÁøÁÖ" WHERE address = "´ëÀü"- INSERT ¹®Àå
INSERT INTO customer(name, address) VALUES('µµ³²Ã¶', '´ëÀü')- DELETE ¹®Àå
DELETE FROM customer WHERE name = 'µµ³²Ã¶'
Knowledge & Engineering Databases (c) copyright Namchul Do, 2002