Taran Dbms

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Database Management System GU-2023-1436

Pratical No.1:-
Introduction to Database Management System(DBMS)and Structured Query
Language(SQL).
Solution-
What is a Database Management System.
A database management system (DBMS) is a software tool that enables users to manage a database
easily. It allows users to access and interact with the underlying data in the database. These actions can
range from simply querying data to defining database schemas that fundamentally affect the database
structure.

Furthermore, DBMS allow users to interact with a database securely and concurrently without interfering
with each user and while maintaining data integrity.

What does an DBMS do.

The DBMS manages the data; the database engine allows data to be accessed, locked and modified; and
the database schema defines the database's logical structure. These three foundational elements help
provide concurrency, security, data integrity and uniform data administration procedures. The DBMS
supports many typical database administration tasks, including change management, performance
monitoring and tuning, security, and backup and recovery. Most database management systems are also
responsible for automated rollbacks and restarts as well as logging and auditing of activity in databases
and the applications that access them.

The DBMS provides a centralized view of data that can be accessed by multiple users from multiple
locations in a controlled manner. A DBMS can limit what data end users see and how they view the data,
providing many views of a single database schema. End users and software programs are free from
having to understand where the data is physically located or on what type of storage medium it resides
because the DBMS handles all requests.

The DBMS can offer both logical and physical data independence to protect users and applications from
having to know where data is stored or from being concerned about changes to the physical structure of
data. So long as programs use the application programming interface (API) for the database that the
DBMS provides, developers won't
Database Management System GU-2023-1436

What are the components of a DBMS?

Solution-A DBMS is a sophisticated piece of system software consisting of multiple integrated


components that deliver a consistent, managed environment for creating, accessing and modifying data in
databases. These components include the following:

 Storage engine. This basic element of a DBMS is used to store data. The DBMS must interface with
a file system at the operating system (OS) level to store data. It can use additional components to
store data or interface with the actual data at the file system level.

 Metadata catalog. Sometimes called a system catalog or database dictionary, a metadata catalog
functions as a repository for all the database objects that have been created. When databases and
other objects are created, the DBMS automatically registers information about them in the metadata
catalog. The DBMS uses this catalog to verify user requests for data, and users can query the catalog
for information about the database structures that exist in the DBMS. The metadata catalog can
include information about database objects, schemas, programs, security, performance,
communication and other environmental details about the databases it manages.

 Database access language. The DBMS also must provide an API to access the data, typically in the
form of a database access language to access and modify data but may also be used to create database
objects and secure and authorize access to the data. SQL is an example of a database access language
and encompasses several sets of commands, including Data Control Language for authorizing data
access, Data Definition Language for defining database structures and Data Manipulation Language
for reading and modifying data.

 Optimization engine. A DBMS may also provide an optimization engine, which is used to parse
database access language requests and turn them into actionable commands for accessing and
modifying data.

 Query processor. After a query is optimized, the DBMS must provide a means for running the query
and returning the results.

 Lock manager. This crucial component of the DBMS manages concurrent access to the same data.
Locks are required to ensure multiple users aren't trying to modify the same data simultaneously.

 Log manager. The DBMS records all changes made to data managed by the DBMS. The record of
changes is known as the log, and the log manager component of the DBMS is used to ensure that log
Database Management System GU-2023-1436

records are made efficiently and accurately. The DBMS uses the log manager during shutdown and
startup to ensure data integrity, and it interfaces with database utilities to create backups and run
recoveries.

 Data utilities. A DBMS also provides a set of utilities for managing and controlling database
activities. Examples of database utilities include reorganization, runstats, backup and copy, recover,
integrity check, load data, unload data and repair database.

What is Structured Query Language(SQL)

What is SQL
Database Management System GU-2023-1436

Structured query language (SQL) is a programming language for storing and processing information in a
relational database. A relational database stores information in tabular form, with rows and columns
representing different data attributes and the various relationships between the data values. You can use
SQL statements to store, update, remove, search, and retrieve information from the database. You can
also use SQL to maintain and optimize database performance.

Why is SQL important


Structured query language (SQL) is a popular query language that is frequently used in all types of
applications. Data analysts and developers learn and use SQL because it integrates well with different
programming languages. For example, they can embed SQL queries with the Java programming
language to build high-performing data processing applications with major SQL database systems such
as Oracle or MS SQL Server. SQL is also fairly easy to learn as it uses common English keywords in its
statements

History of SQL
SQL was invented in the 1970s based on the relational data model. It was initially known as the
structured English query language (SEQUEL). The term was later shortened to SQL. Oracle, formerly
known as Relational Software, became the first vendor to offer a commercial SQL relational database
management system.

These SQL commands are mainly categorized into five categories:

1. DDL – Data Definition Language

2. DQL – Data Query Language

3. DML – Data Manipulation Language


Database Management System GU-2023-1436

4. DCL – Data Control Language

5. TCL – Transaction Control Language


Database Management System GU-2023-1436

Pratical No 2.
To write a query with a function of DDL commands such as create,alter,drop
DDL is used as an abbreviation for Data Definition Language. DDL refers to a computer
language that is primarily used for creating as well as modifying the structure of the database
objects present in a database. Such database objects include indexes, tables, schemas, views,
and many more.

DDL is also referred to as a data description language in certain contexts since it describes the
records and fields in the DB (database) tables.

