MySQL - Day 02 - DDL Commands
MySQL - Day 02 - DDL Commands
❑ CREATE: used to create databases & new objects in the database, such as tables, views,
indexes, and stored procedures.
❑ TRUNCATE: This command is used to delete all the data in a table, but not the table structure
itself.
❑ RENAME: This command is used to rename a database object, such as a table or column.
❑ COMMENT: This command is used to add comments to a database object, such as a table,
column, or index.
❑ CONSTRAINT: This command is used to enforce rules and restrictions on the data stored in a
table, such as unique, primary key, and foreign key constraints.
❑ INDEX: This command is used to create indexes on one or more columns of a table, which can
speed up data retrieval.
Common Constraints
❑ PRIMARY KEY: Used to uniquely identify each record in a table. A primary key column cannot
contain null values, and each value must be unique.
❑ FOREIGN KEY: Used to establish a relationship between two tables. The foreign key column in
one table is linked to the primary key column in another table. This ensures that data entered
into the foreign key column matches data in the primary key column.
❑ UNIQUE: Ensures that the values in a column are unique, but allows null values.
❑ CHECK: Used to specify a condition that must be met for data to be entered into a column.
❑ AUTO_INCREMENT: used to automatically generate a unique value for a column when a new
row is inserted into a table. It is commonly used with the primary key column to ensure that
each row has a unique identifier.
❑ DEFAULT: Used to specify a default value for a column when a new row is inserted into a table,
if no value is explicitly specified for that column. The DEFAULT constraint can be used with any
data type in MySQL.
Referential Actions
Referential Actions in MySQL are used to define actions that should be taken when a row in a
‘parent table’ is updated or deleted, and there are rows in ‘child tables’ that reference the parent
row.
❑ CASCADE: If a row in the parent table is updated or deleted, all related rows in the child table
will also be updated or deleted, respectively.
❑ SET NULL: If a row in the parent table is updated or deleted, all related rows in the child table
will have their foreign key column(s) set to NULL.
❑ RESTRICT: If a row in the parent table is updated or deleted, and there are related rows in the
child table, the update or delete operation will be prevented.
❑ NO ACTION: This is similar to RESTRICT, and is the default behavior if no referential action is
specified.
Referential actions help to ensure data consistency and accuracy in the database by
automatically updating or deleting related rows in child tables when a row in a parent table is
updated or deleted.
CREATE with related CONSTRAINTS
Creating a Database Creating a Table
In the right side image, We have created a new table called "mytable" within the
"mydatabase" database. The table has three columns: "id", "name", and "age". The "id"
column is defined as the primary key of the table, and will contain unique integer values.
The "name" column is defined as a string with a maximum length of 50 characters, and the
"age" column is defined as an integer.
Inserting Values into table
We use INSERT command to populate table in our database. Note the Syntax.
In this example, the "id" column is modified to be AUTO_INCREMENT, and the subsequent
INSERT statement inserts a row with values for the "name" and "age" columns, but not for
the "id" column. MySQL will automatically generate a unique value for the "id" column when
the row is inserted.
PRIMARY KEY
In this example, we've defined the "id" column as the primary key of the "students" table. The
"id" column is of the integer data type, and will contain a unique value for each student.
Because the "id" column is the primary key, we must ensure that the "id" value is unique. If we
try to insert a row with a duplicate "id" value, the INSERT statement will fail with an error.
FOREIGN KEY
❑ We create another table ‘classes’ having info about classes offered at school.
❑ We have to associate each class with ‘students’ enrolled in it using FOREIGN KEY as above.
❑ We have to make sure that we enter the values in the ‘classes’ table associated with the relevant
‘student_id’ in previous ‘student’ table, or else the INSERT statement will fail with an error.
CASCADE
❑ We have seen SOME of the most commonly used and Important DDL Commands used in MySQL.
❑ In no way this is a comprehensive presentation, but it definitely refreshes the most important DDL
concepts and commands that we may come across in our day-to-day working with MySQL in our Data
Analysis endeavor.
❑ In the Next presentation, we will Learn about most commonly used DML concepts.