0% found this document useful (0 votes)
21 views20 pages

Practical File DBMS Sagar

Uploaded by

ps0987654321a

Copyright:

© All Rights Reserved

Available Formats

Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
21 views20 pages

Practical File DBMS Sagar

Uploaded by

ps0987654321a

Copyright:

© All Rights Reserved

Available Formats

Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 20

Practical-1

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:

CREATE TABLE table_name (

column1 datatype constraint,

column2 datatype constraint,

……);

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:

 Add a new column:


ALTER TABLE table_name ADD column_name datatype;

 Modify existing column:


ALTER TABLE table_name MODIFY column_name datatype;

 Drop a column:
ALTER TABLE table_name DROP COLUMN column_name;

Example:

 Add New column:

Output:

 Modify existing column:

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:

ALTER TABLE <old_table_name>

RENAME TO <new_table_name>;

ALTER TABLE <table_name>

RENAME COLUMN old_column_name TO new_column_name;

Example:

Output:
Practical-2

Aim:- Write the queries Data Manipulation Language(DML) in RDBMS

DML (Data Manipulation Language):

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.

List of DML Commands:

 INSERT: Adds new data to a table


 SELECT: Retrieves data from the database.
 UPDATE: Modifies existing data within a table
 DELETE: Removes data from a table.

1. INSERT:

The INSERT command is used to add new records to a table

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:

The SELECT command is used to retrieve data from a table.


Syntax:

SELECT column1, column2, ...

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:

DELETE FROM table_name WHERE condition;

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):

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:

GRANT privilege_name ON object TO user;

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:

REVOKE privilege_name ON object FROM user;

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:

2. Entity integrity constraints

 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:

3. Referential Integrity Constraints

 A referential integrity constraint is specified between two tables.


 In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table
2.
Example:
REFERENCE IMAGE:

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 .

(-):Subtraction - Subtracts right hand operand from left hand operand .

(*):Multiplication - Multiplies values on either side of the operator .


(/):Division - Divides left hand operand by right hand operand .

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.

Syntax of the LIKE Predicate:

SELECT column1, column2, ...

FROM table_name

WHERE column_name LIKE pattern;

Wildcards in the LIKE Predicate:

1. Percent Sign (%): Represents zero or more characters.


2. Underscore (_): Represents a single character.

Common Usage Patterns:

 %pattern%: Matches any string that contains pattern anywhere.


 pattern%: Matches any string that starts with pattern.
 %pattern: Matches any string that ends with pattern.
 _pattern_: Matches any string with exactly one character before and after pattern.

1. Match a String Starting with a Pattern:

To retrieve employees whose names start with the letter 'A':

Example:

Output:
2. Match a String Ending with a Pattern:

To retrieve employees whose names end with the letter 'e':

Example:

Output:

3. Match a String Containing a Pattern Anywhere:

To retrieve employees whose names contain the letter 'a' anywhere in their name:

Example:

Output:

4. Match a String with a Single Character:

To retrieve employees whose names have exactly 4 characters:


Example:

Output:

5. Match with Specific Characters in Certain Positions:

To retrieve employees whose second letter is 'l':

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:

You might also like