Oracle SQL Notes
Oracle SQL Notes
INTRODUCTION
Database
A database is the place of storage of the data in the form of tables
Data means information which is very useful. A database is also collection of 1 or
more tables.
DBMS
DBMS – stands for Database Management System
DBMS is a database s/w which allows us to store the data in the form of tables.
SQL COMMANDS
1) Data Definition Language (DDL) Command:
a) Create
b) Alter
c) Drop
d) Rename
e) Truncate
a) create
Create table:-
The CREATE TABLE statement is used to create a new table in the database.
Syntax
CREATE TABLE tablename (
Column1 datatype, Column2 datatype, Column3 datatype,
);
example:
CREATE TABLE employee (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20)
);
b) Alter
ALTER TABLE:-
The ALTER TABLE statement is used such as adding and removing columns or renaming the
columns modify columns, drop columns table.
Syntax Add:-
ALTER TABLE tablename ADD Columnname Datatype;
example:
ALTER TABLE customer ADD customer_type VARCHAR(20);
Syntax Drop:-
ALTER TABLE tablename DROP column Columnname;
example:
ALTER TABLE customer DROP customer_type;
Syntax Modifying:-
ALTER TABLE tablename MODIFY column_name Datatype;
example:
ALTER TABLE customer MODIFY customer_type varchar2(7);
Modify Multiple columns in table
Syntax
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
...
column_n column_type);
example:
ALTER TABLE customers
MODIFY (customer_name varchar2(100) NOT NULL,
city varchar2(75) DEFAULT 'Seattle' NOT NULL);
Rename column in table
Syntax Rename:-
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
example:
ALTER TABLE customers RENAME COLUMN customer_name TO cname;
Rename table
Syntax
ALTER TABLE table_name RENAME TO new_table_name;
example:
ALTER TABLE customers RENAME TO contacts;
c) Drop
The Oracle DROP TABLE statement allows you to remove or delete a table from the Oracle
database.
Syntax
Drop TABLE table_name;
example:
Drop TABLE customers;
d) Rename
Syntax
RENAME TABLE table-Name TO new-Table-Name;
example:
RENAME TABLE customers To cus;
e) Truncate
The TRUNCATE TABLE statement is used to remove all records from a table structure will
remains for future use . If you truncate a table, the TRUNCATE TABLE statement can not be
rolled back.
Syntax
TRUNCATE TABLE table-Name;
example:
TRUNCATE TABLE emp;
b) Update
The update statement is used to modify the existing records in a table.
Syntax
Update table_name set column name=new values where column=old value;
Update table_name set column1=new value,column2=new value where column2=old
value2;
example:
Update emp set ename=’raghu’,city=’tpt’ where mobile_number=9955886644;
c) Delete
The delete statement is used to delete existing records into table
Method1:-
Syntax
Delete from table_name where condition;
example:
Delete from emp where empid=555555;
Method2:-
Syntax