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

Name: Vaishnavi Kailas Raut Seat No.: S190088621 Group B Assignment No: 1

The document discusses creating an entity relationship diagram for a student database with entities like student, instructor, course, department and relationships like a student studying in a department or a instructor teaching a course. It provides examples and explanations of the different components of an ER diagram like entities, attributes, relationships and participation constraints. Finally, an ER diagram is drawn showing the entities, attributes and relationships for a database application tracking students, instructors, courses and departments.

Uploaded by

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

Name: Vaishnavi Kailas Raut Seat No.: S190088621 Group B Assignment No: 1

The document discusses creating an entity relationship diagram for a student database with entities like student, instructor, course, department and relationships like a student studying in a department or a instructor teaching a course. It provides examples and explanations of the different components of an ER diagram like entities, attributes, relationships and participation constraints. Finally, an ER diagram is drawn showing the entities, attributes and relationships for a database application tracking students, instructors, courses and departments.

Uploaded by

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

Name: Vaishnavi Kailas Raut

Seat no. : S190088621

Group B

Assignment No: 1

Aim: Design any database with at least 3 entities and relationships between them. Draw
suitable ER/EER diagram for the system.

Entity Relationship Diagram:

An Entity–relationship model (ER model) describes the structure of a database with the help
of a diagram, which is known as Entity Relationship Diagram (ER Diagram). An ER model is
a design or blueprint of a database that can later be implemented as a database. The main
components of E-R model are: entity set and relationship set.

What is an Entity Relationship Diagram (ER Diagram)?

An ER diagram shows the relationship among entity sets. An entity set is a group of similar
entities and these entities can have attributes. In terms of DBMS, an entity is a table or
attribute of a table in database, so by showing relationship among tables and their attributes,
ER diagram shows the complete logical structure of a database. Lets have a look at a simple
ER diagram to understand this concept.

A simple ER Diagram:

In the following diagram we have two entities ATM and Transactions and their relationship.
The relationship between ATM and Transactions is weak relation. The attributes for ATM
entities are ATM id, Address and Time. For transaction entities are Trans no, Amount and
type.

ER Diagram
Geometric shapes and their meaning:

Rectangle: Represents Entity sets.


Ellipses: Attributes
Diamonds: Relationship Set
Lines: They link attributes to Entity Sets and Entity sets to Relationship Set
Double Ellipses: Multivalued Attributes
Dashed Ellipses: Derived Attributes
Double Rectangles: Weak Entity Sets
Double Lines: Total participation of an entity in a relationship set.

Components of a ER Diagram

An ER diagram has three main components:


1. Entity
2. Attribute
3. Relationship

Entity

An entity is an object or component of data. An entity is represented as rectangle in an ER


diagram.

// give your entity example here with diagram (List all


entities)
student instructor
• student
• instructor
• subject Study
• course subject in department
• department

course

WeakEntity:
An entity that cannot be uniquely identified by its own attributes and relies on the
relationship with other entity is called weak entity. The weak entity is represented by a
double rectangle.

// give your weak entity example here with diagram


subject course

Attribute

An attribute describes the property of an entity. An attribute is represented as Oval in an ER


diagram. There are four types of attributes:

1. Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute

1. Key attribute:

A key attribute can uniquely identify an entity from an entity set. For example, student roll
number can uniquely identify a student from a set of students

Key attribute is represented by oval same as other attributes however the text of key
attribute is underlined.

// give your key attribute example here with diagram


stud_id
• stud_id is a key attribute

name
student

address

mobile

2. Composite attribute:

An attribute that is a combination of other attributes is known as composite attribute. For


example, In student entity, the student address is a composite attribute as an address is
composed of other attributes such as pin code, state, country.

// give your composit attribute example here with diagram

pin
• Address is composite attribute

student address state

country

3. Multivalued attribute:

An attribute that can hold multiple values is known as multivalued attribute. It is represented
with double ovals in an ER Diagram. For example – A person can have more than one phone
numbers so the phone number attribute is multivalued.

// give your Multivalued attribute example here with


diagram
id

name

student

address

mobile

4. Derived attribute:

A derived attribute is one whose value is dynamic and derived from another attribute. It is
represented by dashed oval in an ER Diagram. For example – Person age is a derived
attribute as it changes over time and can be derived from another

// give your derived attribute example here with diagram

• age is derived attribute


id
name

student

DOB

mobile
age

Relationship

A relationship is represented by diamond shape in ER diagram, it shows the relationship


among entities. There are four types of relationships/cardinality ratio:
1. One to One
2. One to Many
3. Many to One
4. Many to Many

1. One to One Relationship

When a single instance of an entity is associated with a single instance of another entity then
it is called one to one relationship. For example, a person has only one passport and a
passport is given to one person.

// give your example here

student has course

2. One to Many Relationship

When a single instance of an entity is associated with more than one instances of another
entity then it is called one to many relationship. For example – a customer can place many
orders but a order cannot be placed by many customers.

// give your example here give your example here

student Has subjects

3. Many to One Relationship


When more than one instances of an entity is associated with a single instance of another
entity then it is called many to one relationship. For example – many students can study in a
single college but a student cannot study in many colleges at the same time.

// give your example here

student study college

4. Many to Many Relationship

When more than one instances of an entity is associated with more than one instances of
another entity then it is called many to many relationship. For example, a can be assigned to
many projects and a project can be assigned to many students.

// give your example here

courses has subjects

Participation Constraints
• Total Participation − Each entity is involved in the relationship. Total participation
is represented by double lines.
• Partial participation − Not all entities are involved in the relationship. Partial
participation is represented by single lines.

// give your example here for participation

subject Study college


in
How to Create an Entity Relationship Diagram (ERD)

Following are the steps to create an ER Diagram:

Step 1) Entity Identification

Step 2) Relationship Identification

Step 3): Cardinality Identification:

Step 4) Identify Attributes

// E-R Diagram for Your database application


inst_name
ee
mobile
inst_id

Instructor
stu_id

name
stu_name

Study
Student Department
In

address

location
mobile
Course

name depart_name

Subject
inst_id

sub_name sub_id

Conclusion: We have studied and created a database with at least 3 entities and relationships
between them with creation of ER/EER diagram for the system
Assignment No: 2

Aim:- Design and implement a database (for assignment no 1) using DDL statements
and apply normalization on them

RDBMS:
A relational database refers to a database that stores data in a structured format,
using rows and columns. This makes it easy to locate and access specific values within the
database. It is "relational" because the values within each table are related to each other.
Tables may also be related to other tables. The relational structure makes it possible to
run queries across multiple tables at once.

RDBMS Concepts:
Before we proceed to explain MySQL database system, let's revise few definitions related to
database.
• Database: A database is a collection of tables, with related data.
• Table: A table is a matrix with data. A table in a database looks like a simple spread
sheet.
• Column: One column (data element) contains data of one and the same kind, for
example the Column post code.
• Row: A row (tuple, entry or record) is a group of related data, for example the data
of one subscription.
• Redundancy: Storing data twice, redundantly to make the system faster.
• Primary Key: A primary key is unique. A key value cannot occur twice in one table.
With a key, you can find at most one row.
• Foreign Key: A foreign key is the linking pin between two tables.
• Compound Key: A compound key(composite key) is a key that consists of multiple
columns, Because one column is not sufficiently unique.

Structured Query Language(SQL):


• SQL is a database computer language designed for the retrieval and management of
data in a relational database. SQL stands for Structured Query Language.
• SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in a relational database.
• SQL is the standard language for Relational Database System. All the Relational
Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their standard database language.
• SQL uses certain commands like Create, Drop, Insert, etc. to carry out the required
tasks.
These SQL commands are mainly categorized into four categories as:
• DDL – Data Definition Language
• DQl – Data Query Language
• DML – Data Manipulation Language
• DCL – Data Control Language

