//log my lifestyle

[DATABASE] 생활코딩 - MySQL (2) 본문

Web/기타

[DATABASE] 생활코딩 - MySQL (2)

zici 2022. 5. 17. 00:15

강의 흐름에 따라  학습내용을 기록한 내용입니다.

https://opentutorials.org/course/3161/


1. MySQL 테이블의 생성

세미콜론 없이 엔터시 줄바꿈된다.  

CREATE TABLE topic(
    -> id INT(11) NOT NULL AUTO_INCREMENT,
    -> title VARCHAR(100) NOT NULL,
    -> description TEXT NULL,
    -> created DATETIME NOT NULL,
    -> author VARCHAR(30) NULL,
    -> profile VARCHAR(100) NULL,
    -> PRIMARY KEY(id));

topic 이라는 테이블 생성(
id라는 컬럼생성 /데이터타입=INT /표시되는 길이(11)/ NOT NULL =반드시입력되야함 

VARCHAR (데이터타입중 하나로 variable, character의 약자)

PRIMARY KEY(id) : 메인키가 어느 컬럼(id)인지 전달 - 메인키는 각각의 고유값을 가지게 한다.

 

2. CRUD(Create, Read, Update, Delete)

1) INSERT

create row?

INSERT INTO topic(title,description,created,author,profile) VALUES('MySQL','MySQL is...',NOW(),'egoing','dev');

 NULL인 값은 생략이 가능

 

2) SELECT

SELECT * FROM topic;

 

* 모든 데이터 출력 

또는 출력하고싶은 인덱스만 FROM앞에 써서 원하는 컬럼값만 뽑을수 있음

+----+---------+---------------+---------------------+--------+--------------------+
| id | title   | description   | created             | author | profile            |
+----+---------+---------------+---------------------+--------+--------------------+
|  1 | MySQL   | MySQL is...   | 2022-05-16 07:38:07 | egoing | dev                |
|  2 | ORACLE  | ORACLE is...  | 2022-05-16 07:40:21 | egoing | dev                |
|  3 | MongoDB | MongoDB is... | 2022-05-16 09:38:04 | dura   | data administrator |
+----+---------+---------------+---------------------+--------+--------------------+
3 rows in set (0.00 sec)

mysql> SELECT id,title,created,author FROM topic WHERE author='egoing';
+----+--------+---------------------+--------+
| id | title  | created             | author |
+----+--------+---------------------+--------+
|  1 | MySQL  | 2022-05-16 07:38:07 | egoing |
|  2 | ORACLE | 2022-05-16 07:40:21 | egoing |
+----+--------+---------------------+--------+
2 rows in set (0.00 sec)

 

SELECT id,title,created,author FROM topic  ORDER BY id DESC LIMIT 2;

DESC -내림차순으로 정렬

LIMIT -갯수제한

 

3) UPDATE

 UPDATE topic title='Oracle' WHERE id=2;

주의❗ WHERE문을 빠트리면 전체 행을 바꾸게 된다.

 

4)DELETE

 DELETE FROM topic WHERE id =3;

주의❗❗  WHERE문을 빠트리면 전체 행을 삭제하게 된다.

 

 

 REF

 

https://www.w3schools.com/sql/default.asp

 

SQL Tutorial

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

https://dev.mysql.com/doc/mysql-tutorial-excerpt/8.0/en/tutorial.html

 

MySQL :: MySQL Tutorial :: 1 Tutorial

This chapter provides a tutorial introduction to MySQL by showing how to use the mysql client program to create and use a simple database. mysql (sometimes referred to as the “terminal monitor” or just “monitor”) is an interactive program that enab

dev.mysql.com

  

'Web > 기타' 카테고리의 다른 글

[DATABASE] 생활코딩 - MySQL (3)  (0) 2022.05.19
[DATABASE] 생활코딩 - MySQL (1)  (0) 2022.05.15