SQL DAY 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

SQL STUDY MATERIAL

 TABLE CREATION (EMP,DEPT)


 TABLE SCHEMA (COLUMNS) ALTERATION USING ALTER COMMAND
 TABLE DATA INSERTION.
 TABLE DATA UPDATION.
 TABLE DATA SELECTION.
 TABLE DATA DELETION
 TABLE REMOVING
Creating the table using CREATE TABLE command:
syntax: Create Table
CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype,
   ....
);

Example: Creating the DEPT Table:

create table DEPT(


deptno number(2,0) primary key,
dname varchar2(14),
loc varchar2(13),
)

II. Altering SCHEMA OF A TABLE(ADD,DROP) columns of a table

Syntax:ALTER TABLE table_name
ADD column_name datatype;

Example: Adding a new columns DeptHead column as Varchar2() Data type;

ALTER TABLE DEPT ADD DeptHead Varchar2(10);

Example 2: Removing an OLD column FROM DEPT TABLE

SYNTAX:ALTER TABLE table_name
DROP COLUMN column_name;

Eg:ALTER TABLE DEPT DROP DEPTHEAD;

Modifying Tables columns data-type and sizes

we can change or modify once tables columns data type and their size by using ALTER
command as follows

SYNTAX:

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
ALTER TABLE DEPT MODIFY COLUMN DNAME VARCHAR2(24)

**Adding a Check CONSTRIANT TO THE TABLE

ALTER TABLE STUDENT1
ADD CHECK (Age>=18);

DEPT Table Data:

deptno dname loc

10 Accounting New York

20 Research Dallas

30 Sales Chicago

40 Operations Boston

Insert data-values into the table:

The INSERT INTO command is used to insert new rows (data-values) in a table.

Syntax 1: INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);


Syntax 2: INSERT INTO TABLE_NAME (columns1,column2,…..column-n) VALUES
(value1,value2,value3,...valueN);

insert into DEPT values(10, 'ACCOUNTING', 'NEW YORK')

insert into DEPT values(20, 'RESEARCH', 'DALLAS')

insert into DEPT values(30, 'SALES', 'CHICAGO')

insert into DEPT values(40, 'OPERATIONS', 'BOSTON')

empno ename job mgr hiredate sal comm deptno


101 SMITH CLERK 102 1993-06-13 800.00 0.00 20
102 ALLEN SALESMAN 108 1998-08-15 1600.00 300.00 30
103 WARD SALESMAN 108 1996-03-26 1250.00 500.00 30
104 JONES MANAGER 102 1995-10-31 2975.00 20
105 BLAKE MANAGER 103 1992-06-11 2850.00 30
106 CLARK MANAGER 102 1993-05-14 2450.00 10
107 SCOTT ANALYST 108 1996-03-05 3000.00 20
108 KING PRESIDENT 1990-06-09 5000.00 0.00 10

Creating emp Table:

create table emp


(
empno number(4,0) primary key,
ename varchar2(10),
job varchar2(9),
mgr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0),
constraint raja foreign key(deptno) references dept(deptno)
)

Inserting data into EMP table :

insert into emp values (101,'SMITH','CLERK',102,'93/6/13',800,0.00,20);
insert into emp values (102,'ALLEN','SALESMAN',108,'98/8/15',1600,300,30);
insert into emp values (103,'WARD','SALESMAN',108,'96/3/26',1250,500,30);
insert into emp values (104,'JONES','MANAGER',102,'95/10/31',2975,null,20);
insert into emp values (105,'BLAKE','MANAGER',103,'92/6/11',2850,null,30);
insert into emp values (106,'CLARK','MANAGER',102,'93/5/14',2450,null,10);
insert into emp values (107,'SCOTT','ANALYST',108,'96/3/5',3000,null,20);
insert into emp values (108,'KING','PRESIDENT',null,'90/6/9',5000,0,10);

 UPDATE TABLE VALUES:


 We can change/update table values using UPDATE command as follows
 Syntax:
o UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
 Example: sample table with following values

NO NAME AGE

101 RAMA 40

 ****UPDATING above SAMPLE TABLE AGE VALUE 60 ON OLD VALUE 40


 UPDATE SAMPLE SET AGE=60 WHERE AGE=40
Now the SAMPLE TABLE WILL BE AS FOLLOWS

NO NAME AGE
101 RAMA 60

DELETE THE ONLY TABLE VALUES:

 the DELETE command is used to delete the table values.


 Syntax: DELETE FROM table_name WHERE condition;

Example:

 Deleting RAVANA record/entity FROM THE GIVEN SAMPLE TABLE

NO NAME AGE

101 RAMA 60

102 RAVANA 60

 DELETE FROM SAMPLE WHERE Name='RAVANA'


 Now the SAMPLE table will as follows after DELETION of RAVANA record

NO NAME AGE

101 RAMA 60

DELETING ALL RECORDS(ROWS DATA) from the TABLE


(only data will be removed but table schema will be available)

We can DELETE the ALL RECORDS (rows data) from a table using DELETE FROM COMMAND,

SYNTAX:TO DELETE ALL RECORDS OF A TABLE USING DELETE FROM COMMAND

DELETE FROM table_name;

EXAMPLE: DELETE FROM SAMPLE

SQL DROP TABLE Example


 The DROP TABLE statement is used to drop an existing table in a database.
 The following SQL statement drops the existing table "SAMPLE":
o SYNTAX: DROP TABLE SAMPLE;

SQL TRUNCATE TABLE

 The TRUNCATE TABLE statement is used to delete the data inside a table, but not the
table itself.
 Syntax:TRUNCATE TABLE table_name;

You might also like