Data Definition Language:


Data Definition Language helps you to define the database structure or schema. Let's learn
about DDL commands with syntax.
Five types of DDL commands in SQL are:
• CREATE
• INSERT
• UPDATE
• ALTER
• DROP
• DELETE

CREATE:
Create statements is used to define the database structure schema:
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES [,....]);
Example:
CREATE TABLE EMPLOYEE
(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

INSERT:
The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME
(column1, column2, column3 ,.... columnN)
VALUES (value1, value2, value3 , .... valueN);
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3 , .... valueN);
Example:
INSERT INTO employee (Name, Salary) VALUES ("Meghana", 20000);

UPDATE:
This command is use d to update or modify the value of a column in the table.
Syntax:
UPDATE table_name SET [column_name1=value1,…, column_nameN=valueN]
[WHERE CONDITION]
Example:

UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'

ALTER:
Syntax to add new column:
ALTER TABLE table_name ADD column_name datatype;
Syntax to drop column:
ALTER TABLE table_name DROP COLUMN column_name;
Syntax to change datatype:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;

Example:
ALTER TABLE CUSTOMERS ADD Address Varchar(20);
ALTER TABLE CUSTOMERS DROP Address;

DROP
It is used to delete both the structure and record stored in the table.

Syntax:

DROP TABLE ;

Example:

DROP TABLE EMPLOYEE;


DELETE
This command is used to remove one or more rows from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
Example:

DELETE FROM EMPLOYEE


WHERE EMP_NAME = 'Kristen';

Normalization:
• Normalization is the process of organizing the data in the database.
• Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
• Normalization divides the larger table into the smaller table and links them using
relationship.
• The normal form is used to reduce redundancy from the database table.

Types of Normalization:
First Normal Form(1NF):
A relation is in 1NF if it contains an atomic value.
An attribute (column) of a table cannot hold multiple values. It should hold only
atomic values.

// Give your database example here for 1N

Student Name Dob Age Maths Science Lang Maths Science Lang total Book Date of
no. Unit1 Unit1 Unit1 Unit2 Unit2 Unit2 issued return
1 Amruta 1/05/2002 19 59 88 71 79 79 57 433 2/06/2020 4/08/2020

2 Aditi 7/04/2002 19 69 79 58 45 69 84 404 3/09/2020 9/10/2020

3 Tina 1/01/2003 18 67 66 60 75 91 67 426 31/08/2020 9/09/2020


4 Piya 24/11/2002 18 83 79 80 67 40 46 395 27/10/2020 18/11/2020

5 Arya 08/12/2002 18 91 77 86 81 90 91 516 09/07/2020 20/08/2020

Second Normal Form(2NF):

A table is said to be in 2NF if both the following conditions hold:

• Table is in 1NF (First normal form)


• No non-prime attribute is dependent on the proper subset of any candidate key of
table.

An attribute that is not part of any candidate key is known as non-prime attribute.

// Give your database example here for 2NF


STUDENT INFORMATION TABLE-

Student Name Dob Age


no.
1 Amruta 1/05/2002 19
2 Aditi 7/04/2002 19
3 Tina 1/01/2003 18
4 Piya 24/11/2002 18
5 Arya 08/12/2002 18

MARKS OBTAINES BY STUDENTS-


Student Maths Science Lang Maths Science Lang Total
no Unit1 Unit2 Unit1 Unit2 Unit2 Unit2
1 59 88 71 79 79 57 433
2 69 79 58 45 69 84 404
3 67 66 60 75 91 67 426
4 83 79 80 67 40 46 395
5 91 77 86 81 90 91 516

BOOK DETAILS-
Student no. Book Date of return
issued
1 2/06/2020 4/08/2020
2 3/09/2020 9/10/2020
3 31/08/2020 9/09/2020
4 27/10/2020 18/11/2020
5 09/07/2020 20/08/2020

Third Normal Form(3NF):


