Data Definition Commands-1
Data Definition Commands-1
Introduction to DDL
• Data Definition Language (DDL) is used to
define the structure of a database. It includes
commands like CREATE, ALTER, DROP,
TRUNCATE, and RENAME.
CREATE Command
• Syntax:
• CREATE TABLE table_name (
• column1 datatype constraints,
• column2 datatype constraints,
• ...
• );
• Conditions:
• - Table name must be unique.
• - Column names should be valid identifiers.
ALTER Command
• Syntax:
• ALTER TABLE table_name ADD column_name
datatype;
• ALTER TABLE table_name DROP COLUMN
column_name;
• Conditions:
• - Column name should not conflict with existing
ones.
• - Only allowed modifications can be performed.
DROP Command
• Syntax:
• DROP TABLE table_name;
• Conditions:
• - The table must exist.
• - This action permanently deletes the table
and cannot be undone.
TRUNCATE Command
• Syntax:
• TRUNCATE TABLE table_name;
• Conditions:
• - The table remains but all data is removed.
• - Cannot be rolled back in some database
systems.
RENAME Command
• Syntax:
• RENAME TABLE old_name TO new_name;
• Conditions:
• - The old table name must exist.
• - The new name should not conflict with
existing tables.
Conclusion
• DDL commands define database structure and
play a crucial role in database management.
Understanding their syntax and conditions
ensures proper database design.
Data Manipulation Commands
Introduction to Data Manipulation Language (DML)
Data Manipulation Language (DML) is used to manage data within
tables.Common DML commands:
○ SELECT
○ INSERT
○ UPDATE
○ DELETE
DML commands affect the data but do not modify the structure of the
table.
SELECT Command: Retrieves data from a database. Syntax:
SELECT column1, column2 FROM table_name WHERE condition;
Example: Retrieve all employees from the "employees" table:
SELECT * FROM employees;
INSERT Command: Adds new records to a table
Syntax:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Example: Add a new employee:
INSERT INTO employees (id, name, salary) VALUES (101, 'John Doe',
50000);
Importance of DCL
Syntax:
GRANT privileges ON object TO user;
REVOKE Command
Example:
CREATE ROLE manager;
Revoking Roles
Example:
Syntax:
SELECT column FROM table1
UNION ALL
Example:
Syntax:
SELECT column FROM table1
INTERSECT
SELECT column FROM table2;
Example:
SELECT name FROM Employees
INTERSECT
SELECT name FROM Customers;
EXCEPT Operation:Returns records from the first query that are not
in the second query.
Syntax:
SELECT column FROM table1
EXCEPT
SELECT column FROM table2;
Example:
Rules and Considerations
2. UNION ALL
3. INTERSECT
Syntax
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
Keeps duplicates in the result.
INTERSECT
Returns only common records between two
queries.Both queries must have the same number of
columns with the same data types.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Function Description
count () count no of row
sum () sum the values of row
avg() computes avg
max() returns max value
min() returns min value
Example:
Syntax
create view SPIT as <query expression>
Create StudentDetails table
CREATE TABLE StudentDetails (
S_ID INT PRIMARY KEY,
NAME VARCHAR(255),
ADDRESS VARCHAR(255)
);
INSERT INTO StudentDetails (S_ID, NAME, ADDRESS)
VALUES
(1, 'Harsh', 'Kolkata'),
(2, 'Ashish', 'Durgapur'),
(3, 'Pratik', 'Delhi'),
(4, 'Dhanraj', 'Bihar'),
OUTPUT
Creating View From Multiple Tables
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
USE "database_name";
SHOW FULL TABLES
WHERE table_type LIKE "%VIEW";
Using information_schema
SELECT table_name
FROM information_schema.views
WHERE table_schema = 'database_name';
OR
SELECT table_schema, table_name, view_definition
FROM information_schema.views
WHERE table_schema = 'database_name';
Operation on Views
Delete View in SQL
DROP VIEW view_name;
if we now try to insert a new row with a null value in the NAME column
then it will give an error because the view is created with the condition
for the NAME column as NOT NULL.
SQL Trigger
Key Terms
Example:
CREATE TRIGGER prevent_table_creation
ON DATABASE
FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE
AS
BEGIN
PRINT 'you can not create, drop and alter table in this database';
ROLLBACK;
END;
2. DML Triggers:The Data manipulation Language (DML) command
events that begin with Insert, Update, and Delete set off the DML
triggers. DML triggers are used for data validation, ensuring that
modifications to a table are done under controlled conditions.
CREATE TRIGGER prevent_update
ON students
FOR UPDATE
AS
BEGIN
PRINT 'You can not insert, update and delete this table i';
ROLLBACK;
END;
3. Log on Triggers:response to logon events. Logon triggers are
useful for monitoring user sessions or restricting user access to the
database.As a result, the PRINT statement messages and any errors
generated by the trigger will all be visible in the SQL Server error log.
Authentication errors prevent logon triggers from being used. These
triggers can be used to track login activity or set a limit on the
number of sessions that a given login can have in order to audit and
manage server sessions.
Syntax
SELECT name, is_instead_of_trigger
FROM sys.triggers
WHERE type = ‘TR’;
Key Terms
mysql>>desc Student;
THANK YOU