Sunteți pe pagina 1din 55

NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

EXPERIMENT NO 1
AIM: To study about database management system (DBMS) and relational database
management system (RDBMS).

DATABASE MANAGEMENT SYSTEM

DBMS is the abbreviated form of Data Base Management System.

Database management system is a computer software component introduced during 1960′s. It


is used for controlling various databases in the desktop computer or server. It was also termed
as Navigational Database Management System. During 1970′s RDBMS or Relational
database management system came into existence. Database management system is the means
of controlling databases either in the hard disk in a desktop system or on a network. Database
management system is used for creating database, maintained database and provides the
means of using the database. DBMS doesn’t consider relationship between the tables. Instead
it will take the approach of manual navigation. This has led to severe performance problems
when dealing with complex and high amount of data. DBMS has several components. Some
of the major components are external interface, database language engine, query optimizer,
database engine, storage engine, DBMS management component, etc. External Interface is
used to communicate with the DBMS as well as the databases.

Database language engine is the one which interprets the query language and do the necessary
action supplied using the query language. Query optimizer is used to optimize the supplied
query language and identifies the best plan for executing the query and obtaining the result as
fast as it could. Database engine is used to create or manipulate the data in the database
objects like table.

A database management system is the software system that allows users to define, create and
maintain a database and provides controlled access to the data. It is basically a collection of
programs that enables users to store, modify, and extract information from a database as per
the requirements. It is an intermediate layer between programs and the data. Programs access
the DBMS, which then accesses the data. There are different types of DBMS ranging from
small systems that run on personal computers to huge systems that run on mainframes. The
following are main examples of database applications:

• Computerized library systems


• Automated teller machines
• Flight reservation systems
• Computerized parts inventory systems

A database management system is a piece of software that provides services for accessing a
database, while maintaining all the required features of the data. Commercially available
Database management systems in the market are dbase, FoxPro, IMS and Oracle, MySQL,

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

SQL Servers and DB2 etc. These systems allow users to create update, and extract
information from their databases.

RELATIONAL DATABASE MANAGEMENT SYSTEM

RDBMS is the abbreviated form of Relational Data Base Management System. It was
introduced in 1970′s. RDBMS avoided the navigation model as in old DBMS and introduced
Relational model. The relational model has relationship between tables using primary keys,
foreign keys and indexes. Thus the fetching and storing of data become faster than the old
Navigational model. So RDBMS is widely used by the enterprises and developers for storing
complex and large amount of data.

A relational DBMS is special system software that is used to manage the organization,
storage, access, security and integrity of data. This specialized software allows application
systems to focus on the user interface, data validation and screen navigation. When there is a
need to add, modify, delete or display data, the application system simply makes a "call" to
the RDBMS.

Although there are many different types of database management systems, relational
databases are by far the most common. Other types include hierarchical databases and
network databases.
A relational database is a digital database whose organization is based on the relational model
of data, as proposed by E.F. Codd in 1970. This model organizes data into one or more tables
(or "relations") of rows and columns, with a unique key for each row. Generally, each entity
type described in a database has its own table, the rows representing instances of that entity
and the columns representing the attribute values describing each instance. Because each row
in a table has its own unique key, rows in other tables that are related to it can be linked to it
by storing the original row's unique key as an attribute of the secondary row (where it is
known as a "foreign key"). Codd showed that data relationships of arbitrary complexity can be
represented using this simple set of concepts. Prior to the advent of this model, databases were
usually hierarchical, and each tended to be organized with a unique mix of indexes, chains,
and pointers. The simplicity of the relational model led to its soon becoming the predominant
type of database.
Virtually all relational database systems use SQL (Structured Query Language) as the
language for querying and maintaining the database.
Advantages of RDBMS
1. Data Structure: The table format is simple and easy for database users to understand
and use. RDBMSs provide data access using a natural structure and organization of the
data. Database queries can search any column for matching entries.
2. Multi-User Access: RDBMSs allow multiple database users to access a database
simultaneously. Built-in locking and transactions management functionality allow

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