A table design is said to be in 3NF if both the following conditions hold:

• Table must be in 2NF


• Transitive functional dependency of non-prime attribute on any super key should be
removed.

An attribute that is not part of any candidate key is known as non-prime attribute.

// Give your database example here for 3NF


STUDENT INFORMATION TABLE-

Student Name Dob


no.
1 Amruta 1/05/2002
2 Aditi 7/04/2002
3 Tina 1/01/2003
4 Piya 24/11/2002
5 Arya 08/12/2002
Boyce Codd normal form (BCNF)
• It is an advance version of 3NF that’s why it is also referred as 3.5NF.
• BCNF is stricter than 3NF.
• A table complies with BCNF if it is in 3NF and for every functional dependency X-
>Y, X should be the super key of the table.

// Give your database example here for BCNF

Student Name Dob


no.
1 Amruta 1/05/2002
2 Aditi 7/04/2002
3 Tina 1/01/2003
4 Piya 24/11/2002
5 Arya 08/12/2002

// Output Screenshot of all DDL Queries in MySQL.


Conclusion: In this way we have implemented and created database using DDL Statements
with the normalization technique to bring the database into normalized state.

Assignment No. 3

Aim: Create Table with primary key and foreign key constraints.

a. Alter table with add n modify b. Drop table

Primary key:

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of
single or multiple columns (fields).
CREATE TABLE Persons (
Person_ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY


KEY constraint on multiple columns, use the following SQL syntax:

CREATE TABLE Persons (


Person_ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (Person_ID,LastName)
);

To create a PRIMARY KEY constraint on the "ID" column when the table is already created,
use the following SQL:

ALTER TABLE Persons


ADD PRIMARY KEY (ID);

To drop a PRIMARY KEY constraint, use the following SQL:

ALTER TABLE Persons


DROP PRIMARY KEY;
or
ALTER TABLE Persons
DROP CONSTRAINT PK_Person;

Foreign Key:

• The Foreign key constraint is used to prevent actions that would destroy links
between tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
• The table with the foreign key is called the child table, and the table with the primary
key is called the referenced or parent table.
• Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
• The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
• The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.

The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the parent table.

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(Person_ID)
);

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN


KEY constraint on multiple columns, use the following SQL syntax:

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(Person_ID)
);

To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is
already created, use the following SQL:

ALTER TABLE Orders


ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

To drop a FOREIGN KEY constraint, use the following SQL:

ALTER TABLE Orders


DROP FOREIGN KEY FK_PersonOrder;

// Screenshot of all your sql queries having primary and


foreign key constraint by using all above syntax
Conclusion: In this assignment, we have successfully created table with primary key and
foreign key constraints.

Assignment No: 4
Aim: Perform following SQL queries on the database created in assignment 1.
• Implementation of relational operators in SQL
• Boolean operators and pattern matching
• Arithmetic operations and built in functions
• Group functions
• Processing Date and Time functions
• Complex queries and set operators

Operator in SQL:

SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
An operator is a reserved word or a character used primarily in an SQL statement's WHERE
clause to perform operation(s), such as comparisons and arithmetic operations. These
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.

Relational Operator:
Relational operators are used for comparing numbers and strings. If a string is compared to a
number, MySQL will try to convert the string to a number.
Relational operators in SQL are as follows:

Operator Called as Example query


= Equals to SELECT * FROM Products WHERE Price =
18;
> Greater than SELECT * FROM Products WHERE Price >
18;
< Less than SELECT * FROM Products WHERE Price <
18;
>= Greater than or equal to SELECT * FROM Products WHERE Price
>= 18;
<= Less than or equal to SELECT * FROM Products WHERE Price
<= 18;
<> Not equal to SELECT * FROM Products WHERE Price
<> 18;

// give any 3 example query for arithematic operator


related to assignment no 1(Screenshot of query with
output)
Boolean Operator:
Following is the list of Boolean operator along with example query:

Operator Example query


