๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

๐Ÿ—ƒ๏ธ๋‚˜์˜ SQL : ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ์˜ ์‹œ์ž‘-ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌํ•˜๊ธฐ ๋ณธ๋ฌธ

DB/๐ŸฌMySQL

๐Ÿ—ƒ๏ธ๋‚˜์˜ SQL : ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ์˜ ์‹œ์ž‘-ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌํ•˜๊ธฐ

4:Bee 2023. 9. 12. 00:26
728x90

์šฐ๋ฆฌ๊ฐ€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” TABLE์„ ๋ถ„๋ฆฌํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ธฐ์กด์˜ TABLE์˜ ๋ช…์นญ์„ ๋ณ€๊ฒฝํ•ด์„œ ์ƒˆ๋กœ ์ €์žฅํ•  ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

RENAME TABLE topic TO topic_backup;

MariaDB [opentutorials]> RENAME TABLE topic TO topic_backup;
Query OK, 0 rows affected (0.018 sec)

MariaDB [opentutorials]> SHOW TABLES;
+-------------------------+
| Tables_in_opentutorials |
+-------------------------+
| topic_backup            |
+-------------------------+
1 row in set (0.001 sec)

 

์ดํ›„ ์šฐ๋ฆฌ๋Š” ์ „์— ์‚ฌ์šฉํ–ˆ๋˜ topic์ด๋ž€ TABLE์„ ์ƒˆ๋กœ์šด ํ˜•ํƒœ๋กœ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉฐ ๊ธฐ์กด TABLE์„ ๋น„๊ตํ•ด๋ณด์ž.

CREATE TABLE topic(-);

MariaDB [opentutorials]> CREATE TABLE topic (
    ->   id int(11) NOT NULL AUTO_INCREMENT,
    ->   title varchar(30) NOT NULL,
    ->   description text,
    ->   created datetime NOT NULL,
    ->   author_id int(11) DEFAULT NULL,
    ->   PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.017 sec)

 

topic_backup TABLE ๊ณผ topic TABLE ๋น„๊ต

MariaDB [opentutorials]> DESC topic_backup;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(100) | NO   |     | NULL    |                |
| description | text         | YES  |     | NULL    |                |
| created     | datetime     | NO   |     | NULL    |                |
| author      | varchar(30)  | YES  |     | NULL    |                |
| profile     | varchar(100) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.025 sec)

MariaDB [opentutorials]> DESC topic;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| title       | varchar(30) | NO   |     | NULL    |                |
| description | text        | YES  |     | NULL    |                |
| created     | datetime    | NO   |     | NULL    |                |
| author_id   | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
5 rows in set (0.024 sec)

๋งˆ์ง€๋ง‰์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ topic์—์„œ author_id๊ฐ€ ์ฐธ์กฐํ•  TABLE์„ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž

 

CREATE TABLE author(-);

MariaDB [opentutorials]> CREATE TABLE author (
    ->   id int(11) NOT NULL AUTO_INCREMENT,
    ->   name varchar(20) NOT NULL,
    ->   profile varchar(200) DEFAULT NULL,
    ->   PRIMARY KEY (id)
    -> );
    
 MariaDB [opentutorials]> DESC author;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20)  | NO   |     | NULL    |                |
| profile | varchar(200) | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.023 sec)

 

์™ผ์ชฝ์€ SELECT * FROM author; ์˜ค๋ฅธ์ชฝ์€ ์œ„์—์„œ SELECT * FROM topic; DESC topic;

์šฐ๋ฆฌ๊ฐ€ ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ค์ •ํ•ด๋†”์•ผ ํ•œ๋‹ค. 

๋ฐ์ดํ„ฐ ๊ฐ’

INSERT INTO topic(-) VALUES(-);

MariaDB [opentutorials]> INSERT INTO topic(id, title, description, created, author_id) 
VALUES(1, 'MySQL', 'MySQL is...', '2018-1-1 12:10:11', 1);
Query OK, 1 row affected (0.004 sec)

MariaDB [opentutorials]> INSERT INTO topic(id, title, description, created, author_id) 
VALUES(2, 'Oracle', 'Oracle is...', '2018-1-03 13:01:10', 1);
Query OK, 1 row affected (0.002 sec)

MariaDB [opentutorials]> INSERT INTO topic(id, title, description, created, author_id) 
VALUES(3, 'SQL Server', 'SQL Server is...', '2018-01-20 11:01:10', 2);
Query OK, 1 row affected (0.004 sec)

MariaDB [opentutorials]> INSERT INTO topic(id, title, description, created, author_id) 
VALUES(4, 'PostgreSQL', 'PostgereSQL is...', '2018-01-23 1:3:3', 3);
Query OK, 1 row affected (0.005 sec)

MariaDB [opentutorials]> INSERT INTO topic(id, title, description, created, author_id) 
VALUES(5, 'MongoDB', 'MongoDB is...', '2018-01-30 12:31:3', 1);
Query OK, 1 row affected (0.004 sec)

MariaDB [opentutorials]> SELECT * FROM topic;
+----+------------+-------------------+---------------------+-----------+
| id | title      | description       | created             | author_id |
+----+------------+-------------------+---------------------+-----------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 |         1 |
|  2 | Oracle     | Oracle is...      | 2018-01-03 13:01:10 |         1 |
|  3 | SQL Server | SQL Server is...  | 2018-01-20 11:01:10 |         2 |
|  4 | PostgreSQL | PostgereSQL is... | 2018-01-23 01:03:03 |         3 |
|  5 | MongoDB    | MongoDB is...     | 2018-01-30 12:31:03 |         1 |
+----+------------+-------------------+---------------------+-----------+
5 rows in set (0.001 sec)

INSERT INTO author (-) VALUES(-);

MariaDB [opentutorials]> INSERT INTO author (id, name, profile) 
VALUES(1, 'egoing', 'developer');
Query OK, 1 row affected (0.003 sec)

MariaDB [opentutorials]> INSERT INTO author (id,name,profile) 
VALUES(2, 'duru', 'data administrator');
Query OK, 1 row affected (0.003 sec)

MariaDB [opentutorials]> INSERT INTO author (id,name,profile) 
VALUES(3, 'taeho', 'data scientist, developer');
Query OK, 1 row affected (0.003 sec)

MariaDB [opentutorials]> SELECT * FROM author;
+----+--------+---------------------------+
| id | name   | profile                   |
+----+--------+---------------------------+
|  1 | egoing | developer                 |
|  2 | duru   | data administrator        |
|  3 | taeho  | data scientist, developer |
+----+--------+---------------------------+
3 rows in set (0.000 sec)
 

ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌํ•˜๊ธฐ - ์ƒํ™œ์ฝ”๋”ฉ

ํ…Œ์ด๋ธ” ๋ถ„๋ฆฌํ•˜๊ธฐ 2018-02-10 15:56:41 -- -- Table structure for table `author` -- CREATE TABLE `author` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `profile` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ); -- -- Dumping data

opentutorials.org

 

728x90
Comments