0% found this document useful (0 votes)
37 views

SQL Basics and Command

The document provides an overview of SQL basics and commands. It covers SQL features and history, as well as data definition language commands like CREATE, ALTER, and DROP. It also discusses data manipulation language commands such as INSERT, UPDATE, DELETE, and MERGE. The document concludes with transaction control language and constraints.

Uploaded by

Gautham Kumaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

SQL Basics and Command

The document provides an overview of SQL basics and commands. It covers SQL features and history, as well as data definition language commands like CREATE, ALTER, and DROP. It also discusses data manipulation language commands such as INSERT, UPDATE, DELETE, and MERGE. The document concludes with transaction control language and constraints.

Uploaded by

Gautham Kumaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL Basics and Commands

DAY 1

Basics
Structured Query Language
Non-procedural
Non case sensitive

Features

IBM endorsement DB2


Microsoft Endorsement SQL SERVER, ODBC, ADO
Dynamic data Definition
Java Integration JDBC
Client/server architecture
Programmatic database access
Multiple views of data
Portability across computing systems
Complete data base language
Industry infrastructure
Enterprise application support
Extensibility and object technology

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

DDL Data Definition Language


Implicitly Committed

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));

create from existing table


create table employee as
( select * from emp);

Deleting a table
DROP table structure and data gets deleted
Drop table employee;

TRUNCATE table data is deleted, structure remains.


truncate 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;

ENABLE AND DISABLE


alter table emp2
disable constraint emp2_empid_pk;
alter table emp2
enable constraint emp2_empid_pk;

DML Data Manipulation Language


Have to explicitly commited

Insert

insert new values or from existing tables

Update

change existing data field in table

delete

delete data

merge

"UPSERT" UPdate + inSERT

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

Where empid = 123;


Update employee2 set sal=
( select sal from employee2 where empid = 124 )
Where empid = 123;

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);

TCL Transaction Command Language


commit

save changes to database

example - commit;
savepoint

defines a point to rollback to

example - savepoint A;
rollback

go to the specified savepoint

example - rollback to savepoint A;

Sequence
Used to generate unique integers, can be used to generate primary key values
automatically. Cache refers to the maximum number of cycles possible.

create sequence seq


start with 1
increment by 1
minvalue 1
maxvalue 100
cycle
cache 20;
seq.currval, seq.nextval
can be used the access the values in sequence

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

Does not allow null


alter table emp2
modify (empid number(4) constraint emp2_empid_pk Primary
key);
alter table emp2
modify (empid number(4) primary key);
Foreign key

Allows null
Column level foreign key

alter table employee


modify( deptno number(2) constraint employee_deptno_fk
references dept (deptno) );
Table level foreign key

alter table employee


add constraint employee_fk foreign key(first name,last name)
references personel (first name,last name);
alter table emp_bkp
add constraint emp_bkp_fk foreign key (deptno) references
dept (deptno);

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

alter table emp_shrt


modify ( comm number(7,2) unique );

You might also like