0% found this document useful (0 votes)
2 views46 pages

Introduction to SQL

The document provides an introduction to SQL, detailing its history, data types, commands, constraints, and operators. It explains the significance of SQL as the standard language for relational databases and outlines various built-in data types, naming rules, and constraints used in Oracle databases. Additionally, it covers SQL operators, expressions, and the classification of SQL commands into DDL, DML, TCL, and DCL.

Uploaded by

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

Introduction to SQL

The document provides an introduction to SQL, detailing its history, data types, commands, constraints, and operators. It explains the significance of SQL as the standard language for relational databases and outlines various built-in data types, naming rules, and constraints used in Oracle databases. Additionally, it covers SQL operators, expressions, and the classification of SQL commands into DDL, DML, TCL, and DCL.

Uploaded by

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

Introduction to

SQL
INTRO, DATA TYPES, COMMANDS, CONSTRAINTS, OPERATORS
What is SQL?

Structured Query Language (SQL) is the set of statements with


which all programs and users access data in an Oracle database.
Conti..
►Dr. E. F. Codd published the paper, "A
Relational Model of Data for Large Shared
Data Banks", in June 1970 in the
Association of Computer Machinery (ACM)
journal, Communications of the ACM.

►Codd's model is now accepted as the


definitive model for relational database
management systems (RDBMS).
►The language, Structured English
Query Language (SEQUEL) was
developed by IBM Corporation, Inc., to
use Codd's model.
►SEQUEL later became SQL (still
pronounced as "sequel"). In 1979,
Relational Software, Inc. (now Oracle)
introduced the first commercially
available implementation of SQL.
Today, SQL is accepted as the standard
RDBMS language.
►Both ANSI and the ISO have accepted
SQL as the standard language for
relational databases.
►SQL*Plus is an interactive and batch
query tool that is installed with every
Oracle Database server or client
installation.
►It has a command-line user interface and
a web-based user interface called
iSQL*Plus.
Oracle Built-in Datatypes
►Each value manipulated by Oracle Database has a
datatype. The datatype of a value associates a fixed set of
properties with the value. These properties cause Oracle to
treat values of one datatype differently from values of
another.

►When you create a table, you must specify a datatype for


each of its columns.
Varchar2(size)
►Variable-length character string having maximum length
size bytes or characters.
►Maximum size is 4000 bytes or characters, and minimum is
1 byte or 1 character. You must specify size for
VARCHAR2.
►Mainly used for fields that are not involved in any
calculation. A column with varchar2() datatype can contain
numbers, characters as well as special characters.
Char(size)
► Fixed-length character data of length size bytes.
► Maximum size is 2000 bytes or characters.
► Default and minimum size is 1 byte.
► This datatype is generally used for those columns that
have fixed length values.
Number(p,s)

► It is used to store numeric values either integer values


or floating point values.
► ‘p’ stands for precision and indicates the total number
of digits in a number.
► ‘s’ stands for scale and indicates the total number of digits
after the decimal point in the number.
► Columns that are involved in any calculation need to be
assigned number datatype.
Date
► Columns that need to store date values or time
values need to be assigned the date datatype.
► The default date format is ‘DD-MON-YY’.
Table and Field Naming Rules
Some basic rules for naming tables and fields are listed
below:
▪ The name of table or field can be from 1 character to 30
characters.
▪ Alphabets from A – Z (in upper case or lower case) and
numbers from 0 to 9 are allowed.
▪ Names must start with an alphabet.
▪ Special characters ‘_’, ‘$’ and ‘#’ are allowed. Use of ‘_’
must be made in place of a space.
▪ SQL reserved words are not allowed.
Few examples of valid and invalid table and field
names are given below:
Reason for Invalid
Valid Names Invalid Names
Names

Employee_personal_ Name more than 30


Employee
details_table characters long

Joining_Date Joining Date Space not allowed


Name cannot start
Student#Marks #Student#Marks
with #
A1 A*1 Invalid Character *
Constraints:
▪ Constraint can be considered as a set of
rule associated with a single column or
multiple columns of Oracle Table.
▪ Constraints on columns of Oracle table
help in validating the data and restricting
incorrect data.
▪ Constraints can be categorized as:
Primary Key, Foreign Key, Unique, Not
Null and Check.
▪ Constraints can be associated while
creating a table or while altering the table
structure.
▪ When a constraint is given while creating a table along with the column
definition than we say that it is column level constraint.
▪ In case a constraint is defined after all column are defined while creating
a table, we say that the constraint is defined at table level.
▪ A constraint at table level is required when:
- One constraint is associated with multiple
columns like composite primary key.
- Multiple constraints need to be specified on a
single column.
▪ Not Null constraint can be specified only at column level.
Constraints:
Constraints:

• NOT NULL Constraint − Ensures that a column cannot have NULL value.
• DEFAULT Constraint − Provides a default value for a column when none is
specified.
• UNIQUE Constraint − Ensures that all values in a column are different.
• PRIMARY Key − Uniquely identifies each row/record in a database table.
• FOREIGN Key − Uniquely identifies a row/record in any of the given
database table.
• CHECK Constraint − The CHECK constraint ensures that all the values in a
column satisfies certain conditions.
• INDEX − Used to create and retrieve data from the database very quickly.
Difference between Primary Key Constraint
and Unique Constraint
Primary Key Unique

There can be multiple


There can be only one primary
Unique constraints in one
key constraint in one table.
table.

A column with Unique constraint


A column(s) with Primary key
can contain multiple NULL
cannot contain NULL values.
values.
Difference between Primary Key Constraint
and Foreign Constraint

Primary Key Foreign Key

There can be only one


There can be multiple Foreign
primary key constraint in one
Key constraints in one table.
table.

A column with Foreign Key


A column(s) with Primary key
constraint can contain multiple
cannot contain NULL values.
NULL values.
Example :
►CREATE TABLE EMPLOYEE(
EID VARCHAR2(8) PRIMARY KEY,
ENAME VARCHAR2(30) NOT NULL,
DEPTID VARCHAR2(8) REFERENCES
DEPARTMENT(DEPT_ID) ON DELETE
CASCADE);
Additional Clauses that can be assigned with
Foreign Key Constraint:

1. On Delete Cascade: The ‘On Delete Cascade’


clause can be used additionally with the foreign
key constraint. On using this clause when we
delete a record from the parent table, all its
related records from the child table will also be
deleted.
Additional Clauses that can be assigned with
Foreign Key Constraint:
►On Delete Set Null: The ‘On Delete Set Null’
clause can be used additionally with the
foreign key constraint. On using this clause
when we delete a record from the parent table,
all its related records will have foreign key
values set to NULL.
EXAMPLE :

►CREATE TABLE EMPLOYEE(


EID VARCHAR2(8) PRIMARY KEY,
ENAME VARCHAR2(30) NOT NULL,
DEPTID VARCHAR2(8) REFERENCES
DEPARTMENT(DEPT_ID) ON DELETE SET
NULL);
Check Constraint
It is used when we need to enforce integrity rules that can be evaluated
based on a logical expression.
CREATE TABLE student
(RollNo VARCHAR2(4) CHECK(RollNo like C%’),
Name VARCHAR2(20),
Address VARCHAR2(30) ,
Marks number(5,2) CHECK(Marks > 40));
►Point to Remember
1.All SQL commands are case insensitive (i.e. it does
not make a difference if SQL command is given in
upper case or lower case).
2.However data stored in table is case sensitive. (i.e.
SQL differentiates between ‘india’ and ‘India’).
3.Each SQL command terminates with a semicolon (;).
4.Oracle stores data in data dictionary in upper case.
Make a practice of storing all data in upper case.
Operators in SQL
► An operator is a symbol used to carry out
some mathematical or logical manipulations.
► Generally operators are used to manipulate
data and variables.
► In context to SQL usually operators are used
to manipulate data and variables in the form of column
data or column names.
Expressions in SQL
► An expression is a sequence of operands and
operators that reduce to a single value.
► e.g. [10 + 5], [a+10].
► Here 10 and 5 are operands and ‘+’ is an operator. In
the second example a and 10 are operands where a is a
variable.
Operators Classification based on their
Usability:

► Arithmetic Operators
►Logical Operators
►Relational Operators
►Miscellaneous Operators
Arithmetic Operators
The list of arithmetic operators used in SQL
expressions are:
+ Addition
- Subtraction
* Multiplication
/ Division
() Enclosed Operation –
Brackets
Relational Operators
All the relational operators are binary operators i.e. each
operator works on two operands and the result is one of the
values true or false.
List of relational operators:
►= Comparison (Equals to)
►!= , <> Comparison (Not equals to)
►> Greater than
►>= Greater than or equals to
►< Less than
Logical Operators

An expression that combines two or more relational


expressions is known as logical expression. Logical
operators are used in logical expressions. The three most
commonly used logical operators are AND, OR and NOT.

<Relational expression 1><Logical


Operator><Relational expression 2>
AND Operator

When the AND operator is used in a logical


expression it evaluates to true only if both the
relational expressions evaluate to true.
If one of the conditions evaluates to false the
expression becomes false.
OR Operator
When the OR operator is used in a logical
expression it evaluates to true if any one
relational expression / condition evaluate to true.
It evaluates to false only if both the
conditions individually evaluate to false.
NOT Operator
The NOT operator can be used either with a relational
expression or logical expression. It is used in the form:
NOT <Relational expression>
When NOT operator is used with a relational expression, it
reverses the result of the relational expression. i.e. if the
relational expression evaluates to TRUE the final result becomes
false and vice versa.
Miscellaneous Operators
Apart from the most commonly used general
operators mentioned above, there are a number of
special purpose operators used in SQL queries.
►Range Searching Operator
►Pattern Matching Operators
►IS Operator
►IN Operator
IS Operator
►The IS operator can be considered as a special type
of relational operator mainly used with NULL
values.
►‘IS’ operator is similar to ‘=’ operator used with
numeric data.
►The ‘IS’ operator is used to find out NULL values in
a column of Oracle table.
►Here the column of Oracle table can have number,
varchar2 or date data type.
Range Searching Operator
► The ‘BETWEEN . . . . AND . . . .‘ can be referred to as a
range searching operator in SQL.
►This is a special operator used to fetch data that falls within a
specified range of values.
►This means the operator will return all values that fall between a
given upper limit and lower limit, including the upper and lower
limit values. The operator takes the form:
BETWEEN operand 1 AND operand 2
Here operand 1 and operand 2 can take any values.
Pattern Matching Operator
The LIKE operator used mostly for text / string
comparison is known as pattern matching operator.
Two wild card characters generally used with LIKE
operator are:

Used for comparing a single character.


(Underscore)

% Used for comparing multiple characters.


IN Operator
►The relational operator = (equals to) is used to compare
one value with any other single value. The operator IN is a
special operator used to compare a single value with
multiple (a list of) values. It takes the form:
IN(value 1, value2, . . . value n)
The IN operator will return all values from a set of
data that match the list of values specified along with the
IN operator.
►Note: SQL commands are logically classified into
different categories. This classification is totally
done by researchers or authors. Here we classify
the SQL commands as:

SQL

DDL DML TCL DCL


►DDL: Data Definition Language. These set of commands are
related to the table structure.

►DML: Data Manipulation Language. All commands related to


manipulating data fall in this category.

►TCL: Transaction Control Language. They help the user in


controlling oracle commands.

►DCL: Data Control Language. Here the user can give some
access permission or take back the permissions already assigned
to users.
DDL
DML
DCL
TCL
DQL

You might also like