[DB/SQL] DDL

1 분 소요

CREATE command

CREATE DATABASE SOMETHING

I made SOMETHING database.

create tables

I should choice database to create tables. like this.

USE SOMETHING

/* when you want to know what the current using database is:
SELECT DATABASE();
*/

To make a table,

CREATE TABLE table_name(
	'field_name' data_type attributes,
	~
);

ALTER command

ALTER command is used when you want to fix the entity(add field or set limits on a table).

add field

ALTER TABLE table_name ADD field_name data_type;

/*after this, use DESC to check lists of table fields.
DESC table_name;
*/

if you use AFTER keyword, you can add the field next to certain field you want. with FIRST keyword, you add the field at the first of the table.

modify field

To change name of field, use ALTER and CHANGE keyword like this

ALTER TABLE table_name CHANGE original_name new_name original_data_type;

To modify data type of certain field, use MODIFY keyword.

ALTER TABLE table_name MODIFY field_name new_data_type;

To delete certain field, use DROP keyword.

ALTER TABLE table_name DROP field_name;

To change table name, use RENAME keyword.

ALTER TABLE table_name RENAME new_table_name;

DROP command

Drop command is used when you delete certain entity. You can also use DROP as keyword with ALTER.

DROP TABLE table_name;

TRUNCATE command

TRUNCATE command delete all of records of certain tables. It keeps fields.

TRUNCATE TABLE table_name;

The end

태그: ,

카테고리:

업데이트:

댓글남기기