Você está na página 1de 42

EXPERIMENT 1 

Objective: 
To get to know about different RDBMS software 
 
Problem Statement: 
Compare 5 different RDBMS software and explain in detail 
 
Comparison: 
What are RDBMS software? 
The relational model is the basis for a relational database management system 
(RDBMS). Essentially, an RDBMS moves data into a database, stores the data, and 
retrieves it so that it can be manipulated by applications. An RDBMS distinguishes 
between the following types of operations: 

● Logical operations:​ In this case, an application specifies ​what​ content is 


required. For example, an application requests an employee name or 
adds an employee record to a table. 
● Physical operations:​ In this case, the RDBMS determines h
​ ow​ things 
should be done and carries out the operation. For example, after an 
application queries a table, the database may use an index to find the 
requested rows, read the data into memory, and perform many other 
steps before returning a result to the user. The RDBMS stores and 
retrieves data so that physical operations are transparent to database 
applications. 

Now let’s study various RDBMS software: 

Oracle Database: O
​ racle Database is an RDBMS that implements 
object-oriented features such as user-defined types, inheritance and 
polymorphism. Oracle database has extended the relational model to an 
object-relational model, making it possible to store complex business models 


in a relational database. One characteristic of an RDBMS is the independence 
of physical data storage from logical data structures.  

a. Schema Objects: ​In Oracle Database, a database schema is a 


collection of logical data structures, or schema objects. A database 
schema is owned by a database user and has the same name as the 
user name. Schema objects are user-created structures that directly 
refer to the data in the database. The database supports many types 
of schema objects, the most important of which are tables and 
indexes. A schema object is one type of database object. 
i. TABLES: ​A table describes an entity such as employees. You 
define a table with a table name, such as e
​ mployees​, and set of 
columns. In general, you give each column a name, a data type, 
and a width when you create the table. We can optionally 
specify rules for each column of a table.  
ii. INDEXES: ​An index is an optional data structure that you can 
create on one or more columns of a table. Indexes can increase 
the performance of data retrieval. When processing a request, 
the database can use available indexes to locate the requested 
rows efficiently. Indexes are useful when applications often 
query a specific row or range of rows. 
b. Data Access: A
​ general requirement for a DBMS is to adhere to 
accepted industry standards for a data access language. 
i. SQL: ​SQL is a set-based declarative language that provides an 
interface to an RDBMS such as Oracle Database. In contrast to 
procedural languages such as C, which describe ​how​ things 
should be done, SQL is nonprocedural and describes w
​ hat 
should be done. Users specify the result that they want (for 
example, the names of current employees), not how to derive it. 
SQL is the ANSI standard language for relational databases. 


ii. PL/SQL and Java: P
​ L/SQL is a procedural extension to Oracle 
SQL. PL/SQL is integrated with Oracle Database, enabling you to 
use all of the Oracle Database SQL statements, functions, and 
data types. You can use PL/SQL to control the flow of a SQL 
program, use variables, and write error-handling procedures. A 
primary benefit of PL/SQL is the ability to store application logic 
in the database itself. 
c. Transaction Management: O
​ racle Database is designed as a 
multiuser database. The database must ensure that multiple users can 
work concurrently without corrupting one another's data. 
i. Transactions: A
​ n RDBMS must be able to group SQL 
statements so that they are either all committed, which means 
they are applied to the database, or all rolled back, which means 
they are undone. A transaction is a logical, atomic unit of work 
that contains one or more SQL statements. 
ii. Data Concurrency: ​A requirement of a multiuser RDBMS is the 
control of concurrency, which is the simultaneous access of the 
same data by multiple users. Without concurrency controls, 
users could change data improperly, compromising data 
integrity. For example, one user could update a row while a 
different user simultaneously updates it. 
iii. Data Consistency: ​In Oracle Database, each user must see a 
consistent view of the data, including visible changes made by a 
user's own transactions and committed transactions of other 
users. For example, the database must not permit a dirty read, 
which occurs when one transaction sees uncommitted changes 
made by another concurrent transaction. 
d. Oracle database Architecture: ​A database server is the key to 
information management. In general, a server reliably manages a large 
amount of data in a multiuser environment so that users can 
concurrently access the same data. A database server also prevents 


