-
생활코딩( Database2 MySQL ) (5) - CRUD of InsertBackend/MYSQL 2021. 5. 31. 23:00
■ Insert
다음 syntax를 사용
INSERT INTO <table name>(parm1, parm2, ...) VALUES(var1, var2, ...);
1) SQL script
-- select opentutorials schema USE opentutorials; -- show tables in schema SHOW TABLES; -- show row profile DESC topic; -- insert a row(data) in the table /* 1) id is a skip! 2) tabale parameters must be wrapped with VAULES() 3) NOW() is a function to call and get current time-date 4) ' ' + \ will allow you to write sql on the next line as one */ INSERT INTO topic (title, description, created, author, profile) \ VALUES('MySQL', 'MySQL is...', NOW(), 'HJ Yang', 'developer'); -- selecting all the data from topic SELECT * FROM topic;
1) \! 문은 cmd로 실행시 cmd 문 자체를 실행시킬 수 있게 해주는 인식자
2) id는 primary에 auto_increment이기 때문에 VALUES의 param에 속하지 않음
3) table에 들어갈 데이터의 param들은 VALUES로 감싸야 함
4) comment -- 또는 /* */ 사용
5) 다음 줄에 continue하는 것은 double space + \
> 다음이 출력 됨
"use opentutorials schema!"
Database changed
""
"show tables!"
+-------------------------+
| Tables_in_opentutorials |
+-------------------------+
| topic |
+-------------------------+
1 row in set (0.00 sec)
""
"describe the topic table!"
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| title | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| created | datetime | NO | | NULL | |
| author | varchar(15) | YES | | NULL | |
| profile | varchar(200) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
""
"create a row into the table!"
Query OK, 1 row affected (0.01 sec)
""
"reading a row in the table!"
"- select all data"
+----+-------+-------------+---------------------+---------+-----------+
| id | title | description | created | author | profile |
+----+-------+-------------+---------------------+---------+-----------+
| 1 | MySQL | MySQL is... | 2021-05-31 22:31:50 | HJ Yang | developer |
| 2 | MySQL | MySQL is... | 2021-05-31 22:34:53 | HJ Yang | developer |
| 3 | MySQL | MySQL is... | 2021-05-31 22:35:47 | HJ Yang | developer |
+----+-------+-------------+---------------------+---------+-----------+
3 rows in set (0.00 sec)
""
mysql>
참조 :
https://opentutorials.org/course/3161/19539
반응형'Backend > MYSQL' 카테고리의 다른 글
생활코딩( Database2 MySQL ) (7) - CRUD of Update (0) 2021.06.02 생활코딩( Database2 MySQL ) (6) - CRUD of Select (0) 2021.06.02 생활코딩( Database2 MySQL ) (4) - SQL과 테이블 구조 & 테이블 생성 (0) 2021.05.30 생활코딩( Database2 MySQL ) (3) - MySQL 서버와 접속 / Schema 생성과 사용 (0) 2021.05.30 생활코딩( Database2 MySQL ) (2) - MySQL 기본 구조 (0) 2021.05.27