0% found this document useful (0 votes)
24 views

DDL DML Commands-Ece-A1

Uploaded by

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

DDL DML Commands-Ece-A1

Uploaded by

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

VI SEM-ECE/FDBMS/27-3-2024/Assignment-1

DDL, DML and DCL commands in SQL:


DDL commands:DDL, stands for Data Definition Language, is a subset of SQL (Structured
Query Language) commands used to define and modify the database structure. These
commands are used to create, alter, and delete database objects like tables, indexes, and
schemas. The primary DDL commands in SQL include:

1. CREATE: This command is used to create a new database object. For


example, creating a new table, a view, or a database.
• Syntax for creating a table: CREATE TABLE table_name
(column1 datatype, column2 datatype, ...);
2. ALTER: This command is used to modify an existing database object, such as
adding, deleting, or modifying columns in an existing table.
• Syntax for adding a column in a table: ALTER TABLE table_name
ADD column_name datatype;
• Syntax for modifying a column in a table: ALTER TABLE
table_name MODIFY COLUMN column_name datatype;
3. DROP: This command is used to delete an existing database object like a
table, a view, or other objects.
• Syntax for dropping a table: DROP TABLE table_name;
4. TRUNCATE: This command is used to delete all data from a table, but the
structure of the table remains. It’s a fast way to clear large data from a table.
• Syntax: TRUNCATE TABLE table_name;
5. RENAME: Used to rename an existing database object.
• Syntax: RENAME TABLE old_table_name TO new_table_name;
DDL commands play a crucial role in defining the database schema.

Data Manipulation Language (DML) Commands in SQL

Data Manipulation Language (DML) is a subset of SQL commands used for adding
(inserting), deleting, and modifying (updating) data in a database. DML commands are
crucial for managing the data within the tables of a database. The primary DML commands in
SQL include:

1. INSERT: This command is used to add new rows (records) to a table.


• Syntax: INSERT INTO table_name (column1, column2, column3,
...) VALUES (value1, value2, value3, ...);
2. UPDATE: This command is used to modify the existing records in a table.
• Syntax: UPDATE table_name SET column1 = value1, column2 =
value2, ... WHERE condition;
• The WHERE clause specifies which records should be updated.
Without it, all records in the table will be updated.
3. DELETE: This command is used to remove one or more rows from a table.
• Syntax: DELETE FROM table_name WHERE condition;
• Like with UPDATE, the WHERE clause specifies which rows
should be deleted. Omitting the WHERE clause will result in all
rows being deleted.
4. SELECT: Although often categorized separately, the SELECT command is
sometimes considered part of DML as it is used to retrieve data from the
database.
• Syntax: SELECT column1, column2, ... FROM table_name
WHERE condition;
• The SELECT statement is used to query and extract data from a
table, which can then be used for various purposes.
Data Control Language (DCL) Commands in SQL

Data Control Language (DCL) is a subset of SQL commands used to control access to data in
a database. DCL is crucial for ensuring security and proper data management, especially in
multi-user database environments. The primary DCL commands in SQL include:

1. GRANT: This command is used to give users access privileges to the


database. These privileges can include the ability to select, insert, update,
delete, and so on, over database objects like tables and views.
• Syntax: GRANT privilege_name ON object_name TO user_name;
• For example, GRANT SELECT ON employees TO
user123; gives user123 the permission to read data from
the employees table.
2. REVOKE: This command is used to remove previously granted access
privileges from a user.
• Syntax: REVOKE privilege_name ON object_name FROM
user_name;
• For example, REVOKE SELECT ON employees FROM
user123; would remove user123‘s permission to read data from
the employees table.
DCL commands are typically used by database administrators. When using these commands,
it’s important to carefully manage who has access to what data, especially in environments
where data sensitivity and user roles vary significantly.

Questions:
USE the following schema and write the 5 rows of valid data for each table.
Emp(emp_id, fname,lname,job_title,salary, proj_id)
Project(proj_id, proj_name,proj_location)
Department(dept_num,dept_name,dept_location)
1. Create a dummy table and rename it
2. Update the employee first name whose name starts with letter ‘s’
Hint: to search any string value you may use like ‘%character’
Here the character represents any symbol/alphabet/digit and ‘%’ indicates any
number of characters in the search string of any length.
For ex1: to search an employee id whose name ends with letter ‘a’
Query: select emp_id from EMPLOYEE where name like ’%a’

For ex2: to search an employee id whose name ends with letter ‘a’
Query: select emp_id from EMPLOYEE where name like ’a%’

For ex3
: to search an employee id whose name contains the letter ‘a’
Query: select emp_id from EMPLOYEE where name like ’%a%’

For ex4: to search an employee id whose length of name is 3 characters.


We use undersore symbol for the length of any search string.
Query: select emp_id from EMPLOYEE where name like ’___’
In the above query 3 underscores and enclose in single quote.

For ex5: to search an employee id whose length of name is 5 characters and contains
letter ‘d’
Query: select emp_id from employee where name like ‘_%d%_’

3. Update the employee firstname whose name ends with letter ‘a’
4. Hike all employees salaries to 10000, who are drawing salary greater than 50000.
5. Delete the employee records whose salary value is null
6. Add a new column ‘favorite color’ to employee table.
7. Insert the data to newly added column
8. Retrieve all employees details who belong to department ‘IT’
9. Retrieve all employees id and names whose dept number is 100
10. Get all project details whose location is ‘mumbai’
11. Get all project names that belong to ‘cse’ dept
12. Get the dependent details when emp id is given
13. Update all project locations to Mumbai whose current location is Hyderabad.
14. Delete all rows of data of any table(you may create a dummy table), but not the
schema.

You might also like