unauthorized access and provides efficient solutions for failure 
recovery. 
i. Database and Instance: A
​ n Oracle database server consists of 
a database and at least one database instance (commonly 
referred to as simply an instance). Because an instance and a 
database are so closely connected, the term Oracle database is 
sometimes used to refer to both instance and database. In the 
strictest sense the terms have the following meanings: 
● Database 
● A database is a set of files, located on disk, that store data. 
These files can exist independently of a database instance. 
● Database instance 
● An instance is a set of memory structures that manage 
database files. The instance consists of a shared memory 
area, called the system global area (SGA), and a set of 
background processes. An instance can exist independently 
of database files. 
ii. Database Storage Structures: ​An essential task of a relational 
database is data storage. This section briefly describes the 
physical and logical storage structures used by Oracle Database. 

Physical Storage Structures 


The physical database structures are the files that store the 
data. When you execute the SQL command CREATE DATABASE, 
the following files are created: 

● Data files: Every Oracle database has one or more physical 


data files, which contain all the database data. The data of 
logical database structures, such as tables and indexes, is 
physically stored in the data files. 
● Control files: Every Oracle database has a control file. A 
control file contains metadata specifying the physical 


structure of the database, including the database name 
and the names and locations of the database files. 
● Online redo log files: Every Oracle Database has an online 
redo log, which is a set of two or more online redo log files. 
An online redo log is made up of redo entries (also called 
redo records), which record all changes made to data. 

Logical Storage Structures 


This section discusses logical storage structures. The following 
logical storage structures enable Oracle Database to have 
fine-grained control of disk space use: 

● Data blocks: At the finest level of granularity, Oracle 


Database data is stored in data blocks. One data block 
corresponds to a specific number of bytes on disk. 
● Extents: An extent is a specific number of logically 
contiguous data blocks, obtained in a single allocation, 
used to store a specific type of information. 
● Segments: A segment is a set of extents allocated for a user 
object (for example, a table or index), undo data, or 
temporary data. 
● Tablespaces: A database is divided into logical storage units 
called tablespaces. A tablespace is the logical container for 
a segment. Each tablespace contains at least one data file. 
iii. Database Instance Structures: A
​ n Oracle database uses 
memory structures and processes to manage and access the 
database. All memory structures exist in the main memory of 
the computers that constitute the RDBMS. 


Oracle Database Processes: 
A process is a mechanism in an operating system that can run a 
series of steps. Some operating systems use the terms j​ ob​, t​ ask​, 
or t​ hread​. For the purpose of this discussion, a thread is 
equivalent to a process. An Oracle database instance has the 
following types of processes: 

● Client processes: These processes are created and 


maintained to run the software code of an application 
program or an Oracle tool. Most environments have 
separate computers for client processes. 
● Background processes: These processes consolidate 
functions that would otherwise be handled by multiple 
Oracle Database programs running for each client process. 
Background processes asynchronously perform I/O and 
monitor other Oracle Database processes to provide 
increased parallelism for better performance and reliability. 
● Server processes: These processes communicate with client 
processes and interact with Oracle Database to fulfill 
requests. 

Instance Memory Structures 


Oracle Database creates and uses memory structures for 
purposes such as memory for program code, data shared 
among users, and private data areas for each connected user. 
The following memory structures are associated with an 
instance: 

● System Global Area (SGA): The SGA is a group of shared 


memory structures that contain data and control 


information for one database instance. Examples of SGA 
components include cached data blocks and shared SQL 
areas. 
● Program Global Areas (PGA): A PGA is a memory region that 
contain data and control information for a server or 
background process. Access to the PGA is exclusive to the 
process. Each server process and background process has 
its own PGA. 

IBM DB2: ​DB2 is a database product from IBM. It is a Relational Database 


Management System (RDBMS). DB2 is designed to store, analyze and retrieve 
the data efficiently. DB2 product is extended with the support of 
Object-Oriented features and non-relational structures with XML. 

