SlideShare a Scribd company logo
1 of 38
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
CHAPTER 6
Basic SQL
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Chapter 6 Outline
 SQL Data Definition and Data Types
 Specifying Constraints in SQL
 Basic Retrieval Queries in SQL
 INSERT, DELETE, and UPDATE Statements in
SQL
 Additional Features of SQL
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Basic SQL
 SQL language
 Considered one of the major reasons for the
commercial success of relational databases
 SQL
 The origin of SQL is relational predicate calculus called
tuple calculus (see Ch.8) which was proposed initially
as the language SQUARE.
 SQL Actually comes from the word “SEQUEL” which was the
original term used in the paper: “SEQUEL TO SQUARE” by
Chamberlin and Boyce. IBM could not copyright that term, so they
abbreviated to SQL and copyrighted the term SQL.
 Now popularly known as “Structured Query language”.
 SQL is an informal or practical rendering of the
relational data model with syntax
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
SQL Data Definition, Data Types,
Standards
 Terminology:
 Table, row, and column used for relational model
terms relation, tuple, and attribute
 CREATE statement
 Main SQL command for data definition
 Base tables (base relations)
 Relation and its tuples are actually created and
stored as a file by the DBMS
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
The CREATE TABLE Command in
SQL
 Specifying a new relation
 Provide name of table
 Specify attributes, their types and initial
constraints
 General Format
 CREATE TABLE <Table Name> (Attribute1 type
constraint, Attribute2 type constraint, ……….);
 Eg:
 CREATE TABLE EMPLOYEE (EmpNo Char(4)
NOT NULL, Ename Varchar2(20),Bpay
number(8,2));
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Attribute Data Types in SQL
 Basic data types
 Numeric data types
 Integer numbers: INTEGER, INT, and SMALLINT
 Floating-point (real) numbers: FLOAT or REAL, and
DOUBLE PRECISION
 Character-string data types
 Fixed length: CHAR(n), CHARACTER(n)
 Varying length: VARCHAR(n), CHAR
VARYING(n), CHARACTER VARYING(n)
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Attribute Data Types and Domains in
SQL (cont’d.)
 Bit-string data types
 Fixed length: BIT(n)
 Varying length: BIT VARYING(n)
 Boolean data type
 Values of TRUE or FALSE or NULL
 DATE data type
 Ten positions
 Components are YEAR, MONTH, and DAY in the
form YYYY-MM-DD
 Multiple mapping functions available in RDBMSs to
change date formats
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Attribute Data Types and Domains in
SQL (cont’d.)
 Additional data types
 Timestamp data type
Includes the DATE and TIME fields
 Plus a minimum of six positions for decimal
fractions of seconds
 Optional WITH TIME ZONE qualifier
 INTERVAL data type
 Specifies a relative value that can be used to
increment or decrement an absolute value of a date,
time, or timestamp
 DATE, TIME, Timestamp, INTERVAL data types can
be cast or converted to string formats for comparison.
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
INSERT
 In its simplest form, it is used to add one or more
tuples to a relation
 Attribute values should be listed in the same
order as the attributes were specified in the
CREATE TABLE command
 Constraints on data types are observed
automatically
 Any integrity constraints as a part of the DDL
specification are enforced
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
The INSERT Command
 Specify the relation name and a list of values for
the tuple. All values including nulls are supplied.
 The variation below inserts multiple tuples where a new table is
loaded values from the result of a query.
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
INSERT – add or insert raw into a
table
General format of INSERT
INSERT INTO <table name> VALUES
(value1,value2,value3,………);
Numeric values – just enter the number eg 25
String data – value insider “ “ eg: “Saji”
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Eg; INSERT COMMAND
CREATE TABLE student (stno number(2), name
char(20), m1 number (4,2), m2 number (4,2), m3
number(4,2));
INSERT INTO student VALUES
(01,”Mahesh”,,35,40.75);
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
SELECT Command -
 To Display the content of a table
Format of command
SELECT < */list of columns/formula> FROM <table
name>;
* - display all columns values
list of columns – displaying required columns values
only
Formula – the value of the formula will be displayed
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
 Student - stno,name,m1,m2,m3
To display the whole table content
 SELECT * FROM student;
To display the content of stno and m2
SELECT stno,m2 FROM student;
To display the content name, total mark
SELECT name,m1+m2+m3 FROM student;
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
 Basic form of the SELECT statement:
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
The SELECT-FROM-WHERE Structure
of Basic SQL Queries (cont’d.)
 Logical comparison operators
 =, <, <=, >, >=, and <>
Eg: Select * from student where
M1>20 AND M2>20;
 Two simple condition can be combined by
using AND and OR operator
 AND – combined condition true when both
conditions are true
 Eg: Select * from student where M1>=20
AND m2>=20;
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
(2+4)*6
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
AND , OR operator in SELECT
 OR – combined condition true when either of
the conditions are true
 Eg: Select * from student where M1>20
OR m2>20;
Changing Priority order by using bracket
AND operator will get higher priority than OR
To change the priority bracket can be used
Select * from Employee where dept = ‘Sales’ AND
desig =‘Manager’ OR bpay > 5000
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
AND , OR operator in SELECT
To change the priority bracket can be used
Select * from Employee where dept = ‘Sales’ AND dept
=‘Purchase’ OR bpay > 5000
Select * from Employee where dept = ‘Sales’ AND (dept
=‘Purchase’ OR bpay > 5000)
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Avoiding Duplicates
 SELET DISTINCT dept from Employee;
- Display the different dept from Employee
Using IN and NOT IN operator
SELECT * FROM employee where dept IN
(‘Sales’,’Purchase’,’FINACE’)
SELECT * FROM employee where dept NOT IN
(‘Sales’,’Purchase’)
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Using BETWEEN operator
 Select * from Student where M1>20 and M1<40;
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Sorting Records in a table – ORDER BY
SELECT * FROM STUDENT ORDER BY M1;
- Sorting student records in ascending order of
M1
SELECT * FROM STUDENT ORDER BY NAME
DESC; - descending order of Name
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Sorting Records in a table – ORDER
BY
 SELECT * FROM STUDENT ORDER BY NAME
ASC, M1 DESC; - ASCENDIG ORDER OF NAME
AND WHEN NAME REPEATS IN DESCENDING
ORDER OF M1
 SELECT * FROM STUDENT WHERE M1>25
ORDER BY M1; - using where clause to selectively
choose records and then sorting
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
UPDATE
UPDATE <Table Name> SET
<attribute>=‘value/Expression’ [WHERE clause];
 Used to modify attribute values of one or more
selected tuples
 A WHERE-clause selects the tuples to be
modified
 An additional SET-clause specifies the attributes
to be modified and their new values
 Each command modifies tuples in the same
relation
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
UPDATE (contd.)
 Example: Give all employees with empno 10, to10% raise
in salary.
UPDATE EMPLOYEE
SET BPAY = BPAY *1.1
WHERE EMPNO = 10;
 In this request, the modified SALARY value depends on
the original SALARY value in each tuple
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
UPDATE (contd.)
 Example: Change the location and controlling
department number of project number 10 to
'Bellaire' and 5, respectively
UPDATE PROJECT
SET PLOCATION = 'Bellaire',
DNUM = 5
WHERE PNUMBER=10
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
DELETE
 Removes tuples from a relation
 DELETE <Table Name> [WHERE clause];
 Includes a WHERE-clause to select the tuples to be
deleted
 Tuples are deleted from only one table at a time
 A missing WHERE-clause specifies that all tuples in
the relation are to be deleted; the table then becomes
an empty table
 The number of tuples deleted depends on the number
of tuples in the relation that satisfy the WHERE-clause
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
The DELETE Command
 Removes tuples from a relation
 Includes a WHERE clause to select the tuples to be
