structure query language (sql)
TRANSCRIPT
• SQL is a language that provides an interface to a relational database system.
•SQL is developed by IBM in 1970s and it is a defactostandard as well as ISO & ANSI standard
•SQL was also supports DML for insert, update & delete operations and DDL for creating and modifying tables and other database structures.
• SQL can be used by range of users, including those with little or no programming experience
• It is a non procedural language
• It reduces the amount of time required for creating and maintaining system
• It is a English like language.
Features of Structure Query Language (SQL)
• SQL starts with a verb “select”. This verb may have additional adjective “from“
• Each verb is followed by number of clauses, eg: from, where, having
• A space separates clauses. Eg: drop• A comma (,) separates parameter without a clauses• A ; is used to end SQL statement• Identifiers can contain upto 30 character and must
start with a alphabetical character.• Character and Date literals must be enclosed with in
a single cote(‘ ’)• Comments can be enclosed between /* and */
symbols and may be multilne. • Single line comment may be prefixed with a - symbol
Rules for Structure Query Language (SQL)
• Delimiters are symbols are compound symbols which have a special meaning with in a SQL statement
SQL Delimiter
+ - * / Arithmetic Operators
=, <, >, <=, >= , !=, <> ^=
Relational Operators
:= Assignment Operators
, Item separators
; Terminator
DDL
It is a set of SQL commands used to create, modify and delete databasestructure but not data. This commands are normally used by DBA
DML
It is a area of SQL that allows changing data with in the database
DCL
It is a component of the SQL statement that control access to data and tothe database. DCL Statements are grouped with DML statement.
DQL
It is a component of a SQL statement that allows getting data from thedatabase and imposing ordering upon it. It includes the selectstatement
Components of SQL
• Create: to create object in the database
• Alter: alters the structure of the database
• Drop: delete objects from the database.
• Truncate: remove all records from a table
• Comment: add comments to the data dictionary
• Grant: gives users access privileges to database
• Revoke: withdraw access privileges given with the grant command
Commands used in DDL
Commands used in DML
Insert: insert data into a table
Update: updates existing data within a table
Delete: delete all records from a table. space for the records remain.
Call: call a PL/SQL or Java subprogramme
• Commit: save work done
• Savepoint: identify a point in a transaction to which you can later roll back
• Roll back: restore database to original since the last commit
• Grant
• Revoke
DCL
• Select: retrieve data from a database
DQL
Data type in SQLData types come in several forms and sizes, allowing the programmer to create tables suited to the scope of the project.
Data type Description
CHAR(size) This data type is used to store character strings values of fixed
length. The size in brackets determines the number of
character the cell can hold. It can hold between 1 and 2000
bytes for the CHAR column width. The default is 1 byte.(up to
255 character)
VARCHAR(size)
(size)/
VARCHAR2(size)
The VARCHAR2 data type stores variable-length
alphanumeric data. When you create a table with
a VARCHAR2 column, you specify a maximum string length
(in bytes or characters) between 1 and 4000 bytes for
the VARCHAR2 column.
NUMBER(P,S) The NUMBER data type stores fixed and floating-point numbers.
Numbers of virtually any magnitude can be stored and are guaranteed
portable among different systems operating Oracle Database, up to 38
digits of precision.
For numeric columns, you can specify the column as:
column_name NUMBER
Optionally, you can also specify a precision (total number of digits)
and scale (number of digits to the right of the decimal point):
column_name NUMBER (precision, scale)
If a precision is not specified, the column stores values as given. If no
scale is specified, the scale is zero.
Data type Description
Cont…
DATE The DATE data type stores point-in-time values (dates and times) in a table. The DATE date type stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight).Database can store dates in the Julian era, ranging from January 1, 4712 BC to December 31, 9999 CE (Common Era, or 'AD’), CE date entries are the default.Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second.For input and output of dates, the standard Oracle date format is DD-MON-YY, as '13-NOV-92' You can change this default date format for an instance with the parameter NLS_DATE_FORMAT. You can also change it during a user session with the ALTER SESSION statement. To enter dates that are not in standard Oracle date format, use the TO_DATE function with a format mask:TO_DATE ('November 13, 1992', 'MONTH, DD, YYYY') Oracle Database stores time in 24-hour format—HH:MI:SS.
Cont…
Data type Description
What is an Operator in SQL?
An operator is a reserved word or a character used primarily in an SQLstatement's WHERE clause to perform operation(s), such as comparisons andarithmetic operations.Operators are used to specify conditions in an SQL statement and to serve asconjunctions for multiple conditions in a statement.
•Arithmetic operators•Comparison operators•Logical operators•Operators used to negate conditions
SQL Arithmetic Operators:Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
+ Addition - Adds values on either side of the operator a + b will give 30
- Subtraction - Subtracts right hand operand from left hand
operand
a - b will give -10
* Multiplication - Multiplies values on either side of the
operator
a * b will give 200
/ Division - Divides left hand operand by right hand operand b / a will give 2
% Modulus - Divides left hand operand by right hand
operand and returns remainder
b % a will give 0
SQL Comparison Operators:Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example
= Checks if the values of two operands are equal or not, if yes
then condition becomes true.
(a = b) is not true.
!= Checks if the values of two operands are equal or not, if values
are not equal then condition becomes true.
(a != b) is true.
<> Checks if the values of two operands are equal or not, if values
are not equal then condition becomes true.
(a <> b) is true.
> Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
(a > b) is not true.
< Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
(a < b) is true.
>= Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
(a >= b) is not true.
<= Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.
(a <= b) is true.
!< Checks if the value of left operand is not less than the value of
right operand, if yes then condition becomes true.
(a !< b) is false.
!> Checks if the value of left operand is not greater than the value
of right operand, if yes then condition becomes true.
(a !> b) is true.
SQL Logical Operators:Here is a list of all the logical operators available in SQL.
Operator Description
ALL The ALL operator is used to compare a value to all values in another value set.
AND The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
ANY The ANY operator is used to compare a value to any applicable value in the list according to
the condition.
BETWEEN The BETWEEN operator is used to search for values that are within a set of values, given the
minimum value and the maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row in a specified table that
meets certain criteria.
IN The IN operator is used to compare a value to a list of literal values that have been specified.
LIKE The LIKE operator is used to compare a value to similar values using wildcard operators.
NOT The NOT operator reverses the meaning of the logical operator with which it is used. Eg:
NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
OR The OR operator is used to combine multiple conditions in an SQL statement's WHERE
clause.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
DDL Statements
The SQL CREATE TABLE Statement
Data Definition Language (DDL) statement are SQL statements that support the definition or declaration of database objects (For example, Create table, drop table, and alter table). The CREATE TABLE statement is used to create a table in a database. Tables are organized into rows and columns; and each table must have a name.
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type(size),column_name2 data_type(size),column_name3 data_type(size),....);
The column_name parameters specify the names of the columns of the table. The data_typeparameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.). The size parameter specifies the maximum length of the column of the table.
Example
Now we want to create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
We use the following CREATE TABLE statement:
CREATE TABLE Persons(PersonID number(3),FirstName varchar2(20),Address varchar2(30),City varchar2(15));
Cont…
The ALTER TABLE Statement
A table structure can be modified using the Alter Table command. We can do the following on a table.
• Add a new column• Modify an existing column• Drop an existing column• Define a default value for the new column• Modify the existing constraints
A) Adding columnsTo add a column in a table, we have to use ALTER Command syntax:ALTER TABLE table_nameADD (column_name datatype);
SQL ALTER TABLE ExampleLook at the "Persons" table:
P_Id FirstName Address City
1 Ola Timoteivn 10 Sandnes
2 Tove Borgvn 23 Sandnes
3 Kari Storgt 20 Stavanger
Now we want to add a column named "DateOfBirth" in the "Persons" table.We use the following SQL statement:ALTER TABLE PersonsADD (DateOfBirth date);
The "Persons" table will now like this:
P_Id FirstName Address City DateOfBirth
1 Ola Timoteivn 10 Sandnes
2 Tove Borgvn 23 Sandnes
3 Kari Storgt 20 Stavanger
B) Modify table columns
The data type, size, and default value of a column can also be modified. However, a change to the default value affects only the subsequent insertions to the table. we want to change the data type of the column named "DateOfBirth" in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons modify(DateOfBirth number(4));
Notice that the "DateOfBirth" column is now of type number and is going to hold a year in a two-digit or four-digit format.
P_Id FirstName Address City
1 Ola Timoteivn 10 Sandnes
2 Tove Borgvn 23 Sandnes
3 Kari Storgt 20 Stavanger
C) Deleting a column
Using Drop column key word the table column can be deleted. Some do’s and don’ts with regard to dropping of columns can be summarized below:
•Columns can be dropped even if it has values•Dropping of columns can’t be rolledback•All columns of the table cannot be dropped•Parent column can’t be dropped
we want to delete the column named "DateOfBirth" in the "Persons" table.We use the following SQL statement:
ALTER TABLE Persons DROP COLUMN DateOfBirth;The "Persons" table will now like this:
D) Dropping Table
To drop a table completely from the database, the following command can be used
Syntax: Drop table tablename;
Example drop table persons;
This command drops the table “persons” completely from the database. When this statement is executed,
•All data and data structure in the table is deleted•Any pending transactions are committed•All indexes are droppedYou cannot rollback this statement
E) Renaming a tableTo rename a table for example “persons” to “customer”, we can use the following statementSyntax: Rename persons to customer;This command can be used to rename a view, or sequence, or synonym.
F) Truncating a table
Truncating a table deletes all rows in a table permanently.•Removes all rows from a table•Releases the storage space used by that table•Cannot rollback row removal?•Using the delete statement, rows can be rolledback.
Syntax: Truncate table tablename;