goal
SQL๊ณต์๋ฌธ์๋ ๐๏ธ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ
ํํ ๋ฆฌ์ผ์๋ DB๋ฅผ ์์ฑํ ํ, ์์ฑ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋, ์ฆ table์ ๋ํด์ ๋จผ์ ์ค๋ช ํ์ง๋ง, ๊ฐ๋ ์ด ์์ํ ๋๋ฅผ ์ํด DB ๋จ๋ถํฐ ์์๋ณด์!
CREATE DATABASE [๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ];
DROP DATABASE [์์ฑํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ];
BACKUP DATABASE [๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ]
TO DIST = "[ํ์ผ๊ฒฝ๋ก]";
CREATE TABLE [ํ
์ด๋ธ ์ด๋ฆ] (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE topic(-> id INT(11) NOT NULL AUTO_INCREMENT,-> title VARCHAR(100) NOT NULL,-> description TEXT NULL,-> created DATETIME NOT NULL,-> PRIMARY KEY(id));DROP TABLE [์์ฑํ๋ ํ
์ด๋ธ ์ด๋ฆ];
ALTER TABLE [ํ
์ด๋ธ ์ด๋ฆ]ADD [column์ด๋ฆ] [ํด๋น ๋ฐ์ดํฐํ์
]; (ex. VARCHAR(100))ALTER TABLE [ํ
์ด๋ธ ์ด๋ฆ]DROP COLUMN [column์ด๋ฆ];ALTER TABLE [ํ
์ด๋ธ ์ด๋ฆ]MODIFY COLUMN [column์ด๋ฆ] [ํด๋น ๋ฐ์ดํฐํ์
]; (MySQL๊ธฐ์ค => sql๋ง๋ค ๋ค๋ฅด๋ ํ์ธ ํ์)NOT NULL์ column์ด NULL๊ฐ์ ๊ฐ์ง ์๋๋ก ๊ฐ์ ํ๋ค.NOT NULL์ ๋ฃ์ผ๋ฉด, field์ ํญ์ ๊ฐ์ด ์ถ๊ฐ๋์ด์ผ ํ๋ฏ๋ก, ๊ฐ์ด ์์ผ๋ฉด, ์๋ก์ด record์ ์ถ๊ฐ ์ฝ์
, ์์ ๋ฑ์ด ๋ถ๊ฐํ๋ค.CREATE TABLE topic(-> id INT(11) NOT NULL...CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,CONSTRAINT UC_Person UNIQUE (ID,LastName));CREATE TABLE topic(-> id INT(11) NOT NULL AUTO_INCREMENT,-> title VARCHAR(100) NOT NULL,-> PRIMARY KEY (id));CREATE TABLE Orders (-> OrderID int NOT NULL,-> OrderNumber int NOT NULL,-> PersonID int,-> PRIMARY KEY (OrderID),-> CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)-> REFERENCES Persons(PersonID)-> );DATE - format YYYY-MM-DDDATETIME - format: YYYY-MM-DD HH:MI:SSSMALLDATETIME - format: YYYY-MM-DD HH:MI:SSTIMESTAMP - format: a unique numberSELECT CustomerName, City FROM Customers;// ๋ถ๋ถ
SELECT column1, column2, ...
FROM table_name;
// ์ ์ฒด
SELECT * FROM table_name;
SELECT * FROM Customers WHERE Country='Mexico';SELECT column1, column2, ...
FROM table_name
WHERE condition;

