FDM Manual
FDM Manual
Laboratory Manual
for
Fundamentals of Database
Table of Contents
Session 1: introduction to database and Generally laboratory instruction .................................... 1
Installing SQL Server software ............................................................................................................. 2
opening and connecting Microsoft SQL server .................................................................................. 7
Session 2 Data Definition language ......................................................................................................... 7
CREATING, MODIFYING AND Dropping DATABASES ............................................................... 8
LAB EXPERIMENTS 1 ....................................................................................................................... 11
Session 3 DATA DEFINITION, CONSTRAINTS, AND SCHEMA CHANGES.............................. 11
CREATING, MODIFYING AND DELETING TABLES .............................................................. 12
LAB EXPERIMENTS 2 ....................................................................................................................... 14
Session 3 DATA MANIPULATION LANGUAGE (DML) .................................................................. 15
INSERT UPDATE DELETE TABLES .............................................................................................. 15
Session 4 DATA RETRIEVAL LANGUAGE (DRL) .............................................................................. 17
Retrieve data from one or more tables. .............................................................................................. 17
Implementation of different types of operators in SQL. .................................................................. 18
Session 5 join index and view ................................................................................................................. 20
Joins ........................................................................................................................................................ 20
INDEXING ............................................................................................................................................. 21
VIEW ...................................................................................................................................................... 21
The content is organized into lessons corresponding to the lecture content outlined on the
curriculum. And the code in this manual is written (edited), debug and Execute using Microsoft
SQL server.
Database: a collection of Logical related data.it is a collection of table
Database Management Systems (DBMS): a computerized system that system enables user to
create and maintain a database.
The DBMS is a general-purpose software system that facilitate of defining, constructing and
sharing database users and applications.
DBMS Software
❖ Microsoft access
❖ Oracle
❖ PostgreSQL
❖ Dbase
❖ SQLite
❖ IBM DB2
❖ Maria DB
❖ Microsoft SQL server
❖ My SQL
As a final notice, this manual is now ready to be given to the trainees (students) to help them
acquire the necessary skills and understandings, and then lead them to the level that they
2. Once the SQL Server Installation Center launches choose Installation tab (second from the
right).
3. In most cases you will want to run a New SQL Server New SQL Server stand-alone
installation, but other options are available, for example if you have SQL Server 2014 installed,
you have an option to update.
Otherwise
Step 8: Complete
In window 7
1. Start All Programs Microsoft SQL server SQL Server Management Studio.
Other way in window 10
2. Start All apps Microsoft SQL server SQL Server Management Studio.
5. Right-click either the Databases folder in the console tree or the white space in the right
pane, and choose New Database from the context menu.
6. You should now see the General tab of the Database properties sheet. Enter the
database name, and leave the owner as <default>.
database_name Is the name of the new database. Database names must be unique within an
instance of SQL Server
For example, to create a database with name ‘University, we write the following statement:
1. Expand your database Right Click on Tables and specify columns with their data
types
MODIFYING A DATABASE
We can drop a database either by right clicking the database and pressing Delete on the
context menu or using the following Drop
LAB EXPERIMENTS 1
Question
Solution
row……………………………………...……. tuple
column………………………………….……attribute
DATA TYPES
2. VARCHAR (Size) / VARCHAR2 (Size): This data type is used to store variable length
alphanumeric data. The maximum character can hold is 2000 character.
3. NUMBER (P, S): The NUMBER data type is used to store number (fixed or floating
point).
CREATE SCHEMA
CREATE TABLE
Specifies a new data base relation by giving it a name, and specifying each of its
attributes and their data types
CREATE TABLE <table name> ( <Attribute A1> <Data Type D1> [<
Constraints>], <Attribute A2> <Data Type D2> [< Constraints>], …….
DROP TABLE
ALTER TABLE
• Used to add an attribute to/from one of the base relations drop constraint -- The new
attribute will have NULLs in all the tuples of the relation right after the command is
executed; hence, the NOT NULL constraint is not allowed for such an attribute.
• The database users must still enter a value for the new attribute JOB for each
EMPLOYEE tuple.
This can be done using the UPDATE command.
Solution
1. create schema school
2. Create table
2.1. Department table
create table school.departement(Did char(8) primary key not null,
(data_1,data_2,........data_n);
3. DELETE-FROM: This is used to delete all the records of a relation but it will
retain the structure of that relation.
❖ ARIHMETIC OPERATORS:
(+): Addition - Adds values on either side of the operator.
(-): Subtraction - Subtracts right hand operand from left hand operand.
(*): Multiplication - Multiplies values on either side of the operator.
(/): Division - Divides left hand operand by right hand operand.
(^): Power- raise to power of.
(%): Modulus - Divides left hand operand by right hand operand and returns remainder.
❖ LOGICAL OPERATORS:
AND: The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
OR: The OR operator is used to combine multiple conditions in an SQL statement's WHERE
clause.
NOT: The NOT operator reverses the meaning of the logical operator with which it is used.
Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
❖ COMPARISION OPERATORS:
(=): Checks if the values of two operands are equal or not, if yes then condition becomes true.
(! =): Checks if the values of two operands are equal or not, if values are not equal then
condition becomes true.
(< >): Checks if the values of two operands are equal or not, if values are not equal then
condition becomes true.
(>): Checks if the value of left operand is greater than the value of right operand, if yes then
condition becomes true
SPECIAL OPERATOR:
BETWEEN: The BETWEEN operator is used to search for values that are within a set of
values, given the minimum value and the maximum value.
IS NULL: The NULL operator is used to compare a value with a NULL attribute value.
ALL: The ALL operator is used to compare a value to all values in another value set
ANY: The ANY operator is used to compare a value to any applicable value in the list according
to the condition.
LIKE: The LIKE operator is used to compare a value to similar values using wildcard operators.
It allows to use percent sign (%) and underscore (_) to match a given string pattern.
IN: The in operator is used to compare a value to a list of literal values that have been specified.
EXIST: The EXISTS operator is used to search for the presence of a row in a specified table that
meets certain criteria.
SET OPERATORS:
The Set operator combines the result of 2 queries into a single result. The following are the
operators:
• Union
• Union all
• Intersect
• Minus
Union: Returns all distinct rows selected by both the queries
Union all: Returns all rows selected by either query including the duplicates.
Intersect: Returns rows selected that are common to both queries.
Minus: Returns all distinct rows selected by the first query and are not by the second
VIEW
VIEW: 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 functions, WHERE, and JOIN statements to a view and present the data as
if the data were coming from one single table.
A view is a virtual table, which consists of a set of columns from one or more tables. It is
similar to a table but it does not store in the database. View is a query stored as an object.
Syntax: CREATE VIEW <view_name> AS SELECT <set of fields>
FROM relation_name WHERE (Condition)
Example:
SQL> CREATE VIEW employee AS SELECT empno,ename,job FROM EMP
WHERE job = ‘clerk’;
SQL> View created.
Example:
CREATE VIEW [Current Product List] AS
SELECT ProductID, ProductName
FROM Products
WHERE Discontinued=No;
UPDATING A VIEW : A view can updated by using the following syntax :
Syntax : CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name