You must master these 7 DDL SQL Statements to supercharge your database skills! Do you know these the most important 7 Data Definition Language Statements? If not, continue reading this article. And Unlock the Power of databases with these very important SQL statements
In this tutorial, we will practically learn the most important 7 DDL OR Data Definition Language SQL statements. The DDL statements are used for describing data and its relationships in a database. The Data Definition Language SQL statements are used to create, modify, and delete database structures such as tables, indexes, and views. This video tutorial teaches you the most important 7 DDL SQL statements.
Create Database DDL SQL Statements
The create database DDL SQL statements is used to crate a database. The SQL statement is pretty straightforward. Enter CREATE DATABASE and then the name of the database, here I’ll use pntutorials. You can use any valid database name you want. For naming a database, follow the general rules like, as MySQL is case sensitive, Use lowercase names only. Do not use space in the name. You can replace space with Underscore. Use alphabets and underscore only. Do not use numbers or any other special character.
CREATE DATABASE pntutorials;
We can confirm newly created database using,
SHOW DATABASES;
Use Database SQL Statement
In MySQL database, before we create tables or store data, we must select or use an existing database. The SQL statement to select an existing database is as follows. After executing the statement, we see a confirmation message that our database has been selected successfully.
USE pntutorials;
Drop Database Statement
As the name suggests, the drop database SQL statement will drop, delete or remove an existing database. The DDL SQL statement to delete an existing database is as follows.
DROP DATABASE test;
Please note: If execute a drop database SQL statement, we will not get any confirmation message. The database, tables and data inside that database will get removed right away. And we can’t restore it back. So be careful before deleting any database with data in it.
Create Table
The create table SQL statement is used to create a database table. The statement contains name of the table space opening parenthesis. Then inside parenthesis we enter the column definitions. Close the parenthesis and end the statement with a semi column. In the following example, we will create a table with 4 columns as follows.
CREATE TABLE students (
id INT,
name VARCHAR(40),
age VARCHAR(10),
class INT
);
We can confirm if the table was created successfully using the following SQL statement.
SHOW TABLES;
Alter Table
While creating a table, if we accidentally defined wrong column names, data types or even table name. It is possible for us to modify an existing table definition. In following example DDL SQL statements, we will change the data type of age column. Here is the example.
ALTER TABLE students MODIFY age INT;
We can confirm the changes by displaying the table definition as follows.
DESCRIBE students;
Rename Table
While creating a table, if we entered a wrong name for the database table. It is possible to change the name of an existing table. Below is an example SQL statement to rename an existing database table.
RENAME TABLE students TO s;
Drop Table
The next on of the most important DDL SQL statements is to drop or remove an existing database table. The example SQL statement is as follows.
DROP TABLE s;
We can confirm if the table was removed successfully using show table SQL statement.
In this article we learned 7 the most important Data Definition Language SQL statements OR DDL statements. If you have any questions, suggestions or doubts, you can ask me in the comments section.