0% found this document useful (0 votes)
4 views5 pages

DDLs

The document provides a comprehensive overview of SQL DDL (Data Definition Language) queries, including commands for creating, altering, and dropping databases, tables, and other database objects. It covers various aspects such as constraints, indexes, views, sequences, triggers, stored procedures, and functions, along with examples and explanations for each query type. Additionally, it includes specific commands for dropping, truncating, and renaming database objects.

Uploaded by

muhib.lambay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

DDLs

The document provides a comprehensive overview of SQL DDL (Data Definition Language) queries, including commands for creating, altering, and dropping databases, tables, and other database objects. It covers various aspects such as constraints, indexes, views, sequences, triggers, stored procedures, and functions, along with examples and explanations for each query type. Additionally, it includes specific commands for dropping, truncating, and renaming database objects.

Uploaded by

muhib.lambay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL DDL (Data Definition Language) queries along with their

explanations:

1. CREATE DATABASE Queries

1. CREATE DATABASE School; – Creates a database named "School."


2. CREATE DATABASE IF NOT EXISTS Company; – Creates
"Company" only if it does not already exist.
3. DROP DATABASE School; – Deletes the "School" database.
4. ALTER DATABASE Company READ ONLY = TRUE; – Sets "Company"
database to read-only mode.

2. CREATE TABLE Queries

5. CREATE TABLE Students (ID INT PRIMARY KEY, Name


VARCHAR(100), Age INT); – Creates a "Students" table.
6. CREATE TABLE Employees (ID INT, Salary DECIMAL(10,2),
PRIMARY KEY (ID)); – Creates an "Employees" table with a
primary key.
7. CREATE TABLE Orders (OrderID INT, OrderDate DATE,
CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES
Customers(CustomerID)); – Creates an "Orders" table with a
foreign key.
8. CREATE TABLE IF NOT EXISTS Products (ProductID INT
PRIMARY KEY, Name VARCHAR(50), Price DECIMAL(8,2)); –
Creates a "Products" table only if it does not exist.
9. CREATE TABLE Courses AS SELECT ID, Name FROM Students; –
Creates "Courses" table from "Students."
10. DROP TABLE Students; – Deletes the "Students" table.

3. ALTER TABLE Queries

11. ALTER TABLE Students ADD COLUMN Email VARCHAR(255); –


Adds an "Email" column to "Students."
12. ALTER TABLE Employees DROP COLUMN Salary; – Removes
"Salary" column from "Employees."
13. ALTER TABLE Orders MODIFY COLUMN OrderDate
TIMESTAMP; – Modifies "OrderDate" to TIMESTAMP.
14. ALTER TABLE Products RENAME COLUMN Name TO
ProductName; – Renames "Name" to "ProductName."
15. ALTER TABLE Employees ADD CONSTRAINT chk_salary
CHECK (Salary > 0); – Adds a CHECK constraint for positive
salary.

4. CONSTRAINTS Queries

16. ALTER TABLE Students ADD CONSTRAINT unique_email


UNIQUE (Email); – Adds a UNIQUE constraint to "Email."
17. ALTER TABLE Students DROP CONSTRAINT unique_email; –
Drops the UNIQUE constraint.
18. ALTER TABLE Orders ADD FOREIGN KEY (CustomerID)
REFERENCES Customers(CustomerID); – Adds a foreign key
constraint.
19. ALTER TABLE Employees ADD PRIMARY KEY (ID); – Adds a
primary key to "ID."
20. ALTER TABLE Employees DROP PRIMARY KEY; – Removes
the primary key.

5. INDEX Queries

21. CREATE INDEX idx_name ON Students(Name); – Creates an


index on "Name."
22. DROP INDEX idx_name ON Students; – Drops the index on
"Name."
23. CREATE UNIQUE INDEX idx_email ON Students(Email); –
Creates a unique index on "Email."
24. ALTER TABLE Students DROP INDEX idx_email; – Removes
the unique index.
25. CREATE INDEX idx_order ON Orders(OrderDate DESC); –
Creates an index in descending order.

6. VIEW Queries

26. CREATE VIEW StudentView AS SELECT ID, Name FROM


Students; – Creates a view from "Students."
27. DROP VIEW StudentView; – Deletes the view.
28. ALTER VIEW StudentView AS SELECT ID, Name, Age FROM
Students; – Modifies the view.
29. CREATE OR REPLACE VIEW EmployeeView AS SELECT ID,
Salary FROM Employees; – Creates or replaces a view.
30. SELECT * FROM StudentView; – Retrieves data from the
view.

7. SEQUENCE Queries

31. CREATE SEQUENCE seq_student START WITH 1 INCREMENT


BY 1; – Creates a sequence starting from 1.
32. DROP SEQUENCE seq_student; – Deletes the sequence.
33. SELECT NEXTVAL('seq_student'); – Fetches the next value.
34. ALTER SEQUENCE seq_student RESTART WITH 10; – Restarts
sequence at 10.
35. CREATE SEQUENCE seq_emp MINVALUE 1 MAXVALUE 100
CYCLE; – Creates a sequence with limits and cycles.

8. TRIGGER Queries

36.
CREATE TRIGGER trg_before_insert BEFORE INSERT ON Students FOR EACH ROW
SET NEW.Name = UPPER(NEW.Name);

– Converts "Name" to uppercase before insertion.


37. `DROP TRIGGER trg_before_insert;` – Deletes the trigger.
38. ```sql
CREATE TRIGGER trg_after_update
AFTER UPDATE ON Employees
FOR EACH ROW
INSERT INTO AuditTable (EmpID, ChangeDate) VALUES (OLD.ID,
NOW());

– Logs updates to an audit table.


39. ALTER TRIGGER trg_before_insert ENABLE; – Enables a trigger.
40. ALTER TRIGGER trg_before_insert DISABLE; – Disables a trigger.
9. STORED PROCEDURE Queries

41.

CREATE PROCEDURE GetStudents() BEGIN SELECT * FROM


Students; END;
– Creates a stored procedure to fetch students.
42. `DROP PROCEDURE GetStudents;` – Deletes the stored
procedure.
43. ```sql
CREATE PROCEDURE InsertStudent(IN name VARCHAR(100), IN age
INT)
BEGIN
INSERT INTO Students(Name, Age) VALUES(name, age);
END;

– Inserts a student using a stored procedure.


44. CALL InsertStudent('Alice', 22); – Calls the stored procedure.
45. ```sql CREATE PROCEDURE DeleteStudent(IN studentID INT)
BEGIN DELETE FROM Students WHERE ID = studentID; END;
– Deletes a student by ID using a stored procedure.

### **10. FUNCTION Queries**


46. ```sql
CREATE FUNCTION GetStudentCount() RETURNS INT
BEGIN
DECLARE count INT;
SELECT COUNT(*) INTO count FROM Students;
RETURN count;
END;

– Creates a function to return the student count.


47. SELECT GetStudentCount(); – Calls the function.
48. DROP FUNCTION GetStudentCount; – Deletes the function.
49. ```sql CREATE FUNCTION GetMaxSalary() RETURNS
DECIMAL(10,2) BEGIN RETURN (SELECT MAX(Salary) FROM
Employees); END;
– Retrieves the maximum salary.
50. `SELECT GetMaxSalary();` – Calls the function.
Here are SQL queries for DROP, TRUNCATE, and RENAME:

DROP Queries (Used to permanently remove objects from the database)

1. DROP TABLE Students; – Deletes the "Students" table completely.


2. DROP DATABASE School; – Deletes the "School" database.
3. DROP VIEW StudentView; – Removes the "StudentView" view.
4. DROP INDEX idx_name ON Employees; – Deletes the "idx_name" index on
"Employees."
5. DROP SEQUENCE seq_student; – Removes the "seq_student" sequence.

TRUNCATE Queries (Used to remove all records from a table but keep the
structure)

1. TRUNCATE TABLE Students; – Removes all data from the "Students" table.
2. TRUNCATE TABLE Employees; – Deletes all rows from the "Employees" table.
3. TRUNCATE TABLE Orders; – Clears all records in the "Orders" table.
4. TRUNCATE TABLE Products; – Removes all rows in the "Products" table while
keeping the schema.
5. TRUNCATE TABLE Customers RESTART IDENTITY; – Deletes all records and resets
identity columns.

RENAME Queries (Used to change names of tables, columns, or database


objects)

1. ALTER TABLE Students RENAME TO Learners; – Renames "Students" table to


"Learners."
2. ALTER TABLE Employees RENAME COLUMN Name TO FullName; – Changes column
"Name" to "FullName."
3. ALTER VIEW StudentView RENAME TO LearnerView; – Renames "StudentView" to
"LearnerView."
4. ALTER INDEX idx_name RENAME TO idx_fullname; – Renames index "idx_name"
to "idx_fullname."
5. ALTER SEQUENCE seq_student RENAME TO seq_learner; – Renames sequence
"seq_student" to "seq_learner."

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy