You are on page 1of 25

PUSSGRC, Hoshiarpur

DATABASE MANAGEMENT SYSTEM(PCIT 353)

Submitted to:
Ms. Ritika Arora

Submitted by:
Name : Amit Kumar
Roll NO. : SG21805
IT Branch(3rd semester)

Teacher’s Signature …………………….


Date: ………………………………………………
CONTENT

Sno. Topic Page No Date Remarks


1. Introduction to SQL
a). Creating the table

b). Inserting values into the table

c). Fetching entries from the table

d). Fetching specific entries from


the table

e). Deleting the table

2. Editing, sorting and


Range operations in
MySQL
a). Adding primary key to the table

b). Updating data in the table

c). Deleting data from the table

d). Sorting results using Order by

e). Getting range of results using


Min-Max

3. Arithmetic and Relational


operations in MySQL
a). Using LIKE to modify
conditional statements

b). Using Wildcard characters to


modify the conditional
statement

c). Specifying multiple values in


the conditional statement

d). Getting values of records within


a set range

Topic
Sno. Page No Date Remarks

4. Joining Table in MySQL


INTRODUCTION

DATABASE MANAGEMENT SYSTEM


A database management system (or DBMS) is essentially nothing more than a computerized data-keeping system.
Users of the system are given facilities to perform several kinds of operations on such a system for either
manipulation of the data in the database or the management of the database structure itself.
he 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 have to modify programs just because changes have been made to the database.

SQL
 SQL is a standard language for accessing and manipulating databases.
 SQL stands for Structured Query Language
 SQL lets you access and manipulate databases
 SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987.

Here are some key points which can be done using SQL:

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
Practical-1(a)
CREATING A TABLE

The CREATE TABLE statement is used to create a new table in a database. The column parameters
specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).

SYNTAX:

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

The following example creates a table called "Students" that contains five columns: StudentID,
LastName, FirstName, Address, and City:

CREATE TABLE Students


(
StudentID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Output:
Practical-1(b)
INSERTING INFO TO TABLE

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the column names
in the SQL query. However, make sure the order of the values is in the same order as the columns in the
table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

The following SQL statement inserts a new record in the "Students" table:

INSERT INTO Students ( StudentID,FirstName, LastName, Address, City)


VALUES ('01','VARUN','VERMA','VPO jhanjeri, SAS nagar','Hoshiarpur');

Output:
Practical-1(c)
SELECT STATEMENT

The SELECT statement is used to select data from a database. The data returned is stored in a result
table, called the result-set.

Syntax:

SELECT column1, column2, ...


FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;


Practical-1(d)
SELECT SPECIFIC COLUMN

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.

Syntax:

SELECT DISTINCT column1, column2, ...


FROM table_name;

The following SQL statement selects all (including the duplicates) values from the "FirstName" column
in the "Students" table:
Practical-1(e)
DROP TABLE

The DROP TABLE statement is used to drop an existing table in a database.

Syntax:
DROP TABLE table_name;

The following SQL statement drops the existing table "Students":


Practical-2(a)
PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE
values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple
columns (fields).

Syntax:

CREATE TABLE Persons (


PersonID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
PRIMARY KEY (ID)
);
Practical-2(b)
UPDATE

The UPDATE statement is used to modify the existing records in a table.


The following SQL statement updates the first Person (PersonID = 3) with a new First Name .

Syntax:
UPDATE Persons
SET FirstName = 'NURAV'
WHERE PersonID = 3;

Output:
Practical-2(c)
DELETE

The DELETE statement is used to delete existing records in a table.

Syntax:
DELETE FROM table_name WHERE condition;
The following SQL statement deletes the PERSON “NURAV” from the "Persons" table:
INITIALLY:

AFTER:
Practical-2(d)
ORDER BY

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending
order, use the DESC keyword.

Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

The following SQL statement selects all customers from the "Persons" table, sorted by the
"FirstName" column:
INITIALLY:

AFTER:
Practical-2(e)
MIN-MAX

The MIN() function returns the smallest value of the selected column. The MAX() function
returns the largest value of the selected column. Both the functions are very useful to help
determine the range of the data.
SYNTAX:
SELECT MAX(PersonID) AS MaximumPersonID
FROM Persons;
SELECT MIN(PersonID) AS MaximumPersonID
FROM Persons;
Practical-3(a)
Calculating Sum, Average and number of specific entities.

The SUM() function returns the total sum of a numeric column. The function only works on the
numeric columns only. The AVG() function returns the average value of a numeric column and
just like the SUM() function, it only works on the numeric columns only. The COUNT()
function returns the number of rows that matches a specified criterion. This will return the
number of entries in the table which satisfy a given condition.

SYNTAX:

Select Sum (roll_no) from student;

OUTPUT:

SYNTAX:

Select Avg (roll_no) from student;


OUTPUT:

SYNTAX:
Select Count (name) from student;

OUTPUT :
Practical-3(b)
Using LIKE to modify conditional statements

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
LIKE statements make use of wildcards to modify the conditional statements. There are two
wildcards often used in conjunction with the LIKE operator:
● The percent sign (%) represents zero, one, or multiple characters
● The underscore sign (_) represents one, single character

SYNTAX :

Select * from student where Name like '%og%';

OUTPUT
Practical-3(c)
Using Wildcard characters to modify the conditional statement

A wildcard character is used to substitute one or more characters in a string. Wildcard


characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to
search for a specified pattern in a column. Some of the most commonly used wildcards are :

SYNTAX:
Select * from student where Name like '%a_';

OUTPUT:
Practical-3(d)
Specifying multiple values in the conditional statement

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a
shorthand for multiple OR conditions, which means rather than using multiple OR conditions,
we can use IN operator.

SYNTAX :
Select * from student where Stream in('CSE', 'IT');

OUTPUT:
Practical-3(e)
Getting values of records within a set range

The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates. The BETWEEN operator is inclusive which means that beginning and
ending values are included. It also sorts the values in ascending order by default.

SYNTAX :
Select * from student where Roll_no between 21124 and 21854;

Output :
Practical 4
Joining tables in MySQL

A JOIN clause is used to combine rows from two or more tables, based on a related column
between them. There are 4 ways to join the tables in MySQl and we will talk about them briefly
here. To understand the concept of joining, let's consider these two tables :

CREATE TABLE Student (


id int PRIMARY KEY IDENTITY,
admission_no varchar(45) NOT NULL,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
age int,
city varchar(25) NOT NULL
);

CREATE TABLE Fee (


admission_no varchar(45) NOT NULL,
course varchar(45) NOT NULL,
amount_paid int,
);
INSERT INTO Student (admission_no, first_name, last_name, age, city)
VALUES (3354,'Luisa', 'Evans', 13, 'Texas'),
(2135, 'Paul', 'Ward', 15, 'Alaska'),
(4321, 'Peter', 'Bennett', 14, 'California'),
(4213,'Carlos', 'Patterson', 17, 'New York'),
(5112, 'Rose', 'Huges', 16, 'Florida'),
(6113, 'Marielia', 'Simmons', 15, 'Arizona'),
(7555,'Antonio', 'Butler', 14, 'New York'),
(8345, 'Diego', 'Cox', 13, 'California');
INSERT INTO Fee (admission_no, course, amount_paid)
VALUES (3354,'Java', 20000),
(7555, 'Android', 22000),
(4321, 'Python', 18000),
(8345,'SQL', 15000),
(5112, 'Machine Learning', 30000);

Table: Student

Table: Fee

INNER JOIN:
The INNER JOIN keyword selects records that have matching values in both tables. It takes the
values that are in the intersection of both tables and display them as per the user instructions.

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.amount_paid


FROM Student
INNER JOIN Fee
ON Student.admission_no = Fee.admission_no;
LEFT JOIN:

The LEFT JOIN allows you to query data from two or more tables. Similar to the INNER JOIN
clause, the LEFT JOIN is an optional clause of the SELECT statement, which appears
immediately after the FROM clause.

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.amount_paid


FROM Student
LEFT OUTER JOIN Fee
ON Student.admission_no = Fee.admission_no;

RIGHT JOIN:
The RIGHT JOIN keyword returns all records from the right table (books), and the matching
records (if any) from the left table (student). MySQL RIGHT JOIN is similar to LEFT JOIN,
except that the treatment of the joined tables is reversed.

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.amount_paid


FROM Student
RIGHT OUTER JOIN Fee
ON Student.admission_no = Fee.admission_no;
CROSS JOIN:
The CROSS JOIN keyword returns all records from both tables (student and books). The result
set will include all rows from both tables, where each row is the combination of the row in the
first table with the row in the second table. In general, if each table has N and M rows
respectively, the result set will have N*M rows.

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.course, Fee.amount_paid


FROM Student
CROSS JOIN Fee
WHERE Student.admission_no = Fee.admission_no;

You might also like