Practical File DBMS Sagar
Practical File DBMS Sagar
Aim:- Write the queries for Data Definition Language (DDL) in RDBMS
DDL (Data Definition Language):
DDL or Data Definition Language actually consists of the SQL commands that can be used
to define the database schema. It simply deals with descriptions of the database schema and is
used to create and modify the structure of database objects in the database.
DDL is a set of SQL commands used to create, modify, and delete database structures but not
data. These commands are normally not used by a general user, who should be accessing the
database via an application.
List of DDL Commands:
1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
5. RENAME
1. CREATE:
The CREATE command is used to create a new database object such as a table, index, or
view. It defines the structure of the database elements.
Syntax:
……);
Example:
Output:
Description:
The employees table is created with four columns: id, name, position, and salary. The id is
defined as the PRIMARY KEY, ensuring that each employee has a unique identifier. The
VARCHAR datatype specifies a variable-length string, and DECIMAL(10, 2) defines a
number with two decimal places (for the salary).
2. ALTER
The ALTER command is used to modify the structure of an existing table, such as adding,
deleting, or modifying columns.
Syntax:
Drop a column:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
Output:
Output:
Drop a column:
Output:
The first example adds a department column to the employees table, while the second
example drops the position column from the same table. The ALTER command allows you to
change the table structure after it has been created.
3. DROP:
The DROP command is used to completely remove database objects like tables, indexes, or
databases. Once an object is dropped, all its data and structure are lost.
Syntax:
Drop a Table:
DROP TABLE table_name;
Example:
Output:-
Description:
The DROP command in the example deletes the employees table from the database. All data
contained in the table is lost, and the table structure is also removed. Use this command
carefully as the data cannot be recovered
4. TRUNCATE
The TRUNCATE command is used to remove all rows from a table quickly and efficiently. It
does not remove the table itself or its structure.
Syntax:
TABLE <table_name>;
Example:
Output:
5. RENAME
The RENAME command is used to rename database objects such as tables or columns.
Syntax may vary between SQL implementations.
Syntax:
RENAME TO <new_table_name>;
Example:
Output:
Practical-2
DML (Data Manipulation Language) is used to manage and manipulate data within database
tables. DML commands allow you to insert, update, delete, and retrieve data. Unlike DDL,
which deals with the structure of the database, DML works directly with the data stored
within that structure.
1. INSERT:
Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
Output:
Description:
The INSERT command inserts the values 1, 'Alice Johnson', 'Manager', and 75000 into the
employees table. A new row is added with the specified data for each column.
2. SELECT:
FROM table_name
WHERE condition;
Example:
Output:
Description:
This retrieves all the rows and columns from the employees table. The WHERE clause is
optional and can be used to filter results.
3.DELETE:
The DELETE command removes existing records from a table based on a specified
condition.
Syntax:
Example:
Output:
Description:
The DELETE command removes the row where the id is 1. After this operation, the table is
empty because Alice Johnson's record has been deleted. If no WHERE clause is specified, all
rows will be deleted from the table.
4.UPDATE:
The UPDATE command modifies existing records in a table, changing the values in one or
more columns.
Syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
Output:
Description:
The INSERT command inserts the values 1, 'Alice Johnson', 'Manager', and 75000 into the
employees table. A new row is added with the specified data for each column.
Practicle-3
Aim:- Write the queries for Data Control Language (DCL) in RDBMS.
DCL (Data Control Language) commands are a subset of SQL commands that deal with the
rights, permissions, and controls over database access. They are used to grant or revoke
access privileges to users, ensuring that only authorized users can perform certain operations
on the database. The two main DCL commands are:
GRANT
REVOKE
1. GRANT:
Used to provide specific privileges to users or roles, allowing them to perform particular
actions on database objects such as tables, views, or stored procedures.
Syntax:
Example:
Output:
Description:
This gives the user john_doe the power to run SELECT and INSERT commands on the
employees table. So, John can now view and add new rows to the table.
2.REVOKE:
Used to remove previously granted privileges, restricting users from performing specific
actions on database objects.
Syntax:
Example:
Output:
Description:
revokes only the INSERT privilege, leaving the SELECT privilege intact.
Practical-4
Aim: To perform various integrity constraint on relational database.
DBMS Integrity Constraints
Integrity constraints are the set of predefined rules that are used to maintain the quality of information.
Integrity constraints ensure that the data insertion, data updating, data deleting and other processes
have to be performed in such a way that the data integrity is not affected. They act as guidelines
ensuring that data in the database remain accurate and consistent. So, integrity constraints are used to
protect databases. The various types of integrity constraints are
Types of Integrity Constraints:
Domain Constraints
Entity integrity Constraints
Key Constraints
Referential integrity constraints
1. Domain constraints
Domain constraints can be defined as the definition of a valid set of values for an attribute.
The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain.
o
Example:
Output:
The entity integrity constraint states that primary key value can't be null.
This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
A table can contain a null value other than the primary key field.
Example:
Output:
Output:
If you uncomment the line that attempts to insert a record into the employee table with a dept_id
of 3, you will encounter a referential integrity violation since there is no corresponding dept_id in
the department table.
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A
primary key can contain a unique and null value in the relational table.
Example:
Output:
Practical-5 (a)
Aim: Create a database and perform the following operations:
Arithmetic and Relational operations .
A. ARIHMETIC OPERATORS:
(+) : Addition - Adds values on either side of the operator .
Relational operations:
relational operators like =, ≠, ≥, <, >, ≤.
Practical-5(b)
Aim: Group by & having clauses
The GROUP BY clause is often used with aggregate functions (MAX, SUM, AVG) to group
the results by one or more columns or In simple words we can say that The GROUP BY
clause is used in collaboration with the SELECT statement to arrange required data into
groups
Having Clause is basically like the aggregate function with the GROUP BY clause. The
HAVING clause is used instead of WHERE with aggregate functions.
Syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2, ...;
Example:
Output:
Practical-5(c)
Aim:- Like predicate pattern matching in Database
The LIKE predicate in SQL is used for pattern matching in strings. It allows you to search for
a specified pattern within a column's value. The LIKE operator is often used with the
SELECT statement to filter rows based on patterns.
FROM table_name
Example:
Output:
2. Match a String Ending with a Pattern:
Example:
Output:
To retrieve employees whose names contain the letter 'a' anywhere in their name:
Example:
Output:
Output:
Example:
Output:
6. Match Names Starting with 'A', Containing 'l', and Ending with 'e'
To retrieve employees whose names start with 'A', contain 'l', and end with 'e':
Example:
Output: