SQL Basics and Command
SQL Basics and Command
DAY 1
Basics
Structured Query Language
Non-procedural
Non case sensitive
Features
History of SQL
The first version of SQL was developed by at IBM by Donald
D.Chamberlin and Raymond F.Boyce at IBM by early 1970s
Initially called as SEQUEL used to manage and manipulate data stored in
system R.
In 1979 relational software Inc. released the first commercial version of
SQL
SQL was standardized by ANSI in 1986 and later by ISO in 1987.
COMMANDS FLOWCHART
SQL
COMMANDS
DML
DDL
TCL transaction
control
DQL - Data
Query
DCL - Data
control
Create
DDL
Alter
Drop
Truncate
modify
rename
drop
add
CREATE
Create table employee
(empid number(3) Primary Key,
ename varchar2(40) not null,
job varchar2(9),
hiredate date,
sal Number(7,2),
deptno number(2));
Deleting a table
DROP table structure and data gets deleted
Drop table employee;
Altering a table
4 subcategories MODIFY, RENAME, DROP,And ADD.
Objects affected table (rename only, no alter keyword), column (
COLUMN keyword should be specified for rename, drop), constraint (
has ENABLE and DISABLE )
RENAME
rename employee to employee2;
alter table employee2
rename column empno to empid;
alter table employee2
rename constraint employee_ename_uk to
employee2_ename_uk2;
ADD
alter table employee2
add ( sal2 number(7,2));
alter table employee2
add constraint employee_ename_uk UNIQUE (ename);
MODIFY
Column only
Alter table employee2
Modify (ename varchar2(25));
DROP
alter table employee2
drop constraint employee2_ename_uk2;
alter table employee2
drop column sal2;
Insert
Update
delete
delete data
merge
INSERT
insert into employee2
values(123,'gautham','HR',1234,'21-DEC2014',10000,1000,03);
insert into employee2 (EMPID,ENAME)
values(124,'ram');
insert into employee2 select * from emp;
UPDATE
Update employee2 set ename=satish
DELETE
Delete from employee2;
Delete from employee2 where empid= 124;
MERGE
Compare 2 tables, if the record is present update the record, or
else insert as a new record.
merge into employee2 e2 using emp e
ON ( e2.empid = e.empno)
when matched then
update set e2.sal = e.sal*2
when not matched then
insert
values(e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal/2,e.comm
,e.deptno);
example - commit;
savepoint
example - savepoint A;
rollback
Sequence
Used to generate unique integers, can be used to generate primary key values
automatically. Cache refers to the maximum number of cycles possible.
Constraints
To define rules on the type of data used in a column.
Effectively safeguard against incorrect or inappropriate data.
Integrity
Constraint primary and foreign keys
value
limit the values in a column's
constraint field
column
constraint limits all the values in a column
table
constraint limits all the values in the table
Integrity constraints
All the constraint can be specified with or without the constraint name
Primary key constraint
Allows null
Column level foreign key
Fk-delete options
Default the record in the parent table cannot be deleted if the corresponding
rec exists in child
On delete cascade the child records are deleted when the parent in deleted
On delete set null the foreign key column is set to null when the parent is
deleted.
Other constraints
Check
alter table emp_shrt
add constraint emp_shrt_sal_ch check (sal>500);
Not null
alter table emp_shrt
modify ( comm number(7,2) not null);
Unique