AND SELECT c1,c2 FROM tbl1 WHERE c1= x AND c2=y
OR SELECT c1,c2 FROM tbl1 WHERE c1= x OR c2=y
NOT SELECT c1 FROM tbl1 WHERE NOT c1<100
ANY SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
ALL SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
EXISTS SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
IN SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
BETWEEN SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

// give example of any 3 boolean sql query for Boolean


operator (Screenshot of query with output)
Pattern matching:
SQL pattern matching enables you to use _ to match any single character and % to match an
arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-
insensitive by default. Some examples are shown here. Do not use = or <> when you use
SQL patterns.

Example Query- SELECT * FROM tbl1 WHERE name LIKE 'b%';

// give one example of pattern matching (Screenshot of


query and output)
Arithmetic operations and built in functions

Operator Called as Example query


+ Add SELECT 10 + 20;
- Subtract SELECT 10 - 20;
* Multiply SELECT 10 * 20;
/ Divide SELECT 10 /20;
% Modulo SELECT 10 %20;

// give any 2 example query of arithematic operator


Built in Functions:

Numeric Functions:

Function Input Argument Value Returned


ABS ( m ) m = value Absolute value of m
MOD ( m, n ) m = value, n = divisor Remainder of m divided by n
POWER ( m, n ) m = value, n = exponent m raised to the nth power
ROUND ( m [, n m = value, n = number of decimal m rounded to the nth decimal
]) places, default 0 place
TRUNC ( m [, n m = value, n = number of decimal m truncated to the nth decimal
]) places, default 0 place
SQRT ( n ) n = value positive square root of n
EXP ( n ) n = value e raised to the power n
LN ( n ) n>0 natural logarithm of n
base n2 any positive value other than 0
LOG ( n2, n1 ) logarithm of n1, base n2
or 1, n1 any positive value
smallest integer greater than or
CEIL ( n ) n = value
equal to n
greatest integer smaller than or
FLOOR ( n ) n = value
equal to n
-1 if n < 0, 0 if n = 0, and 1 if n >
SIGN ( n ) n = value
0

String Functions:
Function Input Argument Value Returned
First letter of each word is changed to
INITCAP ( s ) s = character string uppercase and all other letters are in lower
case.
LOWER ( s ) s = character string All letters are changed to lowercase.
UPPER ( s ) s = character string All letters are changed to uppercase.
s1 and s2 are character Concatenation of s1 and s2. Equivalent
CONCAT ( s1, s2 )
strings to s1 || s2
s1 and s2 are character Returns s1 right justified and padded left
LPAD ( s1, n [, s2] ) strings and n is an integer with n characters from s2; s2 defaults to
value space.
s1 and s2 are character Returns s1 left justified and padded right
RPAD ( s1, n [, s2] ) strings and n is an integer with n characters from s2; s2 defaults to
value space.
s is a character string Returns s with characters removed up to
LTRIM ( s [, set ] ) and set is a set of the first character not in set; defaults to
characters space
s is a character string Returns s with final characters removed
RTRIM ( s [, set ] ) and set is a set of after the last character not in set; defaults
characters to space
s = character string,
REPLACE ( s, Returns s with every occurrence of
search_s = target string,
search_s [, replace_s search_s in s replaced by replace_s;
replace_s = replacement
]) default removes search_s
string
s = character string, m = Returns a substring from s, beginning in
SUBSTR ( s, m [, n
beginning position, n = position m and n characters long; default
])
number of characters returns to end of s.
LENGTH ( s ) s = character string Returns the number of characters in s.
s1 and s2 are character
Returns the position of the nth occurrence
INSTR ( s1, s2 [, m strings, m = beginning
of s2 in s1, beginning at position m, both
[, n ] ] ) position, n = occurrence
m and n default to 1.
of s2 in s1

// give any 3 example of built in functions.


Group functions

The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

The GROUPBY statement is often used with aggregate functions (


COUNT(), MAX(), MIN(), SUM(), AVG() ) to group the result-set by one or more columns.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

