0% found this document useful (0 votes)
12 views

Lab Manual (DDS)

Uploaded by

danishmobile060
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab Manual (DDS)

Uploaded by

danishmobile060
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

LAB MANUAL

Submitted By:

Muhammad Waleed Sabir

Roll No:
586403

Class:
BS (Computer Science)

Semester:
6th

Session:
2020 – 2024

Course Title:

Distributed Database System

Punjab Colleges Jaranwala


(Affiliated with)

Government College University Faisalabad


Contents
Introduction to SQL: -..................................................................................................................................2
SQL Statements:..........................................................................................................................................2
Some of The Most Important SQL Commands:.......................................................................................2
1.SQL SELECT Statement:.........................................................................................................................3
2.SQL UPDATE Statement:.......................................................................................................................3
3.SQL DELETE Statement:........................................................................................................................3
4.SQL INSERT INTO Statement:................................................................................................................4
SQL Operators: -..........................................................................................................................................4
SQL Logical Operators:.............................................................................................................................4
1.SQL AND Operator:...............................................................................................................................5
2.SQL OR Operator:.................................................................................................................................6
3.SQL NOT Operator:...............................................................................................................................6
SQL Joins: -...................................................................................................................................................7
Different Types of SQL JOINs: -................................................................................................................7
1.SQL INNER JOIN:...............................................................................................................................7
2.SQL LEFT JOIN Keyword:...................................................................................................................8
3.SQL RIGHT JOIN Keyword:................................................................................................................9
3.SQL FULL OUTER JOIN Keyword:.....................................................................................................10
4.SQL UNION Operator:.........................................................................................................................11
SQL HAVING Clause...............................................................................................................................11
SQL ANY and ALL Operators:.................................................................................................................12
SQL ANY Example:.................................................................................................................................12
SQL ALL Example:..................................................................................................................................13
SQL Database: -.........................................................................................................................................14
1.The SQL CREATE DATABASE Statement:.............................................................................................14
2.SQL DROP DATABASE Statement:.......................................................................................................14
SQL CREATE TABLE Statement:..................................................................................................................15
1.DROP TABLE Statement:.....................................................................................................................16
2.SQL ALTER TABLE Statement:.............................................................................................................16
3.SQL Create Constraints:......................................................................................................................16
SQL References: -.......................................................................................................................................17
Introduction to SQL: -
SQL is a standard language for accessing and manipulating databases.

SQL:

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

SQL Work:

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

SQL Statements:
Most of the actions you need to perform on a database are done with SQL statements. SQL
statements consist of keywords that are easy to understand.

Some of The Most Important SQL Commands:

 SELECT - extracts data from a database


 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

1.SQL SELECT Statement:


The SELECT statement is used to select data from a database.

Syntax:
SELECT colum1, colum2, ………..

FROM table_name;

Example:
Return all columns from Customer table

SELECT * FROM Customer;

2.SQL UPDATE Statement:


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

Syntax:
UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

Example:
UPDATE Customers

SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'

WHERE CustomerID = 1;

3.SQL DELETE Statement:


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

Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

4.SQL INSERT INTO Statement:

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

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

Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

SQL Operators: -

SQL Logical Operators:

Operator Description

ALL TRUE if all of the subquery values meet the condition

AND TRUE if all the conditions separated by AND is TRUE

ANY TRUE if any of the subquery values meet the condition


BETWEEN TRUE if the operand is within the range of comparisons

EXISTS TRUE if the subquery returns one or more records

IN TRUE if the operand is equal to one of a list of expressions

LIKE TRUE if the operand matches a pattern

NOT Displays a record if the condition(s) is NOT TRUE

1.SQL AND Operator:


The WHERE clause can contain one or many AND operators.

The AND operator is used to filter records based on more than one condition, like if you want to
return all customers from Spain that starts with the letter 'G':

Example:

Select all customers from Spain that starts with the letter 'G':

SELECT *FROM Customers

WHERE Country = 'Spain' AND CustomerName LIKE 'G%';

Syntax:

SELECT column1, column2, ... FROM table_name

WHERE condition1 AND condition2 AND condition3 ...;


2.SQL OR Operator:
The WHERE clause can contain one or more OR operators.

The OR operator is used to filter records based on more than one condition, like if you want to
return all customers from Germany but also those from Spain:

Example:

Select all customers from Germany or Spain:

SELECT * FROM Customers

WHERE Country = 'Germany' OR Country = 'Spain';

Syntax:

SELECT column1, column2, ... FROM table_name

WHERE condition1 OR condition2 OR condition3 ...;

3.SQL NOT Operator:


The NOT operator is used in combination with other operators to give the opposite result, also
called the negative result.

In the select statement below we want to return all customers that are NOT from Spain:

Example:

Select only the customers that are NOT from Spain:

SELECT * FROM Customers

WHERE NOT Country = 'Spain';

Syntax:
SELECT column1, column2, ... FROM table_name

WHERE NOT condition;


SQL Joins: -
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

Different Types of SQL JOINs: -


Here are the different types of the JOINs in SQL:

 (INNER) JOIN
 LEFT (OUTER) JOIN
 RIGHT (OUTER) JOIN
 FULL (OUTER) JOIN

1.SQL INNER JOIN:

The INNER JOIN keyword selects records that have matching values in both tables.

Example:
Join Products and Categories with the INNER JOIN keyword:
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;

Syntax:
SELECT column_name(s)

FROM table1

INNER JOIN table2

ON table1.column_name = table2.column_name;
2.SQL LEFT JOIN Keyword:

The LEFT JOIN keyword returns all records from the left table (table1), and the matching
records from the right table (table2). The result is 0 records from the right side, if there is no
match.

Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SQL LEFT JOIN Example:


SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
3.SQL RIGHT JOIN Keyword:
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no
match.

Syntax:

SELECT column_name(s)

FROM table1

RIGHT JOIN table2

ON table1.column_name = table2.column_name;

Example:

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName

FROM Orders

RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID

ORDER BY Orders.OrderID;
3.SQL FULL OUTER JOIN Keyword:
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.

Syntax:

SELECT column_name(s)

FROM table1

FULL OUTER JOIN table2

ON table1.column_name = table2.column_name

WHERE condition;

Example:

SELECT Customers.CustomerName, Orders.OrderID

FROM Customers

FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID

ORDER BY Customers.CustomerName;
4.SQL UNION Operator:
The UNION operator is used to combine the result-set of two or more SELECT statements.

UNION Syntax:

SELECT column_name(s) FROM table1

UNION

SELECT column_name(s) FROM table2;

Example:

SELECT City FROM Customers

UNION

SELECT City FROM Suppliers

ORDER BY City;

SQL HAVING Clause


The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.

HAVING Syntax:

SELECT column_name(s)

FROM table_name

WHERE condition

GROUP BY column_name(s)HAVING conditionORDER BY column_name(s);


Example:

SELECT COUNT(CustomerID), Country

FROM Customers

GROUP BY Country

HAVING COUNT(CustomerID) > 5;

SQL ANY and ALL Operators:


The ANY and ALL operators allow you to perform a comparison between a single column value
and a range of other values.

ANY Syntax:

SELECT column_name(s)

FROM table_name

WHERE column_name operator ANY

(SELECT column_name

FROM table_name

WHERE condition);

ALL Syntax:

SELECT ALL column_name(s)

FROM table_name

WHERE condition;

SQL ANY Example:


SELECT ProductName

FROM Products

WHERE ProductID = ANY


(SELECT ProductID

FROM OrderDetails

WHERE Quantity = 10);

SQL ALL Example:


SELECT ProductName

FROM Products

WHERE ProductID = ALL

(SELECT ProductID

FROM OrderDetails

WHERE Quantity = 10);


SQL Database: -

1.The SQL CREATE DATABASE Statement:


The CREATE DATABASE statement is used to create a new SQL database.

Syntax:

CREATE DATABASE databasename;

Example:

CREATE DATABASE testDB;

2.SQL DROP DATABASE Statement:


The DROP DATABASE statement is used to drop an existing SQL database.

Syntax:
DROP DATABASE databasename;

Example:

DROP DATABASE testDB;

Example:

BACKUP DATABASE testDB

TO DISK = 'D:\backups\testDB.bak';
SQL CREATE TABLE Statement:
The CREATE TABLE statement is used to create a new table in a database.

Syntax:

CREATE TABLE table_name (

column1 datatype,

column2 datatype,

column3 datatype,

....

);

Example

CREATE TABLE Persons (

PersonID int,

LastName varchar(255),

FirstName varchar(255),

Address varchar(255),

City varchar(255)

);
1.DROP TABLE Statement:
The DROP TABLE statement is used to drop an existing table in a database.

Syntax:

DROP TABLE table_name;

Example:

DROP TABLE Shippers;

2.SQL ALTER TABLE Statement:


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

3.SQL Create Constraints:


Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.

Syntax:

CREATE TABLE table_name (

column1 datatype constraint,

column2 datatype constraint,

column3 datatype constraint,

....

);
SQL References: -

MS Access String Functions:

Function Description

Asc Returns the ASCII value for the specific character

Chr Returns the character for the specified ASCII number code

Concat Adds two or more strings together


with &

CurDir Returns the full path for a specified drive

Format Formats a value with the specified format

InStr Gets the position of the first occurrence of a string in another

InstrRev Gets the position of the first occurrence of a string in


another, from the end of string

MS Access Date Functions:

Function Description

Date Returns the current system date

DateAdd Adds a time/date interval to a date and then returns


the date

DateDiff Returns the difference between two dates

DatePart Returns a specified part of a date (as an integer)

DateSerial Returns a date from the specified parts (year, month,


and day values)

DateValue Returns a date based on a string

Day Returns the day of the month for a given date


Format Formats a date value with the specified format

MS Access Numeric Functions:

Function Description

Abs Returns the absolute value of a number

Atn Returns the arc tangent of a number

Avg Returns the average value of an expression

Cos Returns the cosine of an angle

Count Returns the number of records returned by a select query

Exp Returns e raised to the power of a specified number

Fix Returns the integer part of a number

Format Formats a numeric value with the specified format


Int Returns the integer part of a number

You might also like