DBMS UNIT 2 SQL Commands
DBMS UNIT 2 SQL Commands
- Pankaj Kathiriya
Content
SQL commands
(i) Creating and altering tables: CREATE
statement with constraints like KEY, CHECK, DEFAULT,
ALTER and DROP statement.
(ii) Handling data using SQL: selecting data using
SELECT statement, FROM clause, WHERE clause,
HAVING clause, ORDER BY, GROUP BY,DISTINCT
and ALL predicates, Adding data with INSERT
statement, changing data with UPDATE statement,
removing data with DELETE statement.
What is data and information ?
Why we require database ?
What is data warehouse ?
What is DBMS ?
What is database ?
DDL (data definition language) and
.
DATABASE .
.
Table n
Tables in SQL Attribute names
Table name
Product
Tuples or rows
Data types
String types
CHAR(n) – fixed-length character data, n characters long Maximum
length = 2000 bytes
VARCHAR2(n) – variable length character data, maximum 4000 bytes
LONG – variable-length character data, up to 4GB. Maximum 1 per
table
Numeric types
NUMBER(p,q) – general purpose numeric data type
INTEGER(p) – signed integer, p digits wide
FLOAT(p) – floating point in scientific notation with p binary digits
precision
Date/time type
DATE – fixed-length date/time in dd-mm-yy form
TIME - a time of day with decimal fractions of a second: HH-MM-SS-
F....F
Some of The Most Important SQL
Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
Primary key
The PRIMARY KEY constraint uniquely identifies each
record in a database table.
Primary keys must contain UNIQUE values.
A primary key column cannot contain NULL values.
Most tables should have a primary key, and each
table can have only ONE primary key.
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Foreign key
A FOREIGN KEY in one table points to a PRIMARY
KEY in another table.
P_Id LastName FirstName Address City
Foreign Key
Primary key
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 2
4 24562 1
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
);
The SQL CREATE DATABASE Statement
NOT NULL
P_Id LastName FirstName Address City
1 Hansen Ola US Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari UK Stavanger
UNIQUE
UNIQUE - Ensures that each row for a column must
have a unique value.
UNIQUE KEY allows NULL value.
Example:
CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
PRIMARY KEY
PRIMARY KEY - A combination of a NOT NULL and
UNIQUE. Ensures that a column (or combination of two or
more columns) have a unique identity which helps to find a
particular record in a table more easily and quickly.
Example :
CREATE TABLE Persons
(
P_Id int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
FOREIGN KEY
FOREIGN KEY - Ensure the referential integrity of
the data in one table to match values in another
table.
Example :
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
P_Id LastName FirstName Address City
Foreign Key
Primary key
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 2
4 24562 1
CHECK
CHECK - Ensures that the value in a column meets a
specific condition.
The CHECK constraint is used to limit the value
range that can be placed in a column.
Example :
CREATE TABLE Persons
(
P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Create table of employee where age must be
greater than 18.
Emp_id
Emp_name
Emp_age
Address
DEFAULT
DEFAULT - Specifies a default value for a column.
The DEFAULT constraint is used to insert a default value
into a column.
The default value will be added to all new records, if
no other value is specified.
Example :
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT ‘predefine_value'
);
Create table with Default constraints allow default
value of salary as 15,000.
Alter Table
The ALTER TABLE statement is used to add, delete,
or modify attributes in an existing table.
SQL ALTER TABLE Syntax
To add a column in a table, use the following
syntax:
ALTER TABLE table_name ADD column_name datatype;
Example:
ALTER TABLE customer ADD city varchar(50);
To add multiple attribute/column.
ALTER TABLE table_name ADD (column_1 data type,
column_2 data type, ... column_n data type);
Example:
ALTER TABLE supplier ADD (supplier_name varchar(50),
city varchar(45));
To delete a column in a table, use the following
syntax (some database systems don't allow deleting
column)
ALTER TABLE table_name DROP COLUMN
column_name;
Example:
Alter table Person drop column city;
Use to modify a column in an existing table, the
SQL ALTER TABLE syntax is:
ALTER TABLE table_name MODIFY column_name
column_type;
Example:
ALTER TABLE supplier MODIFY supplier_name
varchar(100) NOT NULL;
ALTER TABLE table_name MODIFY (column_1 data
type, column_2 data type, ... column_n data type);
RENAME COLUMN IN TABLE
RENAME COLUMN IN TABLE
To rename a column in an existing table, the SQL
ALTER TABLE syntax is:
ALTERTABLE table_name RENAME COLUMN old_name
TO new_name;
Exampe:
ALTER TABLE supplier RENAME COLUMN
supplier_name TO sname;
RENAME TABLE
RENAME TABLE
To rename a table, the SQL ALTER TABLE syntax is:
ALTERTABLE table_name RENAME TO
new_table_name;
Example:
ALTER TABLE supplier RENAME TO vendor;
Drop or Delete table
The DROP TABLE statement is used to delete a
table.
Syntax:
DROP TABLE table_name;
Example:
Drop table employee;
Truncate Table
The TRUNCATE TABLE Statement
What if we only want to delete the data inside the
table, and not the table itself.
Then, use the TRUNCATE TABLE statement:
Syntax:
TRUNCATE TABLE table_name;
Example
truncate table employee;
Handling data using SQL
INSERT, UPDATE and DELETE statement
Selection of data
Select statement
SELECT clause
FROM clause
WHERE clause
HAVING clause
ORDER BY, GROUP BY, DISTINCT
Insertion data in table
The INSERT INTO statement is used to insert new
records in a existing table.
Syntax:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
Syntax:
INSERT INTO table_name (col_1,col_2,…)
VALUES(value1,value2, …)
Note:
If data type is character or variable – ‘data’
If data type is number or integer – number
Default constraint
Create table student(
s_name varchar(20),
s_batch varchar(10) DEFAULT ‘B1’,
s_add varchar(30)
);
To insert the record:
Insert into student(s_name,s_add) values(‘studentA’,’Andheri’);
Create table customer and insert 4 to 5 records:
Customer_id
Customer_name
Cust_contact_number
Customer_address
Customer_city
Pin_code
Date/Timestamp
create table employee(
e_id int,
e_name varchar(30),
e_add varchar(100),
e_joindate date
);
--------insert record---------
insert into employee values(1,'abc','malad',sysdate);
insert into employee values(2,'john','virar','03-DEC-2015');
Timestamp
alter table employee modify e_joindate timestamp;
insert into employee values(5,'abc','malad',sysdate);
insert into employee values
(4,'def','andheri','25-DEC-2015 10.56.41.000000 PM');
insert into employee values(4,'def','andheri','25-DEC-
2015 10-56-41-00 AM');
create table abc(col1 int,sdate date);
insert into abc values(1,sysdate);
Syntax:
Select column_name FROM Table_name;
WHERE SALARY LIKE '%200%' Finds any values that have 200 in any position
WHERE SALARY LIKE '_00%' Finds any values that have 00 in the second and third
positions
WHERE SALARY LIKE '2_%_%' Finds any values that start with 2 and are at least 3
characters in length
WHERE SALARY LIKE '%2' Finds any values that end with 2
WHERE SALARY LIKE '_2%3' Finds any values that have a 2 in the second position and
end with a 3
WHERE SALARY LIKE '2___3' Finds any values in a five-digit number that start with 2
and end with 3
SELECT* FROM CUSTOMERS WHERE SALARY LIKE
'200%';
Operator “BETWEEN”
ProductID ProductName Price
SELECT * FROM Products
1 Chais 18
WHERE Price BETWEEN 10 AND 20;
2 Chang 19
SELECT * FROM Products
3 Aniseed Syrup 10 WHERE Price NOT BETWEEN 10 AND 20;
Syntax:
SELECT column-list FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
sort the result in ascending order by NAME and SALARY:
Table alias:
SELECT C.ID, C.NAME, C.AGE, O.AMOUNT
FROM CUSTOMERS C, ORDERS O
WHERE C.ID = O.CUSTOMER_ID;
Column alias:
SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
FROM CUSTOMERS
WHERE SALARY IS NOT NULL;
select e.eno, d.eno
from emp1 e,dept1 d
where e.eno=d.eno;
Syntax:
UPDATE table_name
SET column1=value1,column2=value2,...;
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;
UPDATE CUSTOMERS
SET ADDRESS = 'Pune', SALARY = 1000.00;
Update table customer:
Cust_contact
number
Customer_address
Pin_code
Update customers
Set cust_contactno=12345678
Where Cust_id=10;
Update customers
Set cust_address=‘Borivali’
Where Cust_id=10;
If interest is to be paid only to accounts with
balance of 1000 or more.
Update account
Set balance=balance * 1.05
where balance>=1000;