SELECT * FROM Customers WHERE Country = "Germany" AND City = "Berlin"SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='Mรผnchen');SELECT * FROM Customers WHERE NOT Country='Germany' AND NOT Country='USA'; SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
--
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
--
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
SELECT * FROM Customers ORDER BY Country, CustomerName; : ๊ธฐ๋ณธ์ ์ผ๋ก Country์ ๋ฐ๋ผ ์ ๋ ฌํ ํ, ๊ฐ ๋๋ผ ์์์ CustomerName ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌSELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
null INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
ZERO VALUES์๋ ๋ฐ๋ฆ// IS NULL - null๊ฐ์ ์ฐพ๊ธฐ ์ํด ์ฌ์ฉํ๋ค (null์ด ์๋์ง ์ฌ๋ถ ํ์
)
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
// IS NOT NULL - ๋น์ด์์ง ์์ ๊ฐ์ ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1; : ํ๋์ฉ ์์ UPDATE Customers SET ContactName='Juan' WHERE Country='Mexico'; : ์ฌ๋ฌ๊ฐ ์์ UPDATE Customers SET ContactName='Juan'; : WHERE์ ์๋ตํ๋ฉด, ์ ์ฒด๋ฅผ ๋์์ผ๋ก ์์ UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste'; : ์กฐ๊ฑด์ญ์ DELETE FROM table_name; : ์ ์ฒด์ญ์ DELETE FROM table_name WHERE condition;
COUNT : ์ง์ ๋ ๊ธฐ์ค๊ณผ ์ผ์นํ๋ ํ ์๋ฅผ ๋ฐํAVG : ์ซ์ ์ด์ ํ๊ท ๊ฐ์ ๋ฐํSUM : ์ซ์ ์ด์ ์ดํฉ์ ๋ฐํSELECT COUNT/AVG/SUM(column_name)
FROM table_name
WHERE condition;
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

LIKE์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ

// Alias Column syntax
SELECT column_name AS alias_name
FROM table_name;
// Alias Table syntax
SELECT column_name(s)
FROM table_name AS alias_name;
// ์ฌ๋ฌ๊ฐ๋ ๊ฐ๋ฅ
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers
// ๋ณ์นญ์ ๊ณต๋ฐฑ์ด ํฌํจ๋ ๊ฒฝ์ฐ, ํฐ ๋ฐ์ดํ or ๋๊ดํธ๊ฐ ํ์ํจ
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
// ์ฌ๋ฌ ๊ฐ์ ์ด์ ๊ฒฐํฉํด์, ์๋ก์ด ๋ณ์นญ(Adress)์ ๋ง๋ฆ
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers;
// etc... ์ฌ๋ฌ๊ฐ์ง๊ฐ ์์
Select all the different values from the Country column in the Customers table.
: SELECT DISTINCT Country FROM Customers;
With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" starts with an "a"?
: SELECT * FROM Persons WHERE FirstName LIKE 'a%'
With SQL, how do you select all the records from a table named "Persons" where the "FirstName" is "Peter" and the "LastName" is "Jackson"?
: SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'
With SQL, how can you insert a new record into the "Persons" table?
: INSERT INTO Persons VALUES ('Jimmy', 'Jackson')
With SQL, how can you insert "Olsen" as the "LastName" in the "Persons" table?
: INSERT INTO Persons (LastName) VALUES ('Olsen')
How can you change "Hansen" into "Nilsen" in the "LastName" column in the Persons table?
: UPDATE Persons SET LastName='Nilsen' WHERE LastName='Hansen'
Which operator is used to select values within a range?
: BETWEEN
Which operator is used to search for a specified pattern in a column?
: LIKE
Select all records where the value of the City column starts with the letter "a".
: SELECT * FROM Customers WHERE City LIKE 'a%';
Select all records where the value of the City column contains the letter "a".
: SELECT * FROM Customers WHERE City LIKE '%a%;
Select all records where the value of the City column starts with letter "a" and ends with the letter "b".
: SELECT * FROM Customers WHERE City LIKE 'a%b';
Select all records where the second letter of the City is an "a".
: SELECT * FROM Customers WHERE City LIKE '_a%';
Select all records where the first letter of the City is an "a" or a "c" or an "s".
: SELECT * FROM Customers WHERE City LIKE '[acs]%';
Select all records where the first letter of the City starts with anything from an "a" to an "f".
: SELECT * FROM Customers WHERE City LIKE '[a-f]%';