e. History:​ Initially, IBM had developed DB2 product for their specific 
platform. Since year 1990, it decided to develop a Universal Database 
(UDB) DB2 Server, which can run on any authoritative operating 
systems such as Linux, UNIX, and Windows. 
f. Versions:​ For IBM DB2, the UDB current version is 10.5 with the 
features of BLU Acceleration and its code name as 'Kepler'. 
g. Command Line Processor:​ The CLP can be started in one of the three 
modes: 
● Command  mode:  In  this  mode,  each  command  and  SQL 

statement  must  be  prefixed  by  “db2”.  For  example,  query  “db2 

activate database sample”. 

● Interactive  input  mode:  you  can  launch  this  mode  by  using  the 

“db2”  command.  Here,  you  can  pass  SQL  statements  without 

prefix. For example, “activate database sample”. 


● Batch  mode:  Here,  you  need  to  create  a  script  file,  which 

contains  all  SQL  queries  of  requirements  and  save  the  file with 

“.db2”  extension.  You  can  call  this  in  command  line  using 

syntax “db2 –tf <filename.db2>”. 

h. Database Architecture:​ A database is a collection of Tables, Schemas, 


Bufferpools, Logs, Storage groups and Tablespaces working together 
to handle database operations efficiently. 
i. Database Directory:​ Database directory is an organized 
repository of databases. When you create a database, all the 
details about database are stored in a database directory, such 
as details of default storage devices, configuration files, and 
temporary tables list etc. 
ii. Partitioned Global Directory:​ Directory Location : 
<instance>/NODExxx/SQLxxx 

The  partition-global  directory  contains  database related files as 


listed below. 

● Global deadlock write-to-file event monitoring files 


● Table space information files [SQLSPCS.1, SQLSPCS.2] 

● Storage group control files [SQLSGF.1, SQLSGF.2] 

● Temporary table space container files. [/storage 

path//T0000011/C000000.TMP/SQL00002.MEMBER0001.T

DA] 

● Global Configuration file [SQLDBCONF] 

● History files [DB2RHIST.ASC, DB2RHIST.BAK, 

DB2TSCHG.HIS, DB2TSCHG.HIS] 


● Logging-related files [SQLOGCTL.GLFH.1, 

SQLOGCTL.GLFH.2] 

● Locking files [SQLINSLK, SQLTMPLK] 

● Automatic Storage containers 

iii. Member Specific Directory:​ Directory location : 


/NODExxxx/SQLxxxx/MEMBER0000 

This directory contains: 

● Objects associated with databases 


● Buffer pool information files [SQLBP.1, SQLBP.2] 

● Local event monitoring files 

● Logging-related files [SQLOGCTL.LFH.1, SQLOGCTL.LFH.2, 

SQLOGMIR.LFH]. 

● Local configuration files 

● Deadlocks event monitor file. The detailed deadlock 

events monitor files are stored in the database directory 

of the catalog node in case of ESE and partitioned 

database environment. 

MICROSOFT SQL SERVER:​ Microsoft SQL Server is a relational database 


management system, or RDBMS, that supports a wide variety of transaction 
processing, business intelligence and analytics applications in corporate IT 
environments. It's one of the three market-leading database technologies, 
along with Oracle Database and IBM's DB2. Like other RDBMS software, 
Microsoft SQL Server is built on top of SQL, a standardized programming 
language that database administrators (DBAs) and other IT professionals use 
to manage databases and query the data they contain. SQL Server is tied to 


Transact-SQL (T-SQL), an implementation of SQL from Microsoft that adds a 
set of proprietary programming extensions to the standard language. 

i. It  is  a  software,  developed  by  Microsoft,  which  is  implemented  from 

the specification of RDBMS. 

j. It is also an ORDBMS. 

k. It is platform dependent. 

l. It is both GUI and command based software. 

m. It  supports  SQL  (SEQUEL)  language  which  is  an  IBM  product, 

non-procedural, common database and case insensitive language. 

Usage Of SQL server: 

a. To create databases. 

b. To maintain databases. 

c. To analyze the data through SQL Server Analysis Services (SSAS). 

d. To generate reports through SQL Server Reporting Services 

(SSRS). 

e. To carry out ETL operations through SQL Server Integration 

Services (SSIS). 

SQL Server Components: 

SQL  Server  works  in  client-server  architecture,  hence  it  supports  two 
types of components − (a) W
​ orkstation​ and (b) ​Server​. 

10 
● Workstation  components  are  installed  in  every  device/SQL 
Server  operator’s  machine.  These  are  just  interfaces to interact 

with  Server  components.  Example:  SSMS,  SSCM,  Profiler,  BIDS, 

SQLEM etc. 

● Server  components  are  installed  in  centralized  server.  These 

are  services.  Example:  SQL  Server,  SQL  Server  Agent,  SSIS, 

SSAS, SSRS, SQL browser, SQL Server full text search etc. 

Instance of SQL Server:  

a. An instance is an installation of SQL Server. 

b. An instance is an exact copy of the same software. 

c. If we install 'n' times, then 'n' instances will be created. 

d. There are two types of instances in SQL Server a) Default b) 

Named. 

e. Only one default instance will be supported in one Server. 

f. Multiple named instances will be supported in one Server. 

g. Default instance will take the server name as Instance name. 

h. Default instance service name is MSSQLSERVER. 

i. 16 instances will be supported in 2000 version. 

j. 50 instances will supported in 2005 and later versions. 

Advantages of Instances: 

a. To install different versions in one machine. 

b. To reduce cost. 

11 
c. To maintain production, development, and test environments 

separately. 

d. To reduce temporary database problems. 

e. To separate security privileges. 

f. To maintain standby server.

MONGODB Database Overview: ​MongoDB is the leading NoSQL database, 


designed for how we build and run applications today. MongoDB empowers 
organizations to be agile and scalable. It helps them enable new types of 
applications, improve customer experience, accelerate time to market and 
reduce total cost of ownership (TCO). 

MongoDB is a general purpose, open-source database. ​MongoDB features​: 

● Document data model with dynamic schemas 


● Full, flexible index support and rich queries 
● Auto-Sharding for horizontal scalability 
● Built-in replication for high availability 
● Text search 
● Advanced security 
● Aggregation Framework and MapReduce 
● Large media storage with GridFS 

Organizations of all sizes use MongoDB to quickly and easily develop, scale 
and operate applications. Instead of storing data in rows and columns as one 
would with a relational database, MongoDB stores a binary form of JSON 
documents (BSON). 

12 
Relational databases impose flat, rigid schemas across many tables. By 
contrast, MongoDB is an agile database that allows schemas to vary across 
documents and to change quickly as applications evolve, while still providing 
the functionality developers expect from relational databases, such as 
secondary indexes, a full query language and strong consistency. 

MongoDB Core Features: 

● Support ad hoc queries 


● Indexing 
● Replication 
● Duplication of data 
● Load balancing 
● Supports map reduce and aggregation tools. 
● Uses JavaScript instead of Procedures. 
● It is a schema-less database written in C++. 
● Provides high performance. 
● Stores files of any size easily without complicating your stack. 
● Easy to administer in the case of failures. 

Advantages of MongoDB: 

● It’s fast and easy to use. 


● The engine supports JSON and other  
● NoSQL documents. 
● Data of any structure can be stored and accessed quickly and easily. 
● Schema can be written without downtime. 
● Disadvantages of MongoDB: 
● SQL is not used as a query language. 

13 
● Tools to translate SQL to MongoDB queries are available, but they add 
an extra step to using the engine. 
● Setup can be a lengthy process. 
● Default settings are not secure. 

MYSQL Overview: M
​ ySQL is an Oracle-backed open source relational database 
management system (RDBMS) based on Structured Query Language (SQL). MySQL 
runs on virtually all platforms, including Linux, UNIX and Windows. Although it can 
be used in a wide range of applications, MySQL is most often associated with web 
applications and online publishing. 

MySQL is an important component of an open source enterprise stack called LAMP. 


LAMP is a web development platform that uses Linux as the operating system, 
Apache as the web server, MySQL as the relational database management system 
and PHP as the object-oriented scripting language. (Sometimes Perl or Python is 
used instead of PHP.) 

Originally conceived by the Swedish company MySQL AB, MySQL was acquired by 
Sun Microsystems in 2008 and then by Oracle when it bought Sun in 2010. 
Developers can use MySQL under the GNU General Public License (GPL), but 
enterprises must obtain a commercial license from Oracle. 

Core MySQL features: 

MySQL enables data to be stored and accessed across multiple storage engines, 
including InnoDB, CSV, and NDB. MySQL is also capable of replicating data and 
partitioning tables for better performance and durability. MySQL users aren't 

14 
required to learn new commands; they can access their data using standard SQL 
commands. 

MySQL is written in C and C++ and accessible and available across over 20 
platforms, including Mac, Windows, Linux and Unix. The RDBMS supports large 
databases with millions records and supports many data types including signed or 
unsigned integers 1, 2, 3, 4, and 8 bytes long; FLOAT; DOUBLE; CHAR; VARCHAR; 
BINARY; VARBINARY; TEXT; BLOB; DATE; TIME; DATETIME; TIMESTAMP; YEAR; SET; 
ENUM; and OpenGIS spatial types. Fixed- and variable-length string types are also 
supported. 

For security, MySQL uses an access privilege and encrypted password system that 
enables host-based verification. MySQL clients can connect to MySQL Server using 
several protocols, including TCP/IP sockets on any platform. MySQL also supports a 
number of client and utility programs, command-line programs and administration 
tools such as MySQL Workbench. 

Offshoots of MySQL​, also known as forks, i​ nclude the following:  

1. Drizzle, a lightweight open source database management system in 


development based on MySQL 6.0; 
2. MariaDB, a popular community-developed "drop-in" replacement for MySQL 
that uses MySQL APIs and commands; and  
3. Percona Server with XtraDB, an enhanced version of MySQL known for 
horizontal scalability. 

Advantages of MySQL: 

● Easy to Use 

15 
● Support available when needed 
● Open Source 
● Incredible Inexpensive 
● Industry Standard 

Disadvantages of MySQL: 

● Stability issues 
● Poor performance 
● Development isn’t community driven 
● Heavily dependant on addons 

16 
EXPERIMENT 2 
Objective: 
To learn the features of SQL 
 
What is SQL? 
SQL (Structured Query Language) is a standardized programming language that's 
used to manage relational databases and perform various operations on the data 
in them. Initially created in the 1970s, SQL is regularly used not only by database 
administrators, but also by developers writing data integration scripts and data 
analysts looking to set up and run analytical queries. 
 
The uses of SQL include modifying database table and index structures; adding, 
updating and deleting rows of data; and retrieving subsets of information from 
within a database for transaction processing and analytics applications. Queries 
and other SQL operations take the form of commands written as statements -- 
commonly used SQL statements include select, add, insert, update, delete, create, 
alter and truncate. 
 
Some characteristics of SQL: 
● SQL is easy to learn. 
● SQL is used to access data from relational database management systems. 
● SQL can execute queries against the database. 
● SQL is used to describe the data. 
● SQL is used to define the data in the database and manipulate it when 
needed. 
● SQL is used to create and drop the database and table. 
● SQL is used to create a view, stored procedure, function in a database. 
● SQL allows users to set permissions on tables, procedures, and views. 
 
 
Basic SQL Queries: 
● 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 

17 
● ALTER TABLE - modifies a table 
● DROP TABLE - deletes a table 
● CREATE INDEX - creates an index (search key) 
● DROP INDEX - deletes an index 
 
Advantages of SQL: 
● High Speed:  
○ SQL Queries can be used to retrieve large amounts of records from a 
database quickly and efficiently. 
● Well Defined Standards Exist: 
○ SQL databases use long-established standard, which is being adopted 
by ANSI & ISO. Non-SQL databases do not adhere to any clear 
standard. 
● No Coding Required: 
○ Using standard SQL it is easier to manage database systems without 
having to write substantial amount of code. 
● Emergence of ORDBMS: 
○ Previously SQL databases were synonymous with relational database. 
With the emergence of Object Oriented DBMS, object storage 
capabilities are extended to relational databases. 
 
Disadvantages of SQL: 
● Difficulty in Interfacing: 
○ Interfacing an SQL database is more complex than adding a few lines 
of code. 
● More Features Implemented in Proprietary way: 
○ Although SQL databases conform to ANSI & ISO standards, some 
databases go for proprietary extensions to standard SQL to ensure 
vendor lock-in. 
 
 
 
 
 
 
 
 
 

18 
 
EXPERIMENT 3 
Objective: 
To get to know what an ER diagram is, what it’s components are, and how to draw 
an ER diagram. 
 
What is an ER Diagram? 
An ER diagram is an Entity Relationship Diagram also known as Entity Relationship 
Model. It​ i​ s a graphical representation of an information system that depicts the 
relationships among people, objects, places, concepts or events within that system. 
 
An ER Model is used to model the logical view of the system from data perspective 
which consists of these components: 
 
Attribute(s): 
Attributes are the properties which define the entity type.  
For example, Roll_No, Name, DOB, Age, Address, Mobile_No are the attributes 
which defines entity type Student. In ER diagram, ​attribute​ is ​represented by​ an 
oval​. 
 
Key Attribute​ – 
The attribute which uniquely identifies each entity in the entity set is called key 
attribute. 
For example, Roll_No will be unique for each student. In ER diagram, k ​ ey attribute 
is ​represented by a ​ n​ oval with underlying lines​. 
 
Composite Attribute​ – 
An attribute composed of many other attribute is called as composite attribute.  
For example, Address attribute of student Entity type consists of Street, City, State, 
and Country. In ER diagram, ​composite attribute​ is ​represented by​ an o ​ val 
comprising of ovals​. 
 
Multivalued Attribute – 
An attribute consisting more than one value for a given entity.  
For example, Phone_No (can be more than one for a given student). In ER diagram, 
multivalued attribute ​is​ represented by double oval​. 
 
Derived Attribute – 

19 
An attribute which can be derived from other attributes of the entity type is known 
as derived attribute.  
e.g.; Age (can be derived from DOB). In ER diagram, d ​ erived attribute ​is 
represented by dashed oval​. 
Relationship Type and Relationship Set: 
A relationship type represents the association between entity types.  
For example,‘Enrolled in’ is a relationship type that exists between entity type 
Student and Course. In ER diagram, r​ elationship type ​is​ represented by a ​  
diamond ​and​ connecting the entities with lines​. 
 
Degree of a relationship set: 
The number of different entity sets participating in a relationship set is called as 
degree of a relationship set. 
● Unary Relationship​ – When there is only ONE entity set participating in a 
relation, the relationship is called as unary relationship. For example, one 
person is married to only one person. 
● Binary Relationship –​ When there are TWO entities set participating in a 
relation, the relationship is called as binary relationship.For example, Student 
is enrolled in Course. 
● n-ary Relationship ​– When there are n entities set participating in a relation, 
the relationship is called as n-ary relationship. 
 
Cardinality: 
The number of times an entity of an entity set participates in a relationship set is 
known as cardinality.  
Cardinality can be of different types: 
● One to one ​– When each entity in each entity set can take part only once in 
the relationship, the cardinality is one to one. Let us assume that a male can 
marry to one female and a female can marry to one male. So the relationship 
will be one to one. 
● Many to one​ – When entities in one entity set can take part only once in the 
relationship set and entities in other entity set can take part more than once 
in the relationship set, cardinality is many to one. Let us assume that a 
student can take only one course but one course can be taken by many 
students. So the cardinality will be n to 1. It means that for one course there 
can be n students but for one student, there will be only one course. 
● Many to many​ – When entities in all entity sets can take part more than 
once in the relationship cardinality is many to many. Let us assume that a 

20 
student can take more than one course and one course can be taken by 
many students. So the relationship will be many to many. 
 
Example of ER Model: 
1. College Management System 

 
2. Employee Management System 
 

 
 
  

21 
EXPERIMENT 4 
 
Objective: 
To learn how to edit a table in SQL using queries 
 
Problem Statement: 
Create a table ‘Student’ with attributes — Roll Number, Name, Date of Birth, Phone 
Number, Address and Course. Also, perform operations using INSERT, SELECT and 
WHERE commands. 
 