deleted. The number of tuples deleted will vary.
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Basic Retrieval Queries
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Basic Retrieval Queries (Contd.)
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Specifying Constraints in SQL
Basic constraints:
 Relational Model has 3 basic constraint types that
are supported in SQL:
 Key constraint: A primary key value cannot be
duplicated
 Entity Integrity Constraint: A primary key value
cannot be null
 Referential integrity constraints : The “foreign key
“ must have a value that is already present as a
primary key, or may be null.
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Specifying Attribute Constraints
Other Restrictions on attribute domains:
 Default value of an attribute
DEFAULT <value>
NULL is not permitted for a particular attribute
(NOT NULL)
 CHECK clause
Dnumber INT NOT NULL CHECK (Dnumber >
0 AND Dnumber < 21);
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Specifying Key and Referential
Integrity Constraints
 PRIMARY KEY clause
 Specifies one or more attributes that make up the
primary key of a relation
 Dnumber INT PRIMARY KEY;
 UNIQUE clause
 Specifies alternate (secondary) keys (called
CANDIDATE keys in the relational model).
 Dname VARCHAR(15) UNIQUE;
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Specifying Key and Referential
Integrity Constraints (cont’d.)
 FOREIGN KEY clause
 Default operation: reject update on violation
 Attach referential triggered action clause
 Options include SET NULL, CASCADE, and SET
DEFAULT
 Action taken by the DBMS for SET NULL or SET
DEFAULT is the same for both ON DELETE and ON
UPDATE
 CASCADE option suitable for “relationship” relations
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Giving Names to Constraints
 Using the Keyword CONSTRAINT
 Name a constraint
 Useful for later altering
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Default attribute values and referential
integrity triggered action specification (Fig.
6.2)
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Specifying Constraints on Tuples
Using CHECK
 Additional Constraints on individual tuples within a
relation are also possible using CHECK
 CHECK clauses at the end of a CREATE TABLE
statement
 Apply to each tuple individually
 CHECK (Dept_create_date <=
Mgr_start_date);
Authors: Elmasri and Navathe
Fundamentals of Database Systems , 7e
Copyright
©
2017
Pearson
India
Education
Services
Pvt.
Ltd
Basic Retrieval Queries in SQL
 SELECT statement
 One basic statement for retrieving information from
a database
 SQL allows a table to have two or more tuples
that are identical in all their attribute values
 Unlike relational model (relational model is strictly
set-theory based)
 Multiset or bag behavior
 Tuple-id may be used as a key

More Related Content

What's hot

η θεση της γυναικας στην εργασια
η θεση της γυναικας στην εργασιαη θεση της γυναικας στην εργασια
η θεση της γυναικας στην εργασιαskourti
 
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝ
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝ
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝDimitris Psounis
 
Τα βουνά, οι λίμνες και τα ποτάμια της Ασίας
Τα βουνά, οι λίμνες και τα ποτάμια της ΑσίαςΤα βουνά, οι λίμνες και τα ποτάμια της Ασίας
Τα βουνά, οι λίμνες και τα ποτάμια της ΑσίαςLakis Varthalitis
 
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισ
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισM.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισ
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισChristos Loizos
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculusSalman Vadsarya
 
1.4 Η Επιχείρηση & το Περιβάλλον
1.4 Η Επιχείρηση & το Περιβάλλον 1.4 Η Επιχείρηση & το Περιβάλλον
1.4 Η Επιχείρηση & το Περιβάλλον Xristina Drosou
 
Κείμενα Βοήθημα Β' Γυμνασίου
Κείμενα Βοήθημα Β' ΓυμνασίουΚείμενα Βοήθημα Β' Γυμνασίου
Κείμενα Βοήθημα Β' ΓυμνασίουΓιάννης Π.
 
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ)
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ) ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ)
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ) lykkarea
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές Ασκήσεις
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές ΑσκήσειςΘουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές Ασκήσεις
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές ΑσκήσειςThanos Stavropoulos
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesDataminingTools Inc
 
types of sets
types of setstypes of sets
types of setsjayzorts
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 

What's hot (20)

Functional dependency
Functional dependencyFunctional dependency
Functional dependency
 
η θεση της γυναικας στην εργασια
η θεση της γυναικας στην εργασιαη θεση της γυναικας στην εργασια
η θεση της γυναικας στην εργασια
 
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝ
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝ
ΠΛΗ20 ΚΑΡΤΑ: ΣΥΝΟΛΑ ΚΑΙ ΠΡΑΞΕΙΣ ΣΥΝΟΛΩΝ
 
Τα βουνά, οι λίμνες και τα ποτάμια της Ασίας
Τα βουνά, οι λίμνες και τα ποτάμια της ΑσίαςΤα βουνά, οι λίμνες και τα ποτάμια της Ασίας
Τα βουνά, οι λίμνες και τα ποτάμια της Ασίας
 
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισ
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισM.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισ
M.x ρυθμοσ μεταβολησ θεωρια-μεοδολογια-ασκησεισ
 
Ms excel formula
Ms excel formulaMs excel formula
Ms excel formula
 
η εξίσωση 2ου βαθμού
η εξίσωση 2ου βαθμούη εξίσωση 2ου βαθμού
η εξίσωση 2ου βαθμού
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
1.4 Η Επιχείρηση & το Περιβάλλον
1.4 Η Επιχείρηση & το Περιβάλλον 1.4 Η Επιχείρηση & το Περιβάλλον
1.4 Η Επιχείρηση & το Περιβάλλον
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Κείμενα Βοήθημα Β' Γυμνασίου
Κείμενα Βοήθημα Β' ΓυμνασίουΚείμενα Βοήθημα Β' Γυμνασίου
Κείμενα Βοήθημα Β' Γυμνασίου
 
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ)
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ) ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ)
ΘΕΜΑΤΑ ΜΑΘΗΜΑΤΙΚΩΝ Α' ΛΥΚΕΙΟΥ (ΤΡΑΠΕΖΑ ΘΕΜΑΤΩΝ)
 
Lecture1 data structure(introduction)
Lecture1 data structure(introduction)Lecture1 data structure(introduction)
Lecture1 data structure(introduction)
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές Ασκήσεις
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές ΑσκήσειςΘουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές Ασκήσεις
Θουκυδίδου "Ιστορίαι" Α' Λυκείου: Γ', 75 – Τράπεζα Θεμάτων, Λεξιλογικές Ασκήσεις
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
Sql join
Sql  joinSql  join
Sql join
 
types of sets
types of setstypes of sets
types of sets
 
EER modeling
EER modelingEER modeling
EER modeling
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 

Similar to Unit-3-SQL-part1.ppt (20)

SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
hoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppthoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppt
 
hoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppthoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppt
 
SQLPpt.ppt
SQLPpt.pptSQLPpt.ppt
SQLPpt.ppt
 
hoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppthoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppt
 
hoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppthoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppt
 
SQLB1.ppt
SQLB1.pptSQLB1.ppt
SQLB1.ppt
 
hoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppthoffer_edm_pp_ch06.ppt
hoffer_edm_pp_ch06.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Ch 3.pdf
Ch 3.pdfCh 3.pdf
Ch 3.pdf
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 

Recently uploaded

1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证ppy8zfkfm
 
Audience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxAudience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxStephen266013
 
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...Amil baba
 
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksSensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksBoston Institute of Analytics
 
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI  MANAJEMEN OF PENYAKIT TETANUS.pptMATERI  MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI MANAJEMEN OF PENYAKIT TETANUS.pptRachmaGhifari
 
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...ssuserf63bd7
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchersdarmandersingh4580
 
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证a8om7o51
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 
Data Analysis Project Presentation : NYC Shooting Cluster Analysis
Data Analysis Project Presentation : NYC Shooting Cluster AnalysisData Analysis Project Presentation : NYC Shooting Cluster Analysis
Data Analysis Project Presentation : NYC Shooting Cluster AnalysisBoston Institute of Analytics
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshareraiaryan448
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...ThinkInnovation
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一fztigerwe
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsBrainSell Technologies
 
Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"John Sobanski
 
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethDigital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethSamantha Rae Coolbeth
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证pwgnohujw
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证zifhagzkk
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancingmohamed Elzalabany
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024patrickdtherriault
 

Recently uploaded (20)

1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
1:1原版定制利物浦大学毕业证(Liverpool毕业证)成绩单学位证书留信学历认证
 
Audience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptxAudience Researchndfhcvnfgvgbhujhgfv.pptx
Audience Researchndfhcvnfgvgbhujhgfv.pptx
 
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
NO1 Best Kala Jadu Expert Specialist In Germany Kala Jadu Expert Specialist I...
 
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor NetworksSensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
Sensing the Future: Anomaly Detection and Event Prediction in Sensor Networks
 
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI  MANAJEMEN OF PENYAKIT TETANUS.pptMATERI  MANAJEMEN OF PENYAKIT TETANUS.ppt
MATERI MANAJEMEN OF PENYAKIT TETANUS.ppt
 
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 
Data Analysis Project Presentation : NYC Shooting Cluster Analysis
Data Analysis Project Presentation : NYC Shooting Cluster AnalysisData Analysis Project Presentation : NYC Shooting Cluster Analysis
Data Analysis Project Presentation : NYC Shooting Cluster Analysis
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data Analytics
 
Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"Aggregations - The Elasticsearch "GROUP BY"
Aggregations - The Elasticsearch "GROUP BY"
 
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae CoolbethDigital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
Digital Marketing Demystified: Expert Tips from Samantha Rae Coolbeth
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancing
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 

Unit-3-SQL-part1.ppt

  • 1. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd CHAPTER 6 Basic SQL
  • 2. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Chapter 6 Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT, DELETE, and UPDATE Statements in SQL  Additional Features of SQL
  • 3. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Basic SQL  SQL language  Considered one of the major reasons for the commercial success of relational databases  SQL  The origin of SQL is relational predicate calculus called tuple calculus (see Ch.8) which was proposed initially as the language SQUARE.  SQL Actually comes from the word “SEQUEL” which was the original term used in the paper: “SEQUEL TO SQUARE” by Chamberlin and Boyce. IBM could not copyright that term, so they abbreviated to SQL and copyrighted the term SQL.  Now popularly known as “Structured Query language”.  SQL is an informal or practical rendering of the relational data model with syntax
  • 4. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd SQL Data Definition, Data Types, Standards  Terminology:  Table, row, and column used for relational model terms relation, tuple, and attribute  CREATE statement  Main SQL command for data definition  Base tables (base relations)  Relation and its tuples are actually created and stored as a file by the DBMS
  • 5. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd The CREATE TABLE Command in SQL  Specifying a new relation  Provide name of table  Specify attributes, their types and initial constraints  General Format  CREATE TABLE <Table Name> (Attribute1 type constraint, Attribute2 type constraint, ……….);  Eg:  CREATE TABLE EMPLOYEE (EmpNo Char(4) NOT NULL, Ename Varchar2(20),Bpay number(8,2));
  • 6. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Attribute Data Types in SQL  Basic data types  Numeric data types  Integer numbers: INTEGER, INT, and SMALLINT  Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION  Character-string data types  Fixed length: CHAR(n), CHARACTER(n)  Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n)
  • 7. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Attribute Data Types and Domains in SQL (cont’d.)  Bit-string data types  Fixed length: BIT(n)  Varying length: BIT VARYING(n)  Boolean data type  Values of TRUE or FALSE or NULL  DATE data type  Ten positions  Components are YEAR, MONTH, and DAY in the form YYYY-MM-DD  Multiple mapping functions available in RDBMSs to change date formats
  • 8. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Attribute Data Types and Domains in SQL (cont’d.)  Additional data types  Timestamp data type Includes the DATE and TIME fields  Plus a minimum of six positions for decimal fractions of seconds  Optional WITH TIME ZONE qualifier  INTERVAL data type  Specifies a relative value that can be used to increment or decrement an absolute value of a date, time, or timestamp  DATE, TIME, Timestamp, INTERVAL data types can be cast or converted to string formats for comparison.
  • 9. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd INSERT  In its simplest form, it is used to add one or more tuples to a relation  Attribute values should be listed in the same order as the attributes were specified in the CREATE TABLE command  Constraints on data types are observed automatically  Any integrity constraints as a part of the DDL specification are enforced
  • 10. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd The INSERT Command  Specify the relation name and a list of values for the tuple. All values including nulls are supplied.  The variation below inserts multiple tuples where a new table is loaded values from the result of a query.
  • 11. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd INSERT – add or insert raw into a table General format of INSERT INSERT INTO <table name> VALUES (value1,value2,value3,………); Numeric values – just enter the number eg 25 String data – value insider “ “ eg: “Saji”
  • 12. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Eg; INSERT COMMAND CREATE TABLE student (stno number(2), name char(20), m1 number (4,2), m2 number (4,2), m3 number(4,2)); INSERT INTO student VALUES (01,”Mahesh”,,35,40.75);
  • 13. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd SELECT Command -  To Display the content of a table Format of command SELECT < */list of columns/formula> FROM <table name>; * - display all columns values list of columns – displaying required columns values only Formula – the value of the formula will be displayed
  • 14. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd  Student - stno,name,m1,m2,m3 To display the whole table content  SELECT * FROM student; To display the content of stno and m2 SELECT stno,m2 FROM student; To display the content name, total mark SELECT name,m1+m2+m3 FROM student;
  • 15. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd The SELECT-FROM-WHERE Structure of Basic SQL Queries  Basic form of the SELECT statement:
  • 16. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd The SELECT-FROM-WHERE Structure of Basic SQL Queries (cont’d.)  Logical comparison operators  =, <, <=, >, >=, and <> Eg: Select * from student where M1>20 AND M2>20;  Two simple condition can be combined by using AND and OR operator  AND – combined condition true when both conditions are true  Eg: Select * from student where M1>=20 AND m2>=20;
  • 17. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd (2+4)*6
  • 18. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd AND , OR operator in SELECT  OR – combined condition true when either of the conditions are true  Eg: Select * from student where M1>20 OR m2>20; Changing Priority order by using bracket AND operator will get higher priority than OR To change the priority bracket can be used Select * from Employee where dept = ‘Sales’ AND desig =‘Manager’ OR bpay > 5000
  • 19. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd AND , OR operator in SELECT To change the priority bracket can be used Select * from Employee where dept = ‘Sales’ AND dept =‘Purchase’ OR bpay > 5000 Select * from Employee where dept = ‘Sales’ AND (dept =‘Purchase’ OR bpay > 5000)
  • 20. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Avoiding Duplicates  SELET DISTINCT dept from Employee; - Display the different dept from Employee Using IN and NOT IN operator SELECT * FROM employee where dept IN (‘Sales’,’Purchase’,’FINACE’) SELECT * FROM employee where dept NOT IN (‘Sales’,’Purchase’)
  • 21. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Using BETWEEN operator  Select * from Student where M1>20 and M1<40;
  • 22. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Sorting Records in a table – ORDER BY SELECT * FROM STUDENT ORDER BY M1; - Sorting student records in ascending order of M1 SELECT * FROM STUDENT ORDER BY NAME DESC; - descending order of Name
  • 23. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Sorting Records in a table – ORDER BY  SELECT * FROM STUDENT ORDER BY NAME ASC, M1 DESC; - ASCENDIG ORDER OF NAME AND WHEN NAME REPEATS IN DESCENDING ORDER OF M1  SELECT * FROM STUDENT WHERE M1>25 ORDER BY M1; - using where clause to selectively choose records and then sorting
  • 24. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd UPDATE UPDATE <Table Name> SET <attribute>=‘value/Expression’ [WHERE clause];  Used to modify attribute values of one or more selected tuples  A WHERE-clause selects the tuples to be modified  An additional SET-clause specifies the attributes to be modified and their new values  Each command modifies tuples in the same relation
  • 25. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd UPDATE (contd.)  Example: Give all employees with empno 10, to10% raise in salary. UPDATE EMPLOYEE SET BPAY = BPAY *1.1 WHERE EMPNO = 10;  In this request, the modified SALARY value depends on the original SALARY value in each tuple
  • 26. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd UPDATE (contd.)  Example: Change the location and controlling department number of project number 10 to 'Bellaire' and 5, respectively UPDATE PROJECT SET PLOCATION = 'Bellaire', DNUM = 5 WHERE PNUMBER=10
  • 27. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd DELETE  Removes tuples from a relation  DELETE <Table Name> [WHERE clause];  Includes a WHERE-clause to select the tuples to be deleted  Tuples are deleted from only one table at a time  A missing WHERE-clause specifies that all tuples in the relation are to be deleted; the table then becomes an empty table  The number of tuples deleted depends on the number of tuples in the relation that satisfy the WHERE-clause
  • 28. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd The DELETE Command  Removes tuples from a relation  Includes a WHERE clause to select the tuples to be deleted. The number of tuples deleted will vary.
  • 29. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Basic Retrieval Queries
  • 30. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Basic Retrieval Queries (Contd.)
  • 31. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Specifying Constraints in SQL Basic constraints:  Relational Model has 3 basic constraint types that are supported in SQL:  Key constraint: A primary key value cannot be duplicated  Entity Integrity Constraint: A primary key value cannot be null  Referential integrity constraints : The “foreign key “ must have a value that is already present as a primary key, or may be null.
  • 32. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Specifying Attribute Constraints Other Restrictions on attribute domains:  Default value of an attribute DEFAULT <value> NULL is not permitted for a particular attribute (NOT NULL)  CHECK clause Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);
  • 33. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Specifying Key and Referential Integrity Constraints  PRIMARY KEY clause  Specifies one or more attributes that make up the primary key of a relation  Dnumber INT PRIMARY KEY;  UNIQUE clause  Specifies alternate (secondary) keys (called CANDIDATE keys in the relational model).  Dname VARCHAR(15) UNIQUE;
  • 34. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Specifying Key and Referential Integrity Constraints (cont’d.)  FOREIGN KEY clause  Default operation: reject update on violation  Attach referential triggered action clause  Options include SET NULL, CASCADE, and SET DEFAULT  Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE  CASCADE option suitable for “relationship” relations
  • 35. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Giving Names to Constraints  Using the Keyword CONSTRAINT  Name a constraint  Useful for later altering
  • 36. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Default attribute values and referential integrity triggered action specification (Fig. 6.2)
  • 37. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Specifying Constraints on Tuples Using CHECK  Additional Constraints on individual tuples within a relation are also possible using CHECK  CHECK clauses at the end of a CREATE TABLE statement  Apply to each tuple individually  CHECK (Dept_create_date <= Mgr_start_date);
  • 38. Authors: Elmasri and Navathe Fundamentals of Database Systems , 7e Copyright © 2017 Pearson India Education Services Pvt. Ltd Basic Retrieval Queries in SQL  SELECT statement  One basic statement for retrieving information from a database  SQL allows a table to have two or more tuples that are identical in all their attribute values  Unlike relational model (relational model is strictly set-theory based)  Multiset or bag behavior  Tuple-id may be used as a key