DISCLAIMER: The Examples in this Tutorial use PostgreSQL. It's highly recommended that you download this application at the link below to follow along:
Download PostgreSQL
Now that you have a clear understanding of the core concepts behind Relational Databases, let's learn some basic
SQL. Data Definition Language (DDL) is a form of SQL used
specifically for creating, altering, and dropping database objects. Listed below are the learning objectives
covered in this lesson:
CREATE DATABASE
statement is used to define the name of the new database. This statement has the following syntax: CREATE DATABASE [db_name];
CREATE TABLE
statement is used to create a table in a database using SQL. This statement has the following syntax:CREATE TABLE [table_name] ([column1] [data_type], [column2] [data type], etc.);
IF NOT EXISTS
keyword. This has the following syntaxes:CREATE DATABASE IF NOT EXISTS [db_name];
CREATE TABLE IF NOT EXISTS [table_name] ([column1] [data_type]...);
DROP TABLE
statement. This statement has the following syntax:
DROP TABLE [table_name];
CREATE TABLE
statement, you can use the IF NOT EXISTS
keyword to avoid any coding errors. However, when dropping tables it's likely you'll want to use the IF EXISTS
keyword to make an actual change in your database.DROP TABLE
statement:
DROP TABLE [table_name1], [table_name2], [table_name3];
ALTER TABLE
statement. This statement has the following syntax:
ALTER TABLE [table_name] ADD COLUMN [column_name] [data_type];
ADD COLUMN
keyword with a comma: ALTER TABLE ADD COLUMN [column_name1] [data_type], ADD COLUMN [column_name2] [data_type], etc.;
CREATE TABLE
statement, use the following syntax: CREATE TABLE [child_table_name] ([column_name] [data_type], [column_name] [data type], FOREIGN KEY ([column_name]) REFERENCES [parent_table_name]([primary_key]));
CREATE TABLE
statement, this has the following syntax: CREATE TABLE [table_name] ([column_name] [data_type], [column_name] [data_type] CHECK([column_name] IN([value1], [value2], [value3])));
CREATE TABLE
statement, this has the following syntax: CREATE TABLE [table_name] ([column_name] [data_type], [column_name] [data_type] DEFAULT [default_value]);
Congratulations! You just completed the DDL Tutorial! We hope you learned a lot and feel more prepared to enter the workforce. To help test your knowledge, let's complete a simple exercise.
Have any issues with the above exercise? Post your question on Discord!