sql cookbook

Create and Alter user

1
2
CREATE USER '<USER_NAME>'@'localhost' IDENTIFIED BY 'password'; --CREATE USER WITH PASSWORD--
GRANT ALL ON *.* TO '<USER_NAME>'@'%' WITH GRANT OPTION; --GRANT USER PERMISSION--

ALTER TABLE

1
2
3
ALTER TABLE <TABLE_NAME> CHANGE <COLUMN1> <COLUMN2> <DATATYPE> --change column name--
ALTER TABLE <TABLE_NAME> DROP <COLUMN_NAME>
ALTER TABLE <TABLE_NAME> MODIFY <COLUMN_NAME> <DATA_TYPE> NOT NULL PRIMARY KEY

INSERT STATEMENT

존재하지 않는 것만 추가하기

1
2
3
4
5
INSERT INTO <TABLE> (<COLUMN1>, <COLUMN2>) 
VALUES (<VALUE1>, <VALUE2>)
WHERE NOT EXISTS (
SELECT <COLUMN> FROM <TABLE2> WHERE id=<value>
)

SELECT STATEMENT

LIKE

1
SELECT * FROM <table_name> WHERE <COLUMN> LIKE "%<CONTENT>%";

ORDER BY

1
SELECT * FROM <TABLE_NAME> WHERE <CONDITION> ORDER BY <COLUMN1> DESC, <COLUMN2> ASC

LIMIT

1
SELECT * FROM <TABLE> LIMIT <INDEX>, <NUMBER>

GROUP BY

1
2
3
4
SELECT u.name, MAX(c.price)
FROM user u LEFT JOIN car c ON u.id = c.user_id
WHERE u.id = 4;
GROUP BY u.name

PROBLEM SOLVING

where 절 없이 업데이트 실패하는 경우
1
SET SQL_SAFE_UPDATES = 0;
가치투자 시작하기 Unix CLI commands

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×