users to access data as it is being changed, prevents collisions between two users
updating the data, and keeps users from accessing partially updated records.
3. Privileges: Authorization and privilege control features in an RDBMS allow the
database administrator to restrict access to authorized users, and grant privileges to
individual users based on the types of database tasks they need to perform.
Authorization can be defined based on the remote client IP address in combination
with user authorization, restricting access to specific external computer systems.
4. Network Access: RDBMSs provide access to the database through a server daemon, a
specialized software program that listens for requests on a network, and allows
database clients to connect to and use the database. Users do not need to be able to log
in to the physical computer system to use the database, providing convenience for the
users and a layer of security for the database. Network access allows developers to
build desktop tools and Web applications to interact with databases.
5. Speed: The relational database model is not the fastest data structure. RDBMS
advantages, such as simplicity, make the slower speed a fair trade-off. Optimizations
built into an RDBMS and the design of the databases, enhance performance, allowing
RDBMSs to perform more than fast enough for most applications and data sets.
Improvements in technology, increasing processor speeds and decreasing memory and
storage costs allow systems administrators to build incredibly fast systems that can
overcome any database performance shortcomings.
6. Maintenance: RDBMSs feature maintenance utilities that provide database
administrators with tools to easily maintain, test, repair and back up the databases
housed in the system. Many of the functions can be automated using built-in
automation in the RDBMS, or automation tools available on the operating system.
7. Language: RDBMSs support a generic language called "Structured Query Language"
(SQL). The SQL syntax is simple, and the language uses Standard English language
keywords and phrasing, making it fairly intuitive and easy to learn. Many RDBMSs
add non-SQL, database-specific keywords, functions and features to the SQL
language.

Disadvantages of RDBMS
1. Cost: One disadvantage of relational databases is the expensive of setting up and
maintaining the database system. In order to set up a relational database, you generally
need to purchase special software. If you are not a programmer, you can use any
number of products to set up a relational database. It does take time to enter in all the
information and set up the program. If your company is large and you need a more
robust database, you will need to hire a programmer to create a relational database
using Structured Query Language (SQL) and a database administrator to maintain the
database once it is built. Regardless of what data you use, you will have to either
import it from other data like text files or Excel spreadsheets, or have the data entered
at the keyboard. No matter the size of your company, if you store legally confidential
or protected information in your database such as health information, social security

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

numbers or credit card numbers, you will also have to secure your data against
unauthorized access in order to meet regulatory standards.
2. Abundance of Information: Advances in the complexity of information cause
another drawback to relational databases. Relational databases are made for organizing
data by common characteristics. Complex images, numbers, designs and multimedia
products defy easy categorization leading the way for a new type of database called
object-relational database management systems. These systems are designed to handle
the more complex applications and have the ability to be scalable.
3. Structured Limits: Some relational databases have limits on field lengths. When you
design the database, you have to specify the amount of data you can fit into a field.
Some names or search queries are shorter than the actual, and this can lead to data
loss.
4. Isolated Databases: Complex relational database systems can lead to these databases
becoming "islands of information" where the information cannot be shared easily from
one large system to another. Often, with big firms or institutions, you find relational
databases grew in separate divisions differently. For example, maybe the hospital
billing department used one database while the hospital personnel department used a
different database. Getting those databases to "talk" to each other can be a large, and
expensive, undertaking, yet in a complex hospital system, all the databases need to be
involved for good patient and employee care.

DBMS vs. RDBMS

• Relationship among tables is maintained in a RDBMS whereas this not the case DBMS as
it is used to manage the database.
• DBMS accepts the ‘flat file’ data that means there is no relation among different data
whereas RDBMS does not accept this type of design.
• DBMS is used for simpler business applications whereas RDBMS is used for more
complex applications.
• Although the foreign key concept is supported by both DBMS and RDBMS but its only
RDBMS that enforces the rules
• RDBMS solution is required by large sets of data whereas small sets of data can be
managed by DBMS.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

EXPERIMENT NO 2
AIM: To study about SQL (Structured Query language).

SQL (STRUCTURED QUERY LANGUAGE)


SQL is a standardized query language for requesting information from a database. The
original version called SEQUEL (structured English query language) was designed by
an IBM research center in 1974 and 1975. SQL was first introduced as a commercial system
in 1979 by Oracle Corporation.
Historically, SQL has been the favorite query language for database management systems
running on minicomputers and mainframes. Increasingly, however, SQL is
being supported by PC database systems because it supports distributed databases (databases
that are spread out over several computer systems). This enables several users on a local-area
network to access the same database simultaneously.
SQL is a special-purpose programming language designed for managing data held in
a relational database management system (RDBMS), or for stream processing in a relational
data stream management system (RDSMS).Originally based upon relational algebra and tuple
relational calculus, SQL consists of a data definition language and a data. The scope of SQL
includes data insert, query, update and delete, schema creation and modification, and data
access control. Although SQL is often described as, and to a great extent is, a declarative
language (4GL), it also includes procedural elements.

LANGUAGE ELEMENT

The SQL language is subdivided into several language elements, including:


• Clauses, which are constituent components of statements and queries. (In some cases,
these are optional.)
• Expressions, which can produce either scalar values, or tables consisting of columns and
rows of data
• Predicates, which specify conditions that can be evaluated to SQL three-valued logic
(3VL)(true/false/unknown) or Boolean truth values and are used to limit the effects of
statements and queries, or to change program flow.
• Queries, which retrieve the data based on specific criteria. This is an important element
of SQL.
• Statements, which may have a persistent effect on schemata and data, or may
control transactions, program flow, connections, sessions, or diagnostics.
• SQL statements also include the semicolon (";") statement terminator. Though not
required on every platform, it is defined as a standard part of the SQL grammar.
• Insignificant whitespace is generally ignored in SQL statements and queries, making it
easier to format SQL code for readability.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

TYPES OF SQL STATEMENTS

The tables in the following sections provide a functional summary of SQL statements and are
divided into these categories:
• Data Definition Language (DDL) Statements
• Data Manipulation Language (DML) Statements
• Transaction Control Statements
• Session Control Statements
• System Control Statement
• Embedded SQL Statements

DATA DEFINITION LANGUAGES (DDL) STATEMENTS


Data definition language (DDL) statements let you to perform these tasks:
 Create, alter, and drop schema objects
 Grant and revoke privileges and roles
 Analyze information on a table, index, or cluster
 Establish auditing options
 Add comments to the data dictionary
The CREATE, ALTER, and DROP commands require exclusive access to the specified
object. For example, an ALTER TABLE statement fails if another user has an open
transaction on the specified table.
The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not require
exclusive access to the specified object. For example, you can analyze a table while other
users are updating the table.
The Data Definition Language (DDL) manages table and index structure. The most basic
items of DDL are the CREATE, ALTER, RENAME, DROP and TRUNCATE statements:
 CREATE creates an object (a table, for example) in the database.
 ALTER modifies the structure of an existing object in various ways, for example,
adding a column to an existing table or a constraint.
 TRUNCATE deletes all data from a table in a very fast way, deleting the data inside
the table and not the table itself. It usually implies a subsequent COMMIT operation, i.e.,
it cannot be rolled back (data is not written to the logs for rollback later, unlike DELETE).
 DROP deletes an object in the database, usually irretrievably, i.e., it cannot be rolled
back.

DATA MANUPULATION (DML) STATEMENTS


Data manipulation language (DML) statements access and manipulate data in existing schema
objects. These statements do not implicitly commit the current transaction. The data
manipulation languages statement is:
 INSERT adds rows to an existing table,
 UPDATE modifies a set of existing table rows, e.g.:
 DELETE removes existing rows from a table, e.g.:

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

 MERGE is used to combine the data of multiple tables. It combines


the INSERT and UPDATE elements. It is defined in the SQL:2003 standard; prior to
that, some databases provided similar functionality via different syntax, sometimes
called "upset".
 The SELECT statement is a limited form of DML statement in that it can only access
data in the database. It cannot manipulate data in the database, although it can operate
on the accessed data before returning the results of the query.
 The CALL and EXPLAIN PLAN statements are supported in PL/SQL only when
executed dynamically. All other DML statements are fully supported in PL/SQL.
TRANSACTION CONTROL STATEMENTS
Transaction control statements manage changes made by DML statements. The transaction
control statements are:
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
All transaction control statements, except certain forms of START
TRANSACTION (or BEGIN WORK, or BEGIN TRANSACTION, depending on SQL
dialect) marks the start of a database transaction, which either completes entirely or not at all.
 SAVE TRANSACTION (or SAVEPOINT) saves the state of the database at the
current point in transaction
 COMMIT makes all data changes in a transaction permanent.
 ROLLBACK discards all data changes since the last COMMIT or ROLLBACK,
leaving the data as it was prior to those changes. Once the COMMIT statement completes,
the transaction's changes cannot be rolled back.
COMMIT and ROLLBACK terminate the current transaction and release data locks. In the
absence of a START TRANSACTION or similar statement, the semantics of SQL are
implementation-dependent. The following example shows a classic transfer of funds
transaction, where money is removed from one account and added to another. If either the
removal or the addition fails, the entire transaction is rolled back.

SESSION CONTROL STATEMENT


Session control statements dynamically manage the properties of a user session. These
statements do not implicitly commit the current transaction.PL/SQL does not support session
control statements. The session control statements are:
ALTER SESSION
SET ROLE

SYSTEM CONTROL STATEMENTS

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The single system control statement, ALTER SYSTEM, dynamically manages the properties


of an Oracle Database instance. This statement does not implicitly commit the current
transaction and is not supported in PL/SQL.

EXPERIMENT NO 3

AIM: Create a database and write the programs to carry out the following
operation:
1. Create a Table.
2. Add a record in the database.
3. Delete a record in the database.
4. Display the record in the database.
5. Update the record in the database.
6. List all the records of database in ascending/descending order.
7. Drop Table.

CREATE TABLE
The CREATE TABLE statement is used to create a table in a database.
Tables are organized into rows and columns; and each table must have a name.
SYNTAX
CREATE TABLE< table_name>
(
<column_name1> data_type(size),
<column_name2> data_type(size),
<column_name3>data_type(size),
....
);
The column_name parameters specify the names of the columns of the table. The data_type
parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal,
date, etc.). The size parameter specifies the maximum length of the column of the table.

INSERT RECORDS

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

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


SYNTAX
INSERT INTO <table_name>
VALUES (value1,value2,value3,...);

DISPLAY TABLE
SELECT
The SELECT statement is used to select data from a database. The result is stored in a result
table, called the result-set.
SYNTAX:
SELECT * FROM <table_name>;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

DISPLAY SPECIFIC RECORD


SELECT <column_name>,<column_name> FROM< table_name>;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

 WHERE CLAUSE
The WHERE clause is used to filter records. The WHERE clause is used to extract only those
records that fulfill a specified criterion.

SYNTAX

SELECT< column_name>,<column_name> FROM table_name WHERE <column_name>


operator value;

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

SYNTAX
DELETE FROM< table_name> WHERE some_column=some_value;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

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

SYNTAX

UPDATE <table_name>
SET column1=value1,column2=value2,... WHERE some_column=some_value;

UPDATED TABLE

ORDER BY KEYWORD

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The ORDER BY keyword is used to sort the result-set by one or more columns. The ORDER
BY keyword sorts the records in ascending order by default. To sort the records in a
descending order, you can use the DESC keyword.
SYNTAX
SELECT column_name,column_name FROM table_name ORDER BY 
column_name,column_name   ASC|DESC;

DROP TABLE
The DROP TABLE statement is used to delete a table.
SYNTAX
DROP TABLE <table_name>;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

EXPERIMENT NO 4
AIM: Write SQL commands to add data constraints. (UNIQUE, NOT NULL,
PRIMARY KEY,FOREIGN KEY, CHECK, IN, BETWEEN & AND)
CREATE A TABLE
SYNTAX
create table <table_name>
( <column_name1> data type(size) data constraint,
<column_name2> data type(size) data constraint,
<column_name3> data type(size) data constraint,
.
);

INSERTION OF ROW
SYNTAX
insert into <table_name> values(value 1,value 2, value 3..........);

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

UNIQUE
The UNIQUE constraint uniquely identifies each record in a database table.
SYNTAX
create table <table_name>
(<column_name1> datatype(size) unique,
<column_name2> datatype(size),
);

INSERTION OF ROW VIOLATING UNIQUE CONSTRAINT

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

INSERTION OF ROW VIOLATING NOT NULL CONSTRAINT NOT NULL


The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL
constraint enforces a field to always contain a value. This means that you cannot insert a new
record, or update a record without adding a value to this field.
SYNTAX
create table <table_name>(<column_name1> datatype(size) not null,<column_name2>
datatype(size),);

PRIMARY KEY

The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary
keys must contain unique values. A primary key column cannot contain NULL values. Most
tables should have a primary key, and each table can have only ONE primary key.
SYNTAX
create table <table_name> ( <column_name1> datatype(size) primary key, <column_name2>
datatype(size),);

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

INSERTION OF ROW VIOLATING PRIMARY KEY CONSTRAINT

FOREIGN KEY:-
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
SYNTAX
create table<table_name>(<column_name1> datatype(size) references
<table_name1>(column_name1), <column_name2> datatype(size),);

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

INSERTION OF VALUES

Violation Of Foreign Key Constraint as value ‘10180’ is not there in the table ‘SALES’.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

CHECK

The CHECK constraint is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table, it can limit the values in certain columns based
on values in other columns in the row.
SYNTAX
alter table<table_n-name> add check(column_name>or< some value)

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

IN OPERATOR

The IN operator allows you to specify multiple values in a WHERE clause.


SYNTAX
SELECT <column_name(s)>
FROM <table_name>
WHERE <column_name> IN (value1,value2,...);

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

BETWEEN ... AND OPERATOR


The BETWEEN operator selects values within a range. The values can be numbers, text, or
dates.
SYNTAX
SELECT <column_name(s)>
FROM <table_name>
WHERE <column_name> BETWEEN value1 AND value2;

EXPERIMENT NO 5

AIM: Write SQL commands to implement Group By and HAVING caluse.

CREATE TABLE
SYNTAX
CREATE TABLE< table_name>(<column_name1> data_type(size),<column_name2>
data_type(size),<column_name3>data_type(size),....);

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

INSERT RECORDS
SYNTAX
INSERT INTO <table_name> VALUES (value1,value2,value3,...);

DISPLAY TABLE
SYNTAX:
SELECT * FROM <table_name>;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

GROUP BY CLAUSE:
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups. The GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause.

SYNTAX
SELECT column1, column2
FROM table_name
WHERE [ conditions ] GROUP BY column1, column2;

HAVING CLAUSE:
The HAVING clause enables you to specify conditions that filter which group results appear
in the final results. The WHERE clause places conditions on the selected columns, whereas
the HAVING clause places conditions on groups created by the GROUP BY clause.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

SYNTAX:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

EXPERIMENT NO. 6
AIM: Write commands to implement various SQL operators
1. Arithmetic
2. Comparison
3. Logical
4. String
5. Aggregate

STEP 1: CREATE TABLE

STEP 2: INSERT VALUES INTO TABLE

STEP 3: EXPERIENCE THE USE OF DIFFERENT TYPES OF OPERATORS


1. SQL ARITHMETIC OPERATORS

Arithmetic operators can perform arithmetical operations on numeric operands involved.


Arithmetic operators are addition(+), subtraction(-), multiplication(*) and division(/). The +
and - operators can also be used in date arithmetic.

SQL ADDITION OPERATOR


The SQL plus (+) operator is used to add two or more expressions or numbers.

SQL SUBTRACTION (-) OPEARTOR

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The SQL minus (-) operator is used to subtract one expression or number from another
expression or number.
SQL MULTIPLICATION (*) OPERATOR
The SQL multiply ( * ) operator is used to multiply two or more expressions or numbers.
SQL DIVIDE (/) OPERATOR
The SQL divide ( / ) operator is used to divide one expressions or numbers by another.

2. SQL LOGICAL OPERATORS


There are three Logical Operators namely, AND, OR, and NOT. These operators compare two
conditions at a time to determine whether a row can be selected for the output. When
retrieving data using a SELECT statement, you can use logical operators in the WHERE
clause, which allows you to combine more than one condition.

OR OPERATOR
For the row to be selected at least one of the conditions must be true.

AND OPERATOR
For a row to be selected all the specified conditions must be true.

NOT OPERATOR

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

For a row to be selected the specified condition must be false.


SYNTAX:
select <column name> from <table name>
where
<column name>=Value (OR/AND/NOT) <column name>= Value.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

3. SQL COMPARISON OPERATORS


Comparison operators are used to compare the column data with specific values in a
condition. Comparison Operators are also used along with the SELECT statement to filter
data based on specific conditions.

= OPERATOR
Checks if the values of two operands are equal or not, if yes then condition becomes true.

!= OPERATOR
Checks if the values of two operands are equal or not, if values are not equal then condition
becomes true.

< OPERATOR
Checks if the value of left operand is less than the value of right operand, if yes then condition
becomes true.
> OPERATORChecks if the value of left operand is greater than the value of right operand,
if yes then condition becomes true.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

<= OPERATOR
Checks if the value of left operand is less than or equal to the value of right operand, if yes
then condition becomes true.
SYNTAX: select <column name> from <table name> where <column name> (>/</=/!
=/<=/>=) value.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

