DBMS2
DBMS2
Practical No: 2
Title: SQL Queries
Date of completion: 19th July 2024
Objectives: To develop Database programming skills.
Problem Statement:
a. Design and Develop SQL DDL statements which demonstrate the use of SQL objects such as Table,
View, Index, Sequence, Synonym, 7 different constraints etc.
b. Write at least 10 SQL queries on the suitable database application using SQL DML statements.
Software and Hardware requirements:
Theory:
➢ SQL:
SQL stands for Structured Query Language. SQL is a computer language used to interact with
relational database systems. SQL is a tool for organizing, managing, and retrieving archived
data from a computer database.
In common usage, SQL encompasses DDL and DML commands
for CREATE, UPDATE, MODIFY, or other operations on database structure.
• Tables
Tables are the fundamental building blocks of a database, consisting of rows (records) and
columns (attributes or fields). They ensure data integrity and consistency by defining the
structure and relationships of the stored information.
• Queries
Queries are SQL commands used to interact with databases. They enable users to retrieve,
update, insert, or delete data from tables, allowing for efficient data manipulation and retrieval.
• Constraints:
Constraints are rules applied to tables to maintain data integrity. They define conditions that
data must meet to be stored in the database, ensuring accuracy and consistency.
The following constraints are commonly used in SQL:
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.
• CREATE: It is used to create a table.
Syntax:
create table tablename(attribute1 datatype......attributen datatype);
• DROP: It is used to delete the table including all the attributes.
Syntax:
drop table tablename;
DBMS LAB 23010086
• ALTER: alter is a reserve word which modifies the structure of the table.
Syntax:
alter table tablename add(new column1 datatype......new columnx datatype);
• RENAME: A table name can be changed using the reserver ‘rename’
Syntax:
rename old table name to new table name;
➢ DML(Data Manipulation Language)
The SQL commands that deal with the manipulation of data present in the database belong to
DML or Data Manipulation Language and this includes most of the SQL statements.
It is the component of the SQL statement that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
The commands are:
• INSERT: This command is generally used after the create command to insert a set of
values into the table.
Syntax:
insert into tablename values(attribute1 datatype);
• DELETE: A command used to delete particular tuples or rows or cardinality from the
table.
Syntax:
delete from tablename where condition;
• UPDATE: It updates the tuples in a table.
Syntax:
update tablename set tuplename='attributename';
• LOCK: Table control concurrency
Syntax:
LOCK TABLE table_name IN lock_mode;
• CALL: Call a PL/SQL or JAVA subprogram
Syntax:
CALL procedure_name(arguments);
• EXPLAIN PLAN: Describe the access path to data
Syntax:
EXPLAIN PLAN FOR SELECT * FROM table_name;
DBMS LAB 23010086
➢ Transactions:
Transactions are groups of SQL statements that are executed as a single unit of work. They
ensure data consistency and integrity by allowing for the rollback of changes if any part of
the transaction fails.
Some other important components include:
• Views:
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
A view is created with the CREATE VIEW statement.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Index:
The CREATE INDEX statement is used to create indexes in tables.
Indexes are used to retrieve data from the database more quickly than otherwise. The users
cannot see the indexes, they are just used to speed up searches/queries.
Creates an index on a table. Duplicate values are allowed.
Syntax:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
• Sequences:
SQL sequences specifies the properties of a sequence object while creating it. An object
bound to a user-defined schema called a sequence produces a series of numerical values in
accordance with the specification used to create it. The series of numerical values can be
configured to restart (cycle) when it runs out and is generated in either ascending or
descending order at a predetermined interval.
Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
DBMS LAB 23010086
CYCLE|NOCYCLE ;
• Synonym:
A SYNONYM provides another name for database object, referred to as original object, that
may exist on a local or another server. A synonym belongs to schema, name of synonym should
be unique. A synonym cannot be original object for an additional synonym and synonym
cannot refer to user-defined function.
The query below results in an entry for each synonym in database. This query provides details
about synonym metadata such as the name of synonym and name of the base object.
select * from sys.synonyms ;
Syntax –
CREATE SYNONYM synonymname
FOR servername.databasename.schemaname.objectname;
GO
Conclusion:
In conclusion, the exercise of designing and developing SQL DDL statements showcased the practical
implementation of SQL objects like Tables, Views, Indexes, Sequences, Synonyms, and various
constraints. This foundational work sets the stage for efficient database management and data
integrity. Furthermore, writing at least 10 SQL DML queries on a suitable database application
demonstrated the ability to manipulate and retrieve data effectively, emphasizing the importance of
understanding both DDL and DML in building robust database applications. Together, these tasks
highlight the critical skills required for proficient database design and querying
Execution of queries:
1. CREATE:
DBMS LAB 23010086
3. INSERT
DBMS LAB 23010086
6. VIEW
7. INDEX
DBMS LAB 23010086
8. SEQUENCE
DBMS LAB 23010086
9. SYNONYM