DBMS Lab File - 4thsem
DBMS Lab File - 4thsem
DBMS : DBMS or Database Management System is a software application used to access, create, and
manage databases by modifying them.
Functions of DBMS :
Types of DBMS :
Installing MySQL on Windows: Your downloaded MySQL is neatly packaged with an installer.
Download the installer package, unzip it anywhere and run setup.exe. By default, this process will
install everything under C:\mysql.
Step-3 : if you already have created data bases then use them else create new database.
Creation of Database :
CREATE TABLE table_name ( column1 datatype , column2 datatype , column3 datatype, ....);
Creating table college :
DESC Command : desc or describe command shows the structure of table which include name of
the column, data-type of column and the nullability which means, that column can contain null values
or not.
syntax :
DDL (Data Definition language) - DDL is used when we need to create database and tables,
alter them , drop them and rename the table .
TRUNCATE student;
DML (Data Manipulation Language) - DML language used to manipulate the database like
inserting data, updating table, retrieving record from a table .
DCL (Data Control Language) : Grant privilege to a user using the GRANT
statement.revoke the privilege using the REVOKE statement.
START TRANSACTION;
INSERT INTO student (name, lastname) VALUES ('Dia', 'Shivi');
COMMIT;
SAVEPOINT: Set a point in transaction to rollback later
ROLLBACK: Restores since last commit
START TRANSACTION;
INSERT INTO student (name, lastname) VALUES ('Dia', 'Shivi');
ROLLBACK;
Example :
Output :
Example :
Output :
UPDATION Query :
Select*from college;
Output :
Example :
UPDATE college SET NAME = 'Atulya', DEPT = 'ECE' WHERE ROLL_NO = 105;
Select*from college;
Output :
DELETION Query :
Select*from college;
Output :
Select*from college;
Output :
DATABASE CONSTRAINTS
Primary Key
Foreign Key
CHECK Constraint
UNIQUE Constraint
NOT NULL Constraint
PRIMARY KEY : A primary key is a column or a group of columns used to identify a row
uniquely in a table. A primary key constraint is the combination of a not-null
constraint and a UNIQUE constraint.
syntax
: CREATE TABLE TABLE ( column_1 data_type PRIMARY KEY, column_
2 data_type, …);
Example :
In case you want to specify the name of the primary key constraint, you
use CONSTRAINT clause as follows:
syntax
: CONSTRAINT constraint_name PRIMARY KEY(column_1, column_2,..
.);
Define primary key when changing the existing table structure
FOREIGN KEY : Also called referential integrity . This constraint identifies any column
referencing the PRIMARY KEY in another table. For a column to be defined as a Foreign
Key, it should be a defined as a Primary Key in the table which it is referring. One or more
columns can be defined as Foreign key.
CHECK Constraint : This constraint defines a business rule on a column. All the rows must
satisfy this rule. The constraint can be applied for a single column or a group of columns.
UNIQUE Constraint : This constraint ensures that a column or a group of columns in each
row have a distinct value. A column(s) can have a null value but the values cannot be
duplicated.
NOT NULL Constraint : This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not allowed.
syntax : [CONSTRAINT constraint name] NOT NULL
Example :
syntax :
ALTER TABLE table_name
ALTER COLUMN column_name_1 SET NOT NULL,
ALTER COLUMN column_name_2 SET NOT NULL;
Example :
Que.5. Apply the constrains like Primary key, Foreign key, NOT NULL to
the tables.
Constraints : SQL constraints are used to specify rules for data in a table.These are used to
limit the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the database.Constraints could be either on a column level or a table level. The
column level constraints are applied only to one column, whereas the table level constraints
are applied to the whole table.Constraints can be divided into the following two types :
Following are the most used constraints that can be applied to a table.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
NOT NULL : NOT NULL constraint restricts a column from having a NULL value.
Once NOT NULL constraint is applied to a column, you cannot pass a null value to that
column. It enforces a column to contain a proper value.
UNIQUE : UNIQUE constraint ensures that a field or column will only have unique values.
A UNIQUE constraint field will not have duplicate data. This constraint can be applied at
column level or table level.
FOREIGN KEY : FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint
is also used to restrict actions that would destroy links between tables. Foreign Key is a field
in a table which uniquely identifies each row of a another table
5.Prevents the addition of a record to a table that contains a foreign key unless
there is a primary key in the linked table
Que.7. Write query for implementing the following functions: MAX( ),
MIN( ), AVG( ) and COUNT( ).
Aggregate Functions: Aggregate functions perform a calculation on a set of rows and
return a single row. You can use aggregate functions as expressions only in the following
clauses i.e., SELECT clause, HAVING clause. There are five aggregate functions:
AVG(): The AVG() function allows you to calculate the average value of a numeric column.
syntax : AVG(column)
Output :
AVERAGE
SAL
22000.0000
COUNT(): The COUNT function returns the total number of values in the specified field.
syntax : COUNT(expression)
Output :
count(*)
6
MAX() : It returns the maximum value from the specified table field.
syntax : MAX(expression)
Output :
MAX(Salary)
30000
MIN(): The MIN function returns the minimum value in the specified table field.
syntax : MIN(expression)
Output :
MIN(Salary)
12000
SUM(): SUM function which returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result returned.
syntax : SUM(expression)
Output :
Total Salary
132000
These functions are called aggregate functions because they operate on the aggregate of tuples.
The result of an aggregate function is a single value.
Que.8. Perform the queries for triggers.
TRIGGERS
TRIGGERS : A SQL trigger is a database object just like a stored procedure, or we can say
it is a special kind of stored procedure which fires when an event occurs in a database. We
can execute a SQL query that will "do something" in a database when an event is fired.
Types of Triggers
1. DDL Trigger
2. DML Trigger
TRIGGERS PROCEDURES
RETURNS trigger AS
SQL CREATE TRIGGER statement :
ON table_name
Example :
);
Output :
id first_name last_name
1 john doe
2 lily blush
UPDATE QUERY :
UPDATE employees
WHERE ID = 2;
Output :
id first_name last_name
1 john doe
2 lily brown
Advantages of Triggers :
SQL JOINS
JOINS : join is used to combine columns from one (self-join) or more tables based on the
values of the common columns between the tables. A JOIN is a means for combining fields
from two tables by using values common to each. There are four types of joins :-
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
sample tables :Suppose we have two tables called basket a and basket b that stores fruits:
CREATE TABLE basket_a (id INT PRIMARY KEY, fruit VARCHAR (100) NOT NUL);
CREATE TABLE basket_b (id INT PRIMARY KEY,fruit VARCHAR (100) NOT NUL);
INNER JOIN : This type of join returns those records which have matching values in both
tables.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a INNER
JOIN basket_b b ON a.fruit = b.fruit;
Output :
1 apple 2 apple
2 orange 1 orange
LEFT JOIN : returns all rows from the left table, even if there are no matches in the right
table.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a LEFT JOIN
basket_b b ON a.fruit = b.fruit;
Output :
1 apple 2 apple
2 orange 1 orange
RIGHT JOIN : returns all rows from the right table, even if there are no matches in the left
table.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a RIGHT
JOIN basket_b b ON a.fruit = b.fruit;
Output :
2 orange 1 orange
1 apple 2 apple
FULL JOIN: returns rows when there is a match in one of the tables.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a FULL
JOIN basket_b b ON a.fruit = b.fruit WHERE a.id IS NULL OR b.id IS NULL;
Output :
CREATE VIEW stu_View AS SELECT NAME, ADDRESS FROM college WHERE S_ID
< 5;
Output :
name address
ani goa
nia chennai
kia delhi
ishi bhutan
to see result :
Output :
ani goa 99
nia chennai 98
kia delhi 89
ishi bhutan 70
DELETING VIEWS:
UPDATING VIEWS :
Insertion :
syntax :
INSERT INTO view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);
Que.11. Write the query for creating the users and their roles.
asterisks in this command refer to the database and table that they can access .Once you have
finalized the permissions that you want to set up for your new users, always be sure to reload
all the privileges.Your changes will now be in effect.
To provide a specific user with a permission, you can use this framework:
Just as you can delete databases with DROP, you can use DROP to delete a
user altogether:
mysql>quit