Skip to content

DDL - MySQL Conclusion

Conclusion for DDL (Structural Blueprint / Define) SQL Syntax for MySQL.

  • DDL = Data Definition Language

Database

DDL - for db

KeywordsSyntaxDescriptionLink
SHOWSHOW DATABASES;Show all of the database in MySQLClick Here
CREATECREATE DATABASE IF NOT EXISTS <db_name>;Create Database If it's not exist in MYSQLClick Here
DROPDROP DATABASE IF EXISTS <db_name>;Drop Database if it's existedClick Here
USEUSE <db_name>;Change the future actions towards a specific database.Click Here
SELECTSELECT DATABASE();Get / Check current database in USEClick Here

Table

DDL for tables

KeywordsSyntaxDescriptionLink
CREATE TABLECREATE TABLE IF NOT EXIST <table_name>(attr1 type_attr1 comment "optional comment");Create a table if not exist.Click Here
SHOWSHOW TABLES;Show all tables in a databaseClick Here
DESCDESC <table_name>;Describe / show the structure of the tables.Click Here
SHOW CREATE TABLESHOW CREATE TABLE <table_name>;Show the SQL Code of creating an tables (Already exist)Click Here

Alter Table (Modify Structure)

KeywordsSyntaxDescriptionLink
ALTER TABLE ... ADDALTER TABLE <table_name> ADD <new_field> <field_data_type>;Add a new field for a table existed.Click Here
ALTER TABLE ... MODIFYALTER TABLE <table_name> MODIFY <field_name> <NEW_DATA_TYPE>;Modify the data type for an existed data fieldClick Here
ALTER TABLE ... CHANGEALTER TABLE <table_name> CHANGE <OLD_FIELD_NAME> <NEW_FIELD_NAME> <data_type>; Change / Rename the name of an current existed data field.Click Here
ALTER TABLE ... DROPALTER TABLE <table_name> DROP <field_name>;Delete the data field in a tableClick Here
ALTER TABLE ... RENAME TO ...ALTER TABLE <table_name> RENAME TO <new_table_name>;Change / Rename the name of tableClick Here
TRUNCATE TABLE ...TRUNCATE TABLE <table_name>;Delete the table and recreate the tableClick Here
DROP TABLE IF EXISTS ...DROP TABLE IF EXISTS <table_name>;Delete the tableClick Here

Flash Cards

Flash card - ddl sql