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

Module – 3 (SQL)

The document provides an overview of SQL data definition, data types, and basic operations such as creating tables, defining attributes, and specifying constraints. It explains the syntax for SQL commands like CREATE TABLE, SELECT, UPDATE, and DELETE, along with examples to illustrate their usage. Additionally, it covers various data types, constraints, and the use of the WHERE clause for filtering results in SQL queries.

Uploaded by

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

Module – 3 (SQL)

The document provides an overview of SQL data definition, data types, and basic operations such as creating tables, defining attributes, and specifying constraints. It explains the syntax for SQL commands like CREATE TABLE, SELECT, UPDATE, and DELETE, along with examples to illustrate their usage. Additionally, it covers various data types, constraints, and the use of the WHERE clause for filtering results in SQL queries.

Uploaded by

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

Yenepoya Institute of

Technology
Moodbidri – 574225, Mangaluru, D.K

Module – 3 (SQL)

Dr. SANGAMESH C J

Professor
Dept. of information science &
Engg
YIT MOODABIDRI
SQL Data Definition and Data
Types
SQL uses the terms table, row, and column for the

formal relational model terms relation, tuple, and

attribute, respectively.
SQL command for data definition is the CREATE

statement, which can be used to create schemas, tables


(relations), types, and domains, as well as other
constructs such as views and triggers.
SQL Data Definition and Data
Types
An SQL schema is identified by a schema name and

includes an authorization identifier to indicate the user or


account who owns the schema, as well as descriptors for
each element in the schema.
SQL Data Definition and Data
Types
An SQL schema is identified by a schema name and
includes an authorization identifier to indicate the user or
account who owns the schema, as well as descriptors for
each element in the schema.
A schema is created via the CREATE SCHEMA
statement, which can include all the schema elements’
definitions.
Eg: CREATE SCHEMA COMPANY
AUTHORIZATION ‘Jsmith’;
In SQL catalog—a named collection of schemas.
The CREATE TABLE Command
in SQL
The CREATE TABLE command is used to specify a new
relation by giving it a name and specifying its attributes and
initial constraints.
The attributes are specified first, and each attribute is given
a name, a data type to specify its domain of values, and
possibly attribute constraints, such as NOT NULL.
The key, entity integrity, and referential integrity constraints
can be specified within the CREATE
TABLE statement after the attributes are declared, or they
can be added later using the ALTER TABLE command.
The CREATE TABLE Command
in SQL
Syntax: CREATION TABLE table_name
( column1 data type,
column2 data type,
.
.
.
ColumnN data type,
PRIMARY KEY(one or more columns));
The CREATE TABLE Command
in SQL

SQL> CREATE TABLE PUC


(REGNO number(5) NOT NULL,
NAME varchar2(20) NOT NULL,
COMB char(5),
MARKS number(3),
PRIMARY KEY (REGNO));
Attribute Data Types and
Domains in SQL
The basic data types available for attributes include

numeric, character string, bit string, Boolean, date, and


time.
Numeric data types
Integer numbers of various sizes like INTEGER or INT,

and SMALLINT
Floating-point (real) numbers of various precision like

FLOAT or REAL, and DOUBLE PRECISION


Formatted numbers using DECIMAL(i, j) or DEC(i, j)

or NUMERIC(i, j) where i, the precision, is the total


number of decimal digits and j, the scale, is the number
of digits after the decimal point.
Character-string datatypes
Fixed length—CHAR(n) or CHARACTER(n), where n is

the number of character

Varying length— VARCHAR(n) or CHAR VARYING(n)

or CHARACTER VARYING(n), where n is the maximum

number of characters.
Bit-string data types
Fixed length n—BIT(n) or varying length— BIT
VARYING(n), where n is the maximum number of bits.
Another variable-length bit string data type called
BINARY LARGE OBJECT or BLOB is also available to
specify columns that have large binary values, such as
images.
Boolean data type
Values of TRUE or FALSE.

In SQL, because of the presence of NULL values, a three-

valued logic is used, so a third possible value for a Boolean


data type is UNKNOWN.
DATE data types
Date has ten positions, and its components are YEAR,

MONTH, and DAY in the form YYYY-MM-DD.


The TIME data type has at least eight positions, with the

components HOUR, MINUTE, and SECOND in the form


