Professional Documents
Culture Documents
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for
an attribute.
o The data type of domain includes string, character, integer, time, date, currency,
etc. The value of the attribute must be available in the corresponding domain.
Example:
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
o A table can contain a null value other than the primary key field.
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set
uniquely.
o An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the relational
table.
Example:
65.3M
1.2K
Difference between JDK, JRE, and JVM
1, ENFORCING INTEGRITY CONSTRAINTS
ENROLLED LIST WITH STUDENT TABLE
4, INTRODUCTION TO VIEWS
CREATE VIEW TABLE NAME (COLA)
AS SELECT COLA WHERE COLA>1
CREATE VIEW TABLE NAME (COLA,COLB)
AS SELECT COLA,COLB WHERE COLA>COLB
Every potential Ie violation is generally checked at the end of each SQL statement execution,
although it can be deferred until the end of the transaction executing the statement, as we will see
in Section 3.3.1. Consider the instance 51 of Students shown in Figure 3.1.
The following insertion violates the primary key constraint because there is already a tuple with the
s'id 53688, and it will be rejected by the DBMS:
The following insertion violates the constraint that the primary key cannot contain null:
Of course, a similar problem arises whenever we try to insert a tuple with a value in a field that is
not in the domain associated with that field, that is, whenever we violate a domain constraint.
Deletion does not cause a violation of clornain, primary key or unique constraints. However, an
update can cause violations, sirnilar to an insertion:
UPDATE Students S
WHERE S.sid = 53688 CHAPTER, 3 This update violates the primary key constraint because there is
already a tuple with sid 50000.
The impact of foreign key constraints is more complex because SQL sometimes tries to rectify a
foreign key constraint violation instead of simply rejecting the change. We discuss the referential
integrity enforcement steps taken by the DBMS in terms of our Enrolled and Students tables, with
the foreign key constraint that Enrolled. sid is a reference to (the primary key of) Students.
In addition to the instance 81 of Students, consider the instance of Enrolled shown in Figure 3.4.
Deletions of Enrolled tuples do not violate referential integrity, but insertions of Enrolled tuples
could.
The following insertion is illegal because there is no Students tuple with sid 51111:
INSERT
SQL is the most popular commercial query language for a relational DBMS.
We now present some SQL examples that illustrate how easily relations can be queried. Consider
the instance of the Students relation shown in Figure 3.1.
We can retrieve rows corresponding to students who are younger than 18 with the following SQL
query:
SELECT *
FROM Students S
The symbol ,*, means that we retain all fields of selected tuples in the result. Think of S as a variable
that takes on the value of each tuple in Students, one tuple after the other. The condition S. age < 18
in the WHERE clause specifies that we want to select only tuples in which the age field has a value
less than 18. This query evaluates to the relation
This example illustrates that the domain of a field restricts the operations that are permitted on field
values, in addition to restricting the values that can appear in the field. The condition S. age < 18
involves an arithmetic comparison of an age value with an integer and is permissible because the
domain of age is the set of integers. On the other hand, a condition such as S.age = S."id does not
make sense because it compares an integer value with a string value, and this comparison is defined
to fail in SQL; a query containing this condition produces no answer tuples.
fields of each selected tuple. vVe can compute the names and logins of students
FROM Students S
WHERE S.age < 18
Figure 3.7 shows the answer to this query; it is obtained by applying the selection to the instance 81
of Students (to get the relation shown in Figure
3.6), followed by removing unwanted fields. Note that the order in which we
cannot check the condition S. age < 18, which involves one of those fields.
want to obtain the names of all students who obtained an A and the id of the
This query can be understood as follows: "If there is a Students tuple Sand
an Enrolled tuple E such that S.sid = E.studid (so that S describes the student
who is enrolled in E) and E.grade = 'A', then print the student's name and
the course id." When evaluated on the instances of Students and Enrolled in
capture all the constraints implicit in the ER design using SQL, unless we use
certain SQL constraints that are costly to check.) We now describe how to
the ER design. (The translation is approximate to the extent that we cannot capture all the
constraints implicit in the ER design using SQL, unless we use certain SQL constraints that are costly
to check.) We now describe how to translate an ER diagram into a collection of tables with
associated constraints, that is, a relational database schema. 3.5.1 Entity Sets to Tables An entity set
is mapped to a relation in a straightforward way: Each attribute of the entity set becomes an
attribute of the table. Note that we know both the domain of each attribute and the (primary) key of
an entity set. Consider the Employees entity set with attributes ssn, name, and lot shown in Figure
3.8. A possible instance of the Employees entity set, containing three
INTRODUCTION TO VIEWS
Creating view
A view can be created using the CREATE VIEW statement. We can create a view from
a single table or multiple tables.
Query:
Just like table query, we can query the view to view the data.
1. SELECT * FROM DetailsView;
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named Marks View from two tables
Student_Detail and Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
Example:
+---------+-------------+-----------+-----------+------------+
| PetId | PetTypeId | OwnerId | PetName | DOB |
|---------+-------------+-----------+-----------+------------|
| 1 | 2 | 3 | Fluffy | 2020-11-20 |
| 2 | 3 | 3 | Fetch | 2019-08-16 |
| 3 | 2 | 2 | Scratch | 2018-10-01 |
| 4 | 3 | 3 | Wag | 2020-03-15 |
| 5 | 1 | 1 | Tweet | 2020-11-28 |
| 6 | 3 | 4 | Fluffy | 2020-09-17 |
| 7 | 3 | 2 | Bark | NULL |
| 8 | 2 | 4 | Meow | NULL |
+---------+-------------+-----------+-----------+------------+
This query selects all rows and all columns from the Pets table. This is
because the asterisk (*) wildcard selects all columns.
Here’s an example.
SELECT PetId, PetName
FROM Pets;
Result:
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 1 | Fluffy |
| 2 | Fetch |
| 3 | Scratch |
| 4 | Wag |
| 5 | Tweet |
| 6 | Fluffy |
| 7 | Bark |
| 8 | Meow |
+---------+-----------+
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 1 | Fluffy |
| 6 | Fluffy |
+---------+-----------+
Here’s another example of filtering the results. This time we use the
greater than operator (>) to filter it by date.
SELECT PetName, DOB
FROM Pets
WHERE DOB > '2020-01-01';
Result:
+-----------+------------+
| PetName | DOB |
|-----------+------------|
| Fluffy | 2020-11-20 |
| Wag | 2020-03-15 |
| Tweet | 2020-11-28 |
| Fluffy | 2020-09-17 |
+-----------+------------+
You can swap the greater than operator to other operators, such as
the greater than or equals to operator (>=), less than operator (<), or
less than or equals to operator (<=).
You can also use the BETWEEN operator to filter the results to a specific
range (e.g. between two dates).
SELECT
PetName,
DOB
FROM Pets
WHERE DOB BETWEEN '2018-01-01' AND '2020-01-01';
Result:
+-----------+------------+
| PetName | DOB |
|-----------+------------|
| Fetch | 2019-08-16 |
| Scratch | 2018-10-01 |
+-----------+------------+
Ascending Order
Use the ASC keyword to sort the results in ascending order. This is the
default value, so you can also omit this keyword if you want the
results in ascending order.
SELECT PetId, PetName
FROM Pets
ORDER BY PetName ASC;
Or:
SELECT PetId, PetName
FROM Pets
ORDER BY PetName;
Result:
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 7 | Bark |
| 2 | Fetch |
| 1 | Fluffy |
| 6 | Fluffy |
| 8 | Meow |
| 3 | Scratch |
| 5 | Tweet |
| 4 | Wag |
+---------+-----------+
Descending Order
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 4 | Wag |
| 5 | Tweet |
| 3 | Scratch |
| 8 | Meow |
| 1 | Fluffy |
| 6 | Fluffy |
| 2 | Fetch |
| 7 | Bark |
+---------+-----------+
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 7 | Bark |
| 2 | Fetch |
| 1 | Fluffy |
| 6 | Fluffy |
| 8 | Meow |
| 3 | Scratch |
| 5 | Tweet |
| 4 | Wag |
+---------+-----------+
(8 rows affected)
+---------+-----------+
| PetId | PetName |
|---------+-----------|
| 7 | Bark |
| 2 | Fetch |
| 6 | Fluffy |
| 1 | Fluffy |
| 8 | Meow |
| 3 | Scratch |
| 5 | Tweet |
| 4 | Wag |
+---------+-----------+
(8 rows affected)
We can see that the two Fluffys are a different order in each result
(we can tell by looking at their PetId values). This is because
the PetName column was sorted first, then the PetId sorted any
duplicates from the first sorting.
IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’
or ‘DBMS’, we can write it with the help of independent nested
query and IN operator. From COURSE table, we can find
out C_ID for C_NAME ‘DSA’ or DBMS’ and we can use
these C_IDs for finding S_IDs from STUDENT_COURSE TABLE.
The inner query will return a set with members C1 and C3 and
outer query will return those S_IDs for which C_ID is equal to any
member of set (C1 and C3 in this case). So, it will return S1, S2 and
S4.
The innermost query will return a set with members C1 and C3.
Second inner query will return those S_IDs for which C_ID is equal
to any member of set (C1 and C3 in this case) which are S1, S2
and S4. The outermost query will return those S_IDs where S_ID is
not a member of set (S1, S2 and S4). So it will return S3.
CORRELATED DELETE :
DELETE FROM table1 alias1
WHERE column1 operator
(SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
Use a correlated subquery to delete rows in one table based on the rows
from another table.
SQL Join
(Inner, Left, Right and Full Joins)
SQL Join
statement is used to combine data or rows from two or more tables based
on a common field between them. Different types of Joins are as follows:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
Studen t Course
B. LEFT JOIN
This join returns all the rows of the table on the left side of the join and matches
rows for the table on the right side of the join. For the rows for which there is no
matching row on the right side, the result-set will contain null. LEFT JOIN is also
known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
same.
Example Queries(RIGHT JOIN):
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
D. FULL JOIN
FULL JOIN creates the result-set by combining results of both LEFT JOIN and
RIGHT JOIN. The result-set will contain all the rows from both tables. For the
rows for which there is no matching, the result-set will contain NULL values.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Example Queries(FULL JOIN):
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NAME COURSE_ID
NULL 4
Syntax
The basic syntax of the AND operator with a WHERE clause is as follows −
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
You can combine N number of conditions using the AND operator
IS NULL operator
All operations upon null values present in the table must be done using this ‘is null’
operator .we cannot compare null value using the assignment operator
Example
select * from emp
where comm is null
out put:……………………..
IS NOT NULL
Once not null is applied to a particular column, you cannot enter null values to that
column and restricted to maintain only some proper value other than null
Example
Using NVL function you can substitute a value in the place of NULL values.
The substituted value then temporarily replaces the NULL values in your calculations or
expression. Remember that the substituted value only replaces the NULL value
temporarily for the session and does not affect the value stored in the table.
parameter exp can be a column name of a table or an arithmetic expression and the second
parameter replacement expression will be the value which you want to substitute when a
Always remember the data type of both the parameters must match otherwise the compiler
Example
Similarly we can use NVL null function while performing arithmetic expression.
Again let’s take the same arithmetic expression which we used in the previous query where