0% found this document useful (0 votes)
116 views10 pages

DBMS Unit-3 Notes - Student

The relational data model was introduced by Ted Codd in 1970 and uses relations resembling tables as its basic building block. A relation contains tuples (rows) with attributes (columns) that have domains restricting their permissible values. The relational model represents a database as a collection of relations and uses constraints like entity integrity to ensure each tuple can be uniquely identified by its primary key. SQL is the most popular language for interacting with relational databases, allowing users to define schemas, manipulate data, and retrieve information.

Uploaded by

ddddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views10 pages

DBMS Unit-3 Notes - Student

The relational data model was introduced by Ted Codd in 1970 and uses relations resembling tables as its basic building block. A relation contains tuples (rows) with attributes (columns) that have domains restricting their permissible values. The relational model represents a database as a collection of relations and uses constraints like entity integrity to ensure each tuple can be uniquely identified by its primary key. SQL is the most popular language for interacting with relational databases, allowing users to define schemas, manipulate data, and retrieve information.

Uploaded by

ddddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-3

The relational data model was first introduced by Ted Codd of IBM Research in 1970 in a classic
paper (Codd, 1970), and it attracted immediate attention due to its simplicity and mathematical
foundation. The model uses the concept of a mathematical relation—which looks somewhat like
a table of values—as its basic building block, and has its theoretical basis in set theory and first-
order predicate logic.

Relational Model Concepts:

The relational model represents the database as a collection of relations. Informally, each relation
resembles a table of values. When a relation is thought of as a table of values, each row in the
table represents a collection of related data values. In the formal relational model terminology, a
row is called a tuple, a column header is called an attribute, and the table is called a relation.

Domain: For each attribute, there is a set of permitted values called the domain of that attribute.
A domain D is a set of atomic values. By atomic we mean that each value in the domain is
indivisible as far as the formal relational model is concerned. A common method of specifying a
domain is to specify a data type from which the data values forming the domain are drawn.

Attributes: Each table column represents an attribute and each column has a distinct name.

Tuples: Each table row represents a single entity occurrence within the entity set.

Relation: Consider a relation named Account:

AccountNumber BranchNumber Balance

A relation schema R, denoted by R (A1, A2, …, An), is made up of a relation name R and a list of
attributes, A1, A2, … , An. Each attribute Ai is the name of a role played by some domain D in
the relation schema R. D is called the domain of Ai and is denoted by dom(Ai). A relation of
degree seven, which stores information about university students, would contain seven attributes
describing each student as follows:

STUDENT (Name, Ssn, mobile, Address, Age)

Using the data type of each attribute, the definition is sometimes written as:

STUDENT (Name: string, Ssn: string, mobile: string, Address: string, Age: integer)
Relational Model Constraints:
Domain Constraints: Domain constraint enforces restrictions on information that we store in the
database so Domain constraints specify that within each tuple, the value of each attribute A must
be an atomic value from the domain dom(A).

At the simplest level, the database assigned to the column enforces domain integrity. For
example, you won’t be able to enter text in a domain that is defined as a number. The more we
can do to limit the data that can be entered into the field to its domain (length of data). SQL
provides a variety of tools for enforcing domain integrity. These are :

 Datatype
 Not Null: It specifies which data is absolute necessary for the database to function
properly.
 Check: It will check the range from values for the column with check constraints.