// give any 2 example of group fuctions


Processing Date and Time functions:

Date and time functions are scalar functions that perform an operation on a date and time
input value and returns either a string, numeric, or date and time value.

Function Syntax Description


SYSDATETIME SYSDATETIME() Returns a datetime value that
contains the date and time
CURRENT_TIMESTAMP CURRENT_TIMESTAMP Returns a datetime value that
contains date and time.
DATEPART DATEPART(datepart,date) Returns an integer that
represents the specified datepart
of the specified date.
DAY/MONTH/YEAR DAY(date)/MONTH(date)/ Returns an integer that
YEAR(date) represents the day/month/year
part of the specified date.
DATEDIFF DATEDIFF(datepart, Returns the number of date and
startdate, enddate) time datepart boundaries that are
crossed between two specified
date.
DATEADD DATEADD(datepart, Returns a new datetime value by
number, date) adding an interval to the
specified datepart of the
specified date.

// execute any 3 date and time commands and put here


that query example with output
Complex queries and set operators

Complex queries:

Queries such as find duplicate row, second highest salary, fetch first or last record form table,
display first five rows are complex queries.

// Give two example of complex query here


Set Operator:

Set operators are used to join the results of two (or more) SELECT statements. The SET
operators are UNION, UNION ALL, INTERSECT, and MINUS.

UNION Operation

The UNION operator is used to combine the result-set of two or more SELECT statements.

• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
Syntax:

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

INTERSECT

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 the INTERSECT operator.

Syntax

The basic syntax of INTERSECT is as follows.


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

INTERSECT

SELECT column1 [, column2 ]


FROM table1 [, table2 ]
[WHERE condition]

MINUS

The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is
used to subtract the result set obtained by first SELECT query from the result set obtained
by second SELECT query. In simple words, we can say that MINUS operator will return
only those rows which are unique in only first SELECT query and not those rows which are
common to both first and second SELECT queries.

Basic Syntax:

SELECT column1 , column2 , ... columnN


FROM table_name
WHERE condition
MINUS
SELECT column1 , column2 , ... columnN
FROM table_name
WHERE condition;

// Give 1 example of SET Operator

Conclusion: In this way, we have studied and implemented different SQL operator on sql
query for our database.
Assignment No: 5
Aim: Execute DDL/DML statements which demonstrate the use of views. Update the base
table using its corresponding view. Also consider restrictions on updatable views and perform
view creation from multiple tables.

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 statements and functions to a view and present the data as if the data were
coming from one single table.

A view is created with the CREATE VIEW statement.

CREATE VIEW view_nameAS


SELECT column1,column2, ...
FROM table_name
WHERE condition;

SQL REPLACE VIEW Syntax

REPLACE VIEW view_nameAS


SELECT column1,column2, ...
FROM table_name
WHERE condition;

A view is deleted with the DROP VIEW statement.

DROP VIEW view_name;

Updatable Views
Some views are updatable and references to them can be used to specify tables to be updated
in data change statements. That is, you can use them in statements such
as UPDATE, DELETE, or INSERT to update the contents of the underlying table.

A view is not updatable if it contains any of the following:

1. The SELECT statement which is used to create the view should not include GROUP
BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. The view should be created from a single table. If the view is created using multiple
tables then we will not be allowed to update the view.
• It is not possible to create an index on a view.

• Indexes can be used for views processed using the merge algorithm. However, a view
that is processed with the temptable algorithm is unable to take advantage of indexes on
its underlying tables (although indexes can be used during generation of the temporary
tables).

View creation from multiple tables

To create a View from multiple tables we can simply include multiple tables in the
SELECT statement.

CREATE VIEW v AS
SELECT tbl1.NAME, tbl1.ADDRESS, tbl2.MARKS
FROM tbl1, tbl2
WHERE tbl1.id = tbl2.id;

// Give Your Output Here


Conclusion: In this way we have demonstrated the use of views with view creation from
multiple tables.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy