Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

MySQL - Day 02 - DDL Commands

All tha ddl command

Uploaded by

sanikaayare79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

MySQL - Day 02 - DDL Commands

All tha ddl command

Uploaded by

sanikaayare79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MySQL Day 02

Introduction to MySQL and Important DDL Commands


Types of SQL Commands

1. Data Definition Language (DDL) :


MySQL Data Definition Language (DDL) commands are used to create, modify, and
delete database objects such as tables, indexes, and views.

2. Data Manipulation Language (DML) :


Data Manipulation Language (DML) is a set of MySQL commands used to
manipulate data in a database. DML commands are used to insert, update, and delete
data in a table, as well as retrieve data using SELECT statements.

3. Data Control Language (DCL) :


MySQL DCL (Data Control Language) commands are used to manage user access to
a database. DCL commands allow database administrators to create, modify, and remove
users, and grant or revoke privileges to control access to the database.

4. Transaction Control Language (TCL) :


MySQL Transaction Control Language (TCL) commands are used to control
transactions in a MySQL database. Transactions are a set of operations that are
performed as a single unit of work. A transaction can contain one or more DML (Data
Manipulation Language) commands such as INSERT, UPDATE, DELETE, and SELECT.
Common DDL Commands

❑ CREATE: used to create databases & new objects in the database, such as tables, views,
indexes, and stored procedures.

❑ ALTER: This command is used to modify existing objects in the database.

❑ DROP: This command is used to delete objects from the database.

❑ 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.

❑ NOT NULL: Ensures that a column cannot contain 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.

There are 4 Referential actions that can be defined in MySQL

❑ 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

We use CREATE command to create a database and Table as well in MySQL.

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

Inserting Values into Table The Table is Created!

We use INSERT command to populate table in our database. Note the Syntax.

Syntax is INSERT INTO <table_name> (col_1,col_2,col_3) VALUES (val_1,val_2,val_3);


ALTER & AUTO_INCREMENT & related constraints

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

❑ Keep in mind the previous slide that has ‘students’ table.

❑ 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

❑ This is one example of CASCADE for understanding the


concept.

❑ We create a new ‘student’ & ‘classes’ tables with PRIMARY


KEY & FOREIGN KEY. (left side image)

❑ Now we try applying CASCADE on a ‘students’ table and a


‘classes’ table with an ‘ON DELETE CASCADE’ constraint
on the ‘student_id’ foreign key column.

❑ We then delete a row from ‘students table’ to verify the


CASCADE constraint (right side image)

❑ In the output, we can see that the corresponding rows in


the ‘classes’ tables are also deleted, and the CASCADE
constraint has worked perfectly.
DROP
Here, We can see ‘mytable’ table in
‘mydatabase’ database.

Here, We Drop the Database, and


Notice the SCHEMAS section, we
no longer have the ‘mydatabase’
database

❑ We used DROP command to delete


‘mytable’ table from the ‘mydatabase’
database (Notice the SCHEMAS section
in the image*)

❑ See the Action Output Section, After


DROP TABLE command, the Table gets
deleted and Workbench notifies that the
Table doesn’t Exist after using SELECT
command (Blue arrow in the image)
CONCLUSION

❑ 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.

Have a Nice Time!

You might also like