Create table student(name char(15) not null, studentId char(10),degree char(15),primary


key(studentId),check(degree in(‘Bachelors’, ’Master’, ’Doctorate’));

Degree must be one of the following Bachelors, Master, and Doctorate

Entity Integrity Constraint: The basic idea of Entity Integrity is that we must be able to uniquely
identify each entity that we store in the database. The entity integrity constraint states that no
primary key value can be NULL. This is because the primary key value is used to identify
individual tuples in a relation. Having NULL values for the primary key implies that we cannot
identify some tuples.

 Only one Primary key is allowed in a table.


 Primary key will not allow null values.
 Primary key will not allow duplicate values.
 It is not compulsory but recommended.

Referential Integrity Constraint: Key constraints and entity integrity constraints are specified on
individual relations. The referential integrity constraint is specified between two relations and is
used to maintain the consistency among tuples in the two relations. Informally, the referential
integrity constraint states that a tuple in one relation that refers to another relation must refer to
an existing tuple in that relation. For example, the attribute Dno of EMPLOYEE gives the
department number for which each employee works; hence, its value in every EMPLOYEE tuple
must match the Dnumber value of some tuple in the DEPARTMENT relation.

Introduction to Triggers in SQL: Another important statement in SQL is CREATE


TRIGGER. In many cases it is convenient to specify the type of action to be taken when certain
events occur and when certain conditions are satisfied. For example, it may be useful to specify a
condition that, if violated, causes some user to be informed of the violation. A manager may
want to be informed if an employee’s travel expenses exceed a certain limit by receiving a
message whenever this occurs. The action that the DBMS must take in this case is to send an
appropriate message to that user. The condition is thus used to monitor the database.

The CREATE TRIGGER statement is used to implement such actions in SQL. Here we just give
a simple example of how triggers may be used.

Suppose we want to check whenever an employee’s salary is greater than the salary of his or her
direct supervisor in the COMPANY database. Several events can trigger this rule: inserting a
new employee record, changing an employee’s salary, or changing an employee’s supervisor.
Suppose that the action to take would be to call an external stored procedure
SALARY_VIOLATION,5 which will notify the supervisor. The trigger could then be written as
in R5 below. Here we are using the syntax of the Oracle database system.
R5: CREATE TRIGGER SALARY_VIOLATION BEFORE INSERT OR UPDATE OF
SALARY, SUPERVISOR_SSN ON EMPLOYEE FOR EACH ROW WHEN ( NEW.SALARY
> ( SELECT SALARY FROM EMPLOYEE WHERE SSN = NEW.SUPERVISOR_SSN ) )
INFORM_SUPERVISOR(NEW.Supervisor_ssn, NEW.Ssn );

SQL: 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. Also, they are using different dialects, such as −
 MS SQL Server using T-SQL,
 Oracle using PL/SQL,
 MS Access version of SQL is called JET SQL (native format) etc.

Why SQL? SQL is widely popular because it offers the following advantages −
 Allows users to access data in the relational database management systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that data.
 Allows to embed within other languages using SQL modules, libraries & pre-compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a database.
 Allows users to set permissions on tables, procedures and views.

SQL Commands: The standard SQL commands to interact with relational databases are
CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be
classified into the following groups based on their nature.

DDL - Data Definition Language

Sr.No. Command & Description

1 CREATE: Creates a new table, a view of a table, or other object in the


database.

2 ALTER: Modifies an existing database object, such as a table.

3 DROP: Deletes an entire table, a view of a table or other objects in the database.
DML - Data Manipulation Language

Sr.No. Command & Description

1 SELECT: Retrieves certain records from one or more tables.

2 INSERT: Creates a record.

3 UPDATE: Modifies records.

4 DELETE: Deletes records.

SQL is followed by a unique set of rules and guidelines called Syntax. This tutorial gives you a
quick start with SQL by listing all the basic SQL Syntax. All the SQL statements start with any
of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE,
SHOW and all the statements end with a semicolon (;).
Various Syntax in SQL
SQL SELECT Statement
SELECT column1, column2....columnN FROM table_name;

SQL DISTINCT Clause


SELECT DISTINCT column1, column2....columnN FROM table_name;

SQL WHERE Clause


SELECT column1, column2....columnN FROM table_name WHERE CONDITION;

SQL AND/OR Clause


SELECT column1, column2....columnN FROM table_name WHERE CONDITION-1
{AND|OR} CONDITION-2;

SQL IN Clause
SELECT column1, column2....columnN FROM table_name WHERE column_name IN (val-1,
val-2,...val-N);

SQL BETWEEN Clause


SELECT column1, column2....columnN FROM table_name WHERE column_name
BETWEEN val-1 AND val-2;

SQL LIKE Clause


SELECT column1, column2....columnN FROM table_name WHERE column_name LIKE {
PATTERN };

SQL ORDER BY Clause


SELECT column1, column2....columnN FROM table_name WHERE CONDITION ORDER
BY column_name {ASC|DESC};

SQL GROUP BY Clause


SELECT SUM(column_name) FROM table_name WHERE CONDITION GROUP BY
column_name;

SQL COUNT Clause


SELECT COUNT(column_name) FROM table_name WHERE CONDITION;

SQL HAVING Clause


SELECT SUM(column_name) FROM table_name WHERE CONDITION GROUP BY
column_name HAVING (arithematic function condition);

SQL CREATE TABLE Statement


CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,
..... columnN datatype, PRIMARY KEY( one or more columns ));

SQL DROP TABLE Statement


DROP TABLE table_name;

SQL DESC Statement


DESC table_name;

SQL TRUNCATE TABLE Statement


TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement


ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE Statement (Rename)


ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement


INSERT INTO table_name( column1, column2....columnN) VALUES ( value1,
value2....valueN);

SQL UPDATE Statement


UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN [ WHERE
CONDITION ];

SQL DELETE Statement


DELETE FROM table_name WHERE {CONDITION};

SQL CREATE DATABASE Statement


CREATE DATABASE database_name;

SQL DROP DATABASE Statement


DROP DATABASE database_name;

SQL COMMIT Statement


COMMIT;

SQL ROLLBACK Statement


ROLLBACK;

Create Table Command: Creating a basic table involves naming the table and defining its
columns and each column's data type. The SQL CREATE TABLE statement is used to create a
new table. The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,
..... columnN datatype, PRIMARY KEY( one or more columns ) );

CREATE TABLE is the keyword telling the database system what you want to do. In this case,
you want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement. The following code block is an example, which creates a CUSTOMERS
table with an ID as a primary key and NOT NULL are the constraints showing that these fields
cannot be NULL while creating records in this table −

SQL> CREATE TABLE CUSTOMERS( ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT
NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL(18, 2),PRIMARY KEY (ID));

You can verify if your table has been created successfully by looking at the message displayed
by the SQL server, otherwise you can use the DESC command as follows –

SQL> DESC CUSTOMERS;


+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | | |
| NAME | varchar(20) | NO | | | |
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | |
| SALARY | decimal(18,2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Insert Command:

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
There are two basic syntaxes of the INSERT INTO statement which are shown below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2,
value3,...valueN)

Here, column1, column2, column3,...columnN are the names of the columns in the table into
which you want to insert the data. You may not need to specify the column(s) name in the SQL
query if you are adding values for all the columns of the table. But make sure the order of the
values is in the same order as the columns in the table.

The SQL INSERT INTO syntax will be as follows −


INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

Update Command: The SQL UPDATE Query is used to modify the existing records in a table.
You can use the WHERE clause with the UPDATE query to update the selected rows,
otherwise all the rows would be affected. The basic syntax of the UPDATE query with a
WHERE clause is as follows:

UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN


WHERE [condition];
You can combine N number of conditions using the AND or the OR operators. Consider the
CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+

| ID | NAME | AGE | ADDRESS | SALARY |

+----+----------+-----+-----------+----------+

| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |

| 2 | Khilan | 25 | Delhi | 1500.00 |


| 3 | kaushik | 23 | Kota | 2000.00 |

| 4 | Chaitali | 25 | Mumbai | 6500.00 |

| 5 | Hardik | 27 | Bhopal | 8500.00 |

The following query will update the ADDRESS for a customer whose ID number is 6 in the
table.

SQL> UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;

Delete Command: The SQL DELETE Query is used to delete the existing records from a
table. You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise all the records would be deleted. The basic syntax of the DELETE query with the
WHERE clause is as follows –

DELETE FROM table_name WHERE [condition];


You can combine N number of conditions using AND or OR operators. Consider the
CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
The following code has a query, which will DELETE a customer, whose ID is 6.

SQL> DELETE FROM CUSTOMERS WHERE ID = 6;

If you want to DELETE all the records from the CUSTOMERS table, you do not need to use
the WHERE clause and the DELETE query would be as follows −

SQL> DELETE FROM CUSTOMERS;

Now, the CUSTOMERS table would not have any record.

Question: A Store buys Product from many wholesalers. Wholesaler_ID is used to identify
individual wholesalers. Products are identified by PRODUCT_ID and store details of product.
Wholesaler phone_no are also stored and wholesaler may have multiple phone numbers. The
rates for each product charged by each wholesaler is also saved. Construct an ER Diagram and
design the database schema for the above scenario.

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