DDL commands are:

 Create
 Alter
 Drop

Create
The CREATE command builds a new table. It has a predefined syntax, and the CREATE statement
syntax goes like this:

CREATE TABLE [name_of_table] ([definitions_of_column]) [parameters_of_table];

Example

CREATE TABLE TARANVEER

id int,

lname char(20));

Output
Database Management System GU-2023-1436

Alter
The alter command modifies any existing table in a database. The Alter command can add up an
additional column, drop the existing columns and even change the data types of various columns
involved in a DB table. Read more on the ALTER command here.

Syntax

ALTER type_of_object name_of_object parameters;

Example

ALTER TABLE TARANVEER ADD(ADDRESS varchar(67));

Output

Drop
The DROP command is used in order to delete objects, like a table, view or index. We cannot rollback
the DROP statement. Thus, once a certain object is destroyed, there would be no way at all to recover it.

The syntax of the DROP statement is:

DROP type_of_object name_of_object;

Example
DROP TABLE TARANVEER

Output
Database Management System GU-2023-1436
Database Management System GU-2023-1436

Practical No.3
Create a table studmarks with following attributes name data type:

Name DataType
Rollno number (6)
Regnumber number (14)
Semester number (1)
CGPA number (2,4)

Alter the Table:

(a) Add the constraints UNIQUE for regnumber attribute from studmarks table.
(b) Remove the constraints for the regnumber attribute.
(c) Modify the data type of regnumber to varchar (16).

SOLUTION:

Creating Stud_marks table :

Query:
create table stud_marks(rollno numeric(6),regnumber numeric(14),semester numeric(1),cgpa
numeric(2,4));

Output-
Database Management System GU-2023-1436

Describe stud_marks;
Output:

a) Add the constraints UNIQUE for regnumber attribute from stud_marks table

Unique Constraint: SQL Constraints Unique constraints in SQL is used to check whether the sub-
query has duplicate tuples in its result. It returns a boolean value indicating the presence/absence
of duplicate tuples. Unique constraint returns true only if the subquery has no duplicate tuples, else
it returns false.
Query-
alter table stud_marks ADD UNIQUE (regnumber);

Output-

b)alter table stud_marks ADD UNIQUE (regnumber); Remove the constraints for the regnumber
attribute.

Syntax:

Alter table table_name drop unique (Column_name);

Query:

alter table stud_marks DROP UNIQUE (regnumber);


Database Management System GU-2023-1436

Output-

a) Modify the data type of regnumber to varchar (16).


Syntax:

Alter table table_name modify Column_name datatype(datasize);


Query:

alter table stud_marks MODIFY regnumber varchar(16);

Output-
Database Management System GU-2023-1426

PRACTICAL No.4
Create a student table and describe the Schema of the student Table:

Name DataType
ROLLNO Number (6)
NAME VARCHAR (15)
DEPT VARCHAR (10)
CITY VARCHAR (1`5)
DOB DATE
GENDER CHAR(1)

a) Add foreign key constraint for the column rollno from studmarks that refers rollno
from student table.
b) Add one more column age in student table with NOT NULL constraint in the
student table.
c) Remove the column city from the student table.
d) RENAME THE TABLE:
i. Change the name of the table student to stud.
ii. Change the name of the attribute dob to dateofbirth.
e) DROP THE TABLE:
i. Drop the table stud.

SOLUTION:

Creating table student:

• Query:
Create table student (rollnonumber(6)primary key,name varchar2(15),dept varchar2(10),city
varchar2(15),dob date, gender char(1),foreign key(rollno) references stud_marks(rollno));
• Output:

12
Database Management System GU-2023-1426

a) Add foreign key constraint for the column rollno from studmarks that refers rollno
from student table.

Foreign key Constraint: A foreign key is a key used to link two tables together and
sometimesit is called as referencing key. The relationship between two tables matches
the primary key inone table with foreign key in second table. This constraint uniquely
identifies a row or recordsin another table.
Syntax:

Alter table table_name add foreign key (Column_name) references table_name1


(column_name);
Query:

Alter table Student1 add foreign key (Rollno) references Stud_mark (Rollno);

• Output:

b) Add one more column age in student table with NOT NULL constraint in the student
table.

Not Null Constraint: This ensures that a cannot have null value.

• Syntax:
13
Database Management System GU-2023-1426

Alter table table_name add Column_name datatype (data size) Not Null;

• Query:
alter table student add age int not null;

• Output:

c) Remove the column city from the student table.\

Syntax:

Alter table table_name drop Column Column_name;

• Query:
alter table student drop column city;

• Output:

14
Database Management System GU-2023-1426

Describe student;

d) RENAME THE TABLE:


i. Change the name of the table student to stud.
• Syntax:
Rename table_name to new_table_name;

• Query:
rename student to stud;

• Output:

15
Database Management System GU-2023-1426

Describe stud;

ii. Change the name of the attribute dob to dateofbirth.


• Syntax:
Alter table table_name rename column Column_name to new_columnc_name;
• Query:

alter table student rename column dob to dateofbirth;


• Output:

16
Database Management System GU-2023-1426
Describe student;

e) DROP THE TABLE:


Drop the table stud.

• Syntax:

Drop table table_name;


• Query:
drop table stud;
• Output:

17

You might also like