Explanation: 
The various commands in the order in which they were used in the experiment are: 
1. CREATE: This command is used to create a table 
2. INSERT: This command is used to insert entries in a table, which may include 
the values of all the attributes or just the selected ones. 
3. SELECT: This command is used to select data from the database. Using this 
command we can either select the entire table (SELECT * FROM table) or 
chosen columns (SELECT Name FROM table). 
4. WHERE: The where clause is used to filter records. This is used to filter only 
those records which fulfill a specified condition.  
 
OUTPUT: 
Creation of Table : 

  

Insertion of Entries: 

22 
 

Displaying Entries based on Conditions: 

  

CONCLUSION: 
Here we learnt how to create a table and perform operations on it like 
displaying an entry based on a condition on one of the attributes. 

23 
EXPERIMENT 5 
Objective​:  
To learn how to multiple tables are connected via Foreign Key.  

Problem statement: 
Perform Queries on multiple tables, that is, a table ‘Course’ with the following 
attributes — Course Code (unique identifier), Description, Duration, Level; and 
another table ‘Student’ with the attributes — Roll Number, Name, Address, Phone 
number, Course (Foreign Key related to Course Code of ‘Course’). 

Following queries are to be performed — 

1.​ T
​ he total number of Under Graduate Students and Post Graduate Students 

1.​ L
​ ist of Students in UG and PG Courses 

2.​ L
​ ist of Students and their course Description for Students of Software 

Engineering Department 

3.​ L
​ ist of Students in a 2 year course from Software Engineering Department 

  

Explanation​: 
A FOREIGN KEY is a column or group of columns in a relational database 
table that provides a link between data in two tables.  

It acts as a cross-reference between tables because it references the 


primary key of another table, thereby establishing a link between them.  

The table containing the foreign key is called the child table, and the table 
containing the candidate key is called the referenced or parent table. 

The different commands used in this experiment are — 

1. CREATE: This command is used to create a table. 

2. INSERT: This command is used to insert entries in a table, which may 


include the values of all the attributes or just the selected ones. 

24 
3. SELECT - This command is used to select data from the database. Using 
this command we can either select the entire table (SELECT * FROM 
table) or chosen columns (SELECT Name FROM table). 

4. WHERE - The where clause is used to filter records. This is used to filter 
only those records which fulfill a specified condition.  

5. REFERENCES - ​The ​references keyword​ is used to define which table and


column is used in a foreign key relationship.

6. COUNT(*) - T
​ he COUNT() function returns the number of rows that
matches a specified criteria. 

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

Output​: 
Creation of Table Course1 

Creation of ‘Student’ Table using REFERENCES command to relate both tables 

Insertion and Display of ‘Course’ Table 

25 
 

Insertion and Display of ‘Student’ Table 

26 
 

Count of UG AND PG students respectively 

27 
 

List of Students in UG and PG Courses (respectively) 

List of Students and their course Description for Students of Software 


Engineering Department 

  

28 
List of Students in a 2 year course from Software Engineering Department 

Conclusion​: 
In this experiment we learnt how to relate two tables and create a Foreign Key 
referenced to an attribute of another table and display entries from both the tables 
together as one table. 

29 
EXPERIMENT 6 
  

Objective​: 
To get a better grasp of SQL by learning about new commands 

Problem Statement​: 

Use the following commands on existing tables — 

  1. ALL 

  2. ALTER TABLE 

  3. ANY 

  4. EXISTS 

  5. GROUP BY 

  6. FULL JOIN 

  7. LEFT JOIN 

  8. RIGHT JOIN 

  9. ORDER BY 

    10. UPDATE 

OUTPUT WITH EXPLANATION​: 


1.​ ​ ALL Operator 

ALL operator is used to select all tuples of SELECT statement. It is also used to 
compare a value to every value in another value set or result from a subquery. 

   

30 
Here, it selected all the values in Student Table where CourseCode is ‘ECEUG’ and 
displayed the Address column only. 

  

2. A
​ LTER TABLE Command 

ALTER TABLE is used to add, delete or modify columns in the existing table. 

ALTER TABLE - ADD 

ADD is used to add columns into the existing table. 

  

Here, Date of Birth column was added into the table and all the values are set to 
NULL by default. 

ALTER TABLE - DROP COLUMN 

DROP COLUMN is used to drop column in a table. 

31 
 

Here, we deleted the PhoneNo column (as compared to above table). 

ALTER TABLE - MODIFY 

It is used to modify the existing columns in a table. 

This command modified the Address column’s size to 100 bytes. 

3. A
​ NY Operator 

ANY compares a value to each value in a list or results from a query and evaluates 
to true if the result of an inner query contains at least one row. 

32 
 

This operator displayed the records in the Course table that have Duration = ‘2 
years’. 

  

4. E
​ XISTS Condition 

The EXISTS Condition in SQL is used to check whether the result of a correlated 
nested query is empty or not. 

This helped us fetch the name of the students whose CourseCode matched with 
the CourseCode in the Course table. 

  

5. G
​ ROUP BY Statement 

33 
The GROUP BY Statement in SQL is used to arrange identical data into groups with 
the help of some functions It is used with the SELECT Statement. 

  

For the table Student, we grouped the data into the Subjects and Year and also 
displayed their count. 

6. L
​ EFT JOIN Statement 

The LEFT JOIN statement gives all rows in A, plus any common rows in B. If a record 
in A doesn’t exist in B, it will return NULL for that row. 

Here, since the last four tuples do not exist in the table Joining, their values for the 
attributes Name, CourseID and Subject are NULL 

  

34 
7. R
​ IGHT JOIN Statement 

The RIGHT JOIN statement gives all rows in table B, plus any common rows in A. If a 
record in B doesn’t exist in A, it will return NULL for that row. 

Here, the last three tuples have NULL values since those don’t exist in the table 
Students. 

  

8. F
​ ULL JOIN 

MySQL does not support FULL JOIN, so we have to combine RIGHT JOIN, UNION, 
LEFT JOIN to get an equivalent. It gives the results of A union B. It returns all records 
from both tables. Those columns which exist only in one table will contain NULL in 
the opposite table. 

35 
 

Here, the values that don’t exist in the other table have been given NULL values. 

9. O
​ RDER BY Clause 

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. 

  

10. ​UPDATE Statement 

36 
The UPDATE Command is used to modify rows in a table. It can be used to update a 
single field or multiple fields at the same time. 

Conclusion: 
Through this experiment, we learnt about the various Commands and   
Conditions that can be performed in SQL, on Single and Multiple Tables. 

  

  

37 
EXPERIMENT 7 
Objective​: To get familiarized with the concept of nested/embedded queries 

Problem Statement: 
Create 2 tables Course and Student. 

A  Students  can  apply  for  only  one  course.  The  course  details  are  in  Course  table 
and  the  Student  Table  should  contain  a  FOREIGN KEY to refer to the Primary Key of 
Course table – “cid”. 

•  P​ opulate  data  into  the  tables  and  then  perform  the  following  using  SQL 

queries. 

◦​ F
​ ind Roll Numbers of All students living in Jaipur 

◦​ F
​ ind Courses where UG students are enrolled 

◦​ F
​ ind Roll Number of all students in UG level 

◦​ F
​ ind Contact Number of all students of course DBMS. 

Explanation​: 
Subqueries provide an easy and efficient way to handle the queries that depend on 
the results from another query. 

A Subquery or Inner query or a Nested query is a query within another SQL query 
and embedded within the WHERE clause. 

A subquery is used to return data that will be used in the main query as a condition 
to further restrict the data to be retrieved. 

Subqueries can be used with the SELECT, INSERT, UPDATE and DELETE statements 
along with the operators like =, <, >, <=, >=, IN, BETWEEN, etc. 

Output​: 
Display of ‘Student1’ Table 

38 
 

Display of ’Course1’ Table 

Roll Numbers of student in Jaipur- 

39 
 

Courses where UG students are enrolled- 

Roll Numbers of Students in UG courses- 

40 
Address of Students in Course Computer Engg-UG 

CONCLUSION​: 
We learnt about Nested/Embedded Sub-queries. 

  

  

  

  

  

  

  

  

41 
  

42 

Você também pode gostar