HH:MM:SS.
TIMESTAMP data types
Timestamp data type (TIMESTAMP)

Includes the DATE and TIME fields, plus a minimum of

six positions for decimal fractions of seconds and an


optional WITH TIME ZONE qualifier.
Interval data types
Another data type related to DATE, TIME, and

TIMESTAMP is the INTERVAL data type.


This specifies an interval—a relative value that can be

used to increment or decrement an absolute value of a


date, time, or timestamp.
Specifying Constraints in SQL
These include key and referential integrity constraints,

restrictions on attribute domains and NULLs, and


constraints on individual tuples within a relation using
the CHECK clause.
SQL Constraints
Constraints are the rules enforced on data columns on
table.
These are used to limit the type of data that can enter into a
table.
This ensures the accuracy and reliability of the data in the
database.
Constraints to be column level or table level.
The column constraints are applied only to one column
where as table level constraints are applied to the entire
table.
Different SQL Constraints
NULL value: A NULL value in a table is a
field that appears to be blank.
Which means a field with a NULL value is a
field with no value.
It is important to understand that a NULL
value is different than a Zero value or a field
that contain spaces.
A field with a NULL value is one that has been
left blank during record creation.
Different SQL Constraints
NOT NULL value:
It ensures that a column cannot have NULL
value.
It does not allow the user to fill NULL as a
Value.
Example CREATE TABLE student
( RegNo number(3) NOT NULL,
Name VARCHAR(20) NOT NULL,
Marks NUMBER(3));
Different SQL Constraints
DEFAULT value:
A default value can be filled for a column using
this constraint.
When a user dose not enter a value for a column
, automatically the given default value is
inserted.
Example CREATE TABLE student
( RegNo number(3) NOT NULL,
Name VARCHAR(20) NOT NULL,
Marks NUMBER(3) DEFAULT 00);
Different SQL Constraints
UNIQUE value:
It ensures that no two rows have same value
in a particular column.

Example CREATE TABLE student


( RegNo number(3) UNIQUE,
Name VARCHAR(20) NOT NULL,
Marks NUMBER(3) DEFAULT 00);
Different SQL Constraints
PRIMARY KEY:
It makes a column as primary key for that
table.
It is similar to UNIQUE and NOT NULL key.

Example CREATE TABLE student


( RegNo number(3) PRIMARY KEY
Name VARCHAR(20) NOT NULL,
Marks NUMBER(3) DEFAULT 00);
Different SQL Constraints
CHECK Constraint:
It restricts the value as per the given range or
limit for a particular column.
Example CREATE TABLE student
( RegNo number(3) PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Marks NUMBER(3) CHECK (MARKS >=0
and MARKS <=100));
Giving Names to Constraints
Giving Names to Constraints
Figure illustrates how a constraint may be
given a constraint name, following the keyword
CONSTRAINT.
The names of all constraints within a particular
schema must be unique.
A constraint name is used to identify a
particular constraint in case the constraint must
be dropped laterand replaced with another
constraint.
Giving Names to Constraints
Basic Retrieval Queries in SQL
 The basic form of the SELECT statement, sometimes called a
mapping or a select-from-where block, is formed of the three clauses
SELECT, FROM, and WHERE and has the following form:
SELECT <attribute list>
FROM <table list>
WHERE<condition> ;
 where <attribute list> is a list of attribute names whose values are to
be retrieved by the query.
 <table list> is a list of the relation names required to process the
query.
 <condition> is a conditional (Boolean) expression that identifies the
tuples to be retrieved bythe query, the basic logical comparison
operators are =, <=, >, >=, and <>.
Basic Retrieval Queries in SQL
Retrieve the birthdate and address of the employee(s)
whose name is ‘John B Smith’
SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME = ‘John’ AND MINIT = ‘B’ AND
LNAME = ‘Smith’;
Basic Retrieval Queries in SQL
Retrieve the name and address of all employees who
work for the ‘Research’ department
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = ‘Research’ AND DNUMBER =
DNO;
SELECT OPERATION
Example : SELECT * FROM
PUC2;
REGNO NAME COMB MARKS
101 Anusha PCMC 363
102 Chandan PCME 437
103 Bheemsen PCMC 117
104 Deeksha PCME 224
105 Anu Pallavi PCMB 135
SELECT OPERATION
The specific details of the students
such as register number and results
can be displayed using the following
command.
Example :
REGNO
SELECT
MARKS
REGNO,MARKS
FROM PUC2;
101 363
102 437
103 117
104 224
105 135
WHERE CLAUSE
•The SQL where clause is used to
specify a condition while fetching the
data from single table or joining with
multiple tables.
•If the given condition is satisfied
then only it returns specific value
from the table.
•You would use WHERE clause to
filter the records and fetching only
WHERE CLAUSE
•Syntax:
SELECT column1,column2....columnN
FROM table_Name
[WHERE <CONDITION>];

You can specify a condition using


comparison or logical operators like
<,<=,>,>=,==,!=,LIKE,NOT etc.
WHERE CLAUSE
Example : Consider the following
PUC2 table for the illustration of
some of the queries:
REGNO NAME COMB MARKS
101 Anusha PCMC 363
102 Chandan PCME 437
103 Bheemsen PCMC 117
104 Deeksha PCME 224
105 Anu Pallavi PCMB 135
WHERE CLAUSE
Few required details of students are
listed using following command.
SQL> SELECT REGNO,NAME,COMB
MARKS FROM PUC2 WHERE MARKS
>= 210;
REGNO NAME COMB MARKS
101 Anusha PCMC 363
102 Chandan PCME 437
104 Deeksha PCME 224
WHERE CLAUSE
To display all details of a student from
the PUC2 table whose register
Number is 102.
SQL> SELECT * FROM PUC2
WHERE REGNO= ‘102’;
REGNO NAME COMB MARKS
102 Chandan PCME 437
WHERE CLAUSE [AND & OR]
The operators AND and OR used to
combine the multiple conditions to
obtain the desired results form the
table.
WHERE CLAUSE [AND]
AND Operator – It allows the
existence of multiple conditions in
WHERE clause.

Syntax:
SELECT col1,col2,...colN FROM
table_name
WHERE [condition1] AND
[condition2]...AND[conditionN];
WHERE CLAUSE
To display all details of a student from
the PUC2 table whose marks is
greater than 210 and their COMB is
‘PCMC’.
SQL> SELECT * FROM PUC2
WHERE
REGNO
MARKS
NAME COMB
>210 and
MARKS
101 Anusha PCMC 363
COMB=‘PCMC’
WHERE CLAUSE [OR]
OR Operator – It is also used to
combine multiple conditions in
WHERE clause.

•For an action to be taken by the SQL


statement it may be a transaction or
query, if any ONE of the conditions
separated by the OR must be TRUE.
WHERE CLAUSE [OR]

Syntax:
SELECT col1,col2,...colN FROM
table_name
WHERE [condition1] OR
[condition2]...OR[conditionN];
WHERE CLAUSE [OR]
To display all details of a student
from the
PUC2 table whose combination
COMB is
‘PCMC’ OR ‘PCME’
SQL> SELECT * FROM PUC2
WHERE
REGNO
101
COMB=‘PCMC’
NAME
Anusha
COMB
PCMC
MARKS
363
OR
COMB=‘PCME’
102 Chandan PCME 437
103 Bheemsen PCMC 117
104 Deeksha PCME 224
UPDATE OPERATION
•The update query is used to modify
the existing records in a table.
•If you considered the PUC2 table, the
modifications such as changes is the
REGNO,NAME,MARKS ,COMB can be
performed using WHERE clause with
UPDATE selected rows otherwise all the
rows would be affected.
UPDATE OPERATION

Syntax:
UPDATE TABLE table_Name
SET column1=value1,
column2=value2,
.
.
columnn=valuen,
[WHERE <condition>]
UPDATE OPERATION
•Logical operators such as AND or OR
also can be used to combine the
conditions.
UPDATE OPERATION
Following is an example, which would
add 5 marks to all students of PCMC
candidates.
SQL> UPDATE PUC2
SET MARKS= MARKS + 5
WHERE
REGNO COMB=‘PCMC’
NAME COMB MARKS
101 Anusha PCMC 368
102 Chandan PCME 437
103 Bheemsen PCMC 122
104 Deeksha PCME 224
105 Anu Pallavi PCMB 135
UPDATE OPERATION
Now there is a need to change the
combination as ‘PCMB’ for a student
whose register number is 104. It can be
changed as follows

SQL> UPDATE PUC2


SET COMB= ‘PCMB’
WHERE REGNO =104;
REGNO NAME COMB MARKS
104 Deeksha PCMB 224
DELETE OPERATION

•DELETE query is used to delete the


existing records from a table.
•You can use WHERE clause with
DELETE query to delete selected
rows, otherwise all the records would
be deleted.
DELETE OPERATION

Syntax:
DELETE FROM table_Name
[WHERE <condition>];

•Logical operators such as AND or OR


also can be used to combine the
conditions.
DELETE OPERATION
SQL> DELETE FROM PUC2 WHERE
REGNO=102;
REGNO NAME COMB MARKS
101 Anusha PCMC 368
103 Bheemsen PCMC 122
104 Deeksha PCME 224
105 Anu Pallavi PCMB 135
DELETE OPERATION
SQL> DELETE FROM PUC2 WHERE
COMB=‘PCMC’ and MARKS < 210;
REGNO NAME COMB MARKS
101 Anusha PCMC 368
102 Chandan PCME 437
104 Deeksha PCME 224
105 Anu Pallavi PCMB 135

If you want to delete all the records from the PUC2 table, you
need not to use WHERE clause. The Delete query can be used as
follows:
SQL> DELETE FROM PUC2;
LIKE CLAUSE

•It is used to compare a value with


similar values using wildcard
operations.
•There are two wildcards used in
conjunction with the LIKE operator.
•The percent sign (%).
•The underscroe ( _ ).
Additional Features of SQL
various techniques for specifying complex
retrieval queries, including nested queries,
aggregate functions, grouping, joined tables,
outer joins, case statements, and recursive
queries; SQL views, triggers, and assertions;
and commands for schema modification.
Additional Features of SQL
Each commercial RDBMS will have, in addition
to the SQL commands, a set of commands for
specifying physical database design parameters,
file structures for relations, and access paths
such as indexes. We called these commands a
storage definition language (SDL).
Additional Features of SQL
SQL has transaction control commands. These
are used to specify units of database
processing for concurrency control and
recovery purposes.
Additional Features of SQL
SQL has transaction control commands.
These are used to specify units of database
processing for concurrency control and
recovery purposes.
Additional Features of SQL
 SQL has language constructs for specifying the granting and revoking of

privileges to users. Privileges typically correspond to the right to use


certain SQL commands to access certain relations.
 Each relation is signed an owner, and either the owner or the DBA staff

can grant to selected users the privilege to use an SQL statement—such


as SELECT ,INSERT, DELETE, or UPDATE— to access the relation.
 In addition, the DBA staff can grant the privileges to create schemas,

tables, or views to certain users.


 These SQL commands—called GRANT and REVOKE
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname,City)
Write a relational algebra queries for the following.
1) List the names of the people who works for the company ‘Wipro’ along
with the Cities they live in.
2) Find the name of the persons who do not work for ‘Infosys’.
3) Find the people whose salaries are more than that of the ‘Oracle’
employees.
4) Find the persons works and lives in the same city.
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname,City)
Write a relational algebra queries for the following.
1) List the names of the people who works for the company ‘Wipro’ along
with the Cities they live in.
2) Find the name of the persons who do not work for ‘Infosys’.
3) Find the people whose salaries are more than that of the ‘Oracle’
employees.
4) Find the persons works and lives in the same city.
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname, City)
Write a relational algebra queries for the following.
1) List the names of the people who works for the
company ‘Wipro’ along with the Cities they live in.
Relational algebra queries
1) List the names of the people who works for the
company ‘Wipro’ along with the Cities they live
in.
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname,City)
Write a relational algebra queries for the following.
2) Find the name of the persons who do not work for
‘Infosys’.
Relational algebra queries
2) Find the name of the persons who do not work
for ‘Infosys’.
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname, City)
Write a relational algebra queries for the following.
3) Find the people whose salaries are more than that
of the ‘Oracle’ employees.
Relational algebra queries
3) Find the people whose salaries are more than
that of the ‘Oracle’ employees.
Relational algebra queries
Works( Pname, Cname, Salary)
Lives(Pname, Street, City)
Located-In(Cname,City)
Write a relational algebra queries for the following.
4) Find the persons works and lives in the same city.
Relational algebra queries
Write a relational algebra queries for the
following.
4) Find the persons works and lives in the same
city.

You might also like