Database Systems Scse
Database Systems Scse
Database Systems Scse
SCSE
INTRODUCTION
Structure Query Language(SQL) is a database query
language used for storing and managing data in
Relational DBMS.
SQL was the first commercial language introduced for
E.F Codd's Relational model of database.
Today almost all RDBMS(MySql, Oracle, Infomix,
Sybase, MS Access) use SQL as the standard database
query language.
SQL is used to perform all types of data operations in
RDBMS.
Datatypes
Oracle supplies the following built-in datatypes:
•character datatypes
CHAR
NCHAR
VARCHAR2 and VARCHAR
NVARCHAR2(
CLOB(character large object)
NCLOB
LONG
•
NUMBER datatype
DATE datatype
•
•binary datatypes
BLOB
BFILE(to represent the location of binary data)
RAW
LONG RAW
DDL: Data Definition Language
• Example:
CREATE TABLE Person ( PersonID int,
LastName varchar(255), FirstName
varchar(255), Address varchar(255), City
varchar(255) );
Create table using another
table
• A copy of an existing table can be created
using a combination of the CREATE TABLE
statement and the SELECT statement.
Syntax:
CREATE TABLE new_table_name AS SELECT
column1, column2,...
FROM existing_table_name;
Example of creating table from another table:
Syntax:
ALTER TABLE table_name ADD
column_name datatype;
Example:
ALTER TABLE Person ADD weight_in_Kgs int;
• To delete a column in an existing table:
Syntax:
Example:
ALTER TABLE Person DROP COLUMN
weight_in_Kgs;
• To rename a column in an existing table:
Syntax:
Example:
ALTER TABLE Person RENAME COLUMN
weight_in_Kgs To Wght_in_kgs;
• To modify a table by changing the data type
of a column in a table
Syntax:
Example:
ALTER TABLE Person MODIFY Wght_in_kgs
varchar(90);
TRUNCATING TABLES
It empties a table completely.
Syntax:
TRUNCATE TABLE table_name;
Example:
Truncate table person;
DESTROYING TABLES
Drop table statement with table name can destroy
a specific table
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE person;
RENAME TABLE COMMAND
Syntax-
ALTER TABLE table_name
RENAME to new_table_name;
DML: Data Manipulation
Language
Example:
SELECT * FROM person;
Selected columns and all rows
• Select<columnname1>,<columnname2>, from
<tablename>;
Example:
DELETE FROM person;
Removal of Specific Rows
Example:
Rename Person_detail To Detail_P;
Key Constraints
SQL PRIMARY KEY Constraint
Syntax:
CREATE TABLE table_name ( column1 datatype(size)
Primary Key, column2 datatype, column3
datatype, .... );
Example:
CREATE TABLE Persons ( ID int PRIMARY KEY,
LastName varchar(255),FirstName varchar(255),Age
int);
OR
CREATE TABLE Persons (ID int ,LastName varchar(255) ,
FirstName varchar(255), Age int,PRIMARY KEY (ID));
SQL PRIMARY KEY on ALTER TABLE
Syntax:
ALTER TABLE tablename
ADD PRIMARY KEY (columnname);
Example:
ALTER TABLE Persons ADD PRIMARY KEY (ID);
DROP a PRIMARY KEY Constraint
ALTER TABLE <tablename> DROP PRIMARY KEY;
SQL NOT NULL Constraint
Syntax:
CREATE TABLE table_name ( column1 datatype(size) Not null,
column2 datatype, column3 datatype, .... );
Example:
CREATE TABLE Persons (ID int NOT NULL, LastName
varchar(25) NOT NULL, FirstName varchar(25) NOT NULL,
Age int);
SQL NOT NULL on ALTER TABLE
ALTER TABLE Table_name modify
column_name NOT NULL;
SQL UNIQUE Constraint
CREATE TABLE Persons (ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,FirstName varchar(255),
Age int);
SQL UNIQUE Constraint on ALTER TABLE
ALTER TABLE Persons ADD UNIQUE (ID);
SQL NULL Values
How to Test for NULL Values?
It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.
IS NULL Syntax
SELECT column_names FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names FROM table_name
WHERE column_name IS NOT NULL;
SQL UNIQUE Constraint
CREATE TABLE Table_name ( column1 UNIQUE, column2..);
Example:create table abc(id int, name varchar2(20) Unique,
subject varchar2(20));
Example:
CREATE TABLE Persons (ID int NOT NULL,LastName
varchar(255) NOT NULL, FirstName varchar(255), Age int,
CHECK (Age>=18);
SQL CHECK on ALTER TABLE
ALTER TABLE Persons ADD CHECK (Age>=18);
Example:
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
SELECT * FROM CustomersWHERE CustomerName LIKE '%or
%';
SELECT * FROM CustomersWHERE CustomerName LIKE '_r%';
4.
SELECT * FROM CustomersWHERE ContactName LIK
E 'a%o';
5. SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
SQL IN Operator
Syntax:
SELECT column_name(s) FROM table_name
WHERE column_name IN (value1, value2, ...);
OR
SELECT column_name(s) FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Example:
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
SQL statement selects all customers that are from the same
countries as the suppliers:
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
The SQL BETWEEN Operator
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
BETWEEN with IN Example
SELECT * FROM ProductsWHERE Price BETWEEN 10 AND 2
0 AND CategoryID NOT IN (1,2,3);
COUNT FUNCTION
COUNT function is used to Count the number of rows in
a database table. It can work on both numeric and non-
numeric data types
COUNT function uses the COUNT(*) that returns the
count of all the rows in a specified table. COUNT(*)
considers duplicate and Null.
SELECT COUNT(Column_name) from table_name;
Example:
SELECT COUNT(customer_name) from customer;
OR
SELECT COUNT(*) from customer;
AVG()
SELECT AVG(column_name)FROM table_name;
Example:
select avg(customer_id) from customer;
SUM()
SELECT SUM(column_name)FROM table_name;
Example:
select sum(customer_id) from customer;
GROUP BY
SQL GROUP BY statement is used to arrange identical
data into groups. The GROUP BY statement is used
with the SQL SELECT statement.
The GROUP BY statement follows the WHERE clause
in a SELECT statement and precedes the ORDER BY
clause.
The GROUP BY statement is used with aggregation
function.
Syntax: SELECT column FROM table_name
WHERE conditions
GROUP BY column
ORDER BY column
Example:
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST
GROUP BY COMPANY;
HAVING
HAVING clause is used to specify a search condition
for a group or an aggregate.
Having is used in a GROUP BY clause. If you are not
using GROUP BY clause then you can use HAVING
function like a WHERE clause.
Syntax
SELECT column1, column2 FROM table_name
WHERE conditions GROUP BY column1, column2
HAVING conditions ORDER BY column1, column2;
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAS
T GROUP BY COMPANY HAVING COUNT(*)>2;
TCL: Transaction Control Language