4. SQL STRING OPERATORS

SQL string functions are used primarily for string manipulation.

• UPPER(): Convert the argument to uppercase


• LOWER(): Return the argument in lowercase
• ASCII(): Return numeric value of left-most character
• SUBSTRING(),SUBSTR(): Return the substring as specified
• LENGTH(): Return the length of a string in bytes
• CONCAT(): Return concatenated string
• TRIM(): Remove leading and trailing spaces
• LTRIM(): Removes leading space
• RTRIM(): Removes trailing space
SYNTAX: select string_operator(column name) from <table name>.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

1. SQL AGGREGATE OPERATORS


SQL aggregate functions return a single value, calculated from values in a column. Useful
aggregate functions:
• AVG (): Returns the average value
• COUNT (): Returns the number of rows
• FIRST () : Returns the first value
• LAST (): Returns the last value
• MAX (): Returns the largest value
• MIN () : Returns the smallest value
• SUM () : Returns the sum
• DISTINCT (): Filters the result set to remove duplicates
SYNTAX: select aggregate_function(column name) from <table name>.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

EXPRIMENT NO. 7

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

AIM : Set operations in SQL


1. Union
2. Union all
3. Intersection
4. Minus

CREATE TABLE

DISPLAY TABLE

CREATE ANOTHER TABLE

INSERTION OF ROWS

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

DISPLAY TABLE

1. SQL UNION OPERATOR


The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within the UNION must have the same number of columns. The
columns must also have similar data types. Also, the columns in each SELECT statement
must be in the same order.The column names in the result-set of a UNION are usually equal
to the column names in the first SELECT statement in the UNION.
SQL UNION SYNTAX
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

2. SQL UNION ALL OPERATOR

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The UNION operator selects only distinct values by default. To allow duplicate values, use
the ALL keyword with UNION.
SQL UNION ALL SYNTAX
SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM
table2;

3. SQLMINUS OPERATOR
The SQL MINUS operator is used to return all rows in the first SELECT statement that are
not returned in the second SELECT statement. Each SELECT statement within the MINUS
query must have the same number of fields in the result sets with similar data types.
SQL MINUS SYNTAX
The syntax for the SQL MINUS operator is:
SELECT expression1, expression2, ...expression_n FROM tables
MINUS SELECT expression1, expression2, ...expression_n FROM tables;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

4. Intersection in SQL
The SQL INTERSECT clause/operator is used to combine two SELECT statements, but
returns rows only from the first SELECT statement that are identical to a row in the second
SELECT statement. This means INTERSECT returns only common rows returned by the two
SELECT statements. Just as with the UNION operator, the same rules apply when using the
INTERSECT operator. MySQL does not support INTERSECT operator

Syntax:
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

INTERSECT

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]

EXPERIMENT NO. 8

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

AIM: To create view and index in SQL.

CREATE TABLE

INSERTION OF ROWS IN FACULTY_REC TABLE

DISPLAY TABLE

SQL VIEW

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

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.
SQL CREATE VIEW SYNTAX
CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE
condition
VIEW V1 CREATED

DISPLAY VIEW V1

INSERT VALUES IN VIEW V1

DISPLAY VIEW V1 AFTER INSERTION OF VALUES

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

DELETE VALUES FROM VIEW V1

DISPLAY VIEW V1 AFTER DELETION

SQL UPDATE VIEW

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The SQL UPDATE VIEW command can be used to modify the data of a view.All views are
not updatable. So, UPDATE command is not applicable to all views. An updatable view is
one which allows performing an UPDATE command on itself without affecting any other
table.

SQL UPDATE VIEW SYNTAX


UPDATE < view_name > SET<column1>=<value1>,<column2>=<value2>,.WHERE
<condition>;

UPADTE VIEW V1

DISPLAY UPDATED VIEW V1

SQL CREATE INDEX STATEMENT
The CREATE INDEX statement is used to create indexes in tables. Indexes allow the
database application to find data fast; without reading the whole table.

SQL CREATE INDEX SYNTAX


CREATE INDEX index_name
ON
table_name (column_name)

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

CREATE INDEX ICON

SELECTS COMMANDS

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

SQL DROP INDEX


The DROP INDEX statement is used to delete an index in a table.

DROP INDEX SYNTAX


DROP INDEX index_name

DROP INDEX ICON

EXPERIMENT NO. 9

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

AIM: To write sub-queries in SQL.


THEORY:
1. A subquery is a SQL query nested inside a larger query.
2. A subquery may occur in:
- A SELECT clause
- A FROM clause
- A WHERE clause
3. The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE
statement or inside another subquery.
4. A subquery is usually added within the WHERE Clause of another SQL SELECT
statement.
5. You can use the comparison operators, such as >, <, or =. The comparison operator can
also be a multiple-row operator, such as IN, ANY, or ALL.
6. A subquery can be treated as an inner query, which is a SQL query placed as a part of
another query called as outer query.
7. The inner query executes first before its parent query so that the results of inner query
can be passed to the outer query.

CREATE TABLE (ITEM MOBILES CREATED)

INSERTION OF ROWS IN TABLE MOBILES

DISPLAY TABLE MOBILES

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

CREATE ANOTHER TABLE (SHOP TABLE CRATED)

INSERTION OF ROWS IN SHOP TABLE

DISPLAY TABLE SHOP

1. SUB QUERIES WITH SELECT STATEMENT


Sub queries are most frequently used with the SELECT statement.

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

SYNTAX
SELECT column_name [, column_name ] FROM table1 [, table2 ] WHERE column_name
OPERATOR (SELECT column_name [, column_name ] FROM table1 [, table2 ] [WHERE])

2. SUB QUERIES WITH INSERT STATEMENT

Sub queries also can be used with INSERT statements. The INSERT statement uses the data
returned from the sub query to insert into another table. The selected data in the sub query can
be modified with any of the character, date or number functions.

SYNTAX
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ];

DISPLAY TABLE AFTER INSERTION

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

3. SUB QUERIES WITH UPDATE STATEMENT


The sub query can be used in conjunction with the UPDATE statement. Either single or
multiple columns in a table can be updated when using a sub query with the UPDATE
statement.

SYNTAX
UPDATE table SET column_name = new_value [ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ]

DISPLAY UPDATED TABLE

4. SUB QUERIES WITH DELETE STATEMENT

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

The subquery can be used in conjunction with the DELETE statement like with any other
statements mentioned above.

The basic syntax is as follows:


DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]

DISPLAY TABLE AFTER DELETION

EXPERIMENT NO. 10

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

AIM: to implement various joins in SQL

CREATE TABLE LAPTOP

INSERT VALUES INTO THE TABLE LAPTOP

DISPLAY TABLE LAPTOP

CREATE TABLE SOFTWARE

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

INSERT VALUES INTO THE TABLE SOFTWARE

DISPLAY TABLE SOFTWARE

1. CROSS JOIN TABLES LAPTOP AND SOFTWARE


SQL CROSS JOIN will return all records where each row from the first table is combined
with each row from the second table. Which also mean CROSS JOIN returns the Cartesian
product of the sets of rows from the joined tables.
SYNTAX:
SELECT * FROM [TABLE1] CROSS JOIN [TABLE2]

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

2. NATURAL JOIN TABLES LAPTOP AND SOFTWARE


The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that,
columns with same name of associate tables will appear once only.
SYNTAX
Select * FROM table1 NATURAL JOIN table2;

3. INNER JOIN
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
SYNTAX:
SELECT column_name(s) FROM table1
INNER JOIN table2 ON table1.column_name=table2.column_name;

4. OUTER JOIN

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

A. LEFT OUTER JOIN


The LEFT JOIN keyword returns all rows from the left table (table1), with the
matching rows in the right table (table2). The result is NULL in the right side
when there is no match.
SYNTAX:
SELECT * FROM[TABLE1] LEFT OUTER JOIN [TABLE2]
ON TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME

B. RIGHT OUTER JOIN


The RIGHT JOIN keyword returns all rows from the RIGHT table (table1), with
the matching rows in the LEFT table (table2). The result is NULL in the right side
when there is no match.
SYNTAX:
SELECT * FROM[TABLE1] RIGHT OUTER JOIN [TABLE2]
ON TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME

C. FULL OUTER JOIN


The FULL OUTER JOIN keyword returns all rows from the left table (table1)
and from the right table (table2). The FULL OUTER JOIN keyword combines
the result of both LEFT and RIGHT joins.
SYNTAX:
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Your Name Roll no 2


NORTHERN INDIA ENGINEERING COLLEGE DEPARTMENT OF ECE

Your Name Roll no 2

S-ar putea să vă placă și