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

CH 2

Uploaded by

naikadinath738
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CH 2

Uploaded by

naikadinath738
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter no 2 -Relational Data Model

Introduction to SQL(Structured Query Language)


1.Data Definition Language (DDL)
DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc. All the command of DDL are auto-committed that means it
permanently save all the changes in the database.

 DDL Commands:

1 CREATE
2 DESC
3 ALTER
4 RENAME
5 TRUNCATE
6 DROP
1. CREATE command
It is used to create a new table in the database.
Syntax:
Create table table_name(col_name1 datatype(size),col_name2
datatype(size)……..);

Example:

1. create table employee(empid number(10),name


varchar(10), address varchar(10), salary number(10,2));

2. create table student (rollno number(10),name varchar2(20),


address varchar(20), marks number(10,5));
2. DESC command
It is used to display the structure of table
Syntax:
desc table_name;

Example:

1. desc student;

2. desc employee;
3. ALTER command
It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new
attribute.

1. To add a new column in the table

Syntax :
ALTER TABLE table_name ADD (column_name datatype);

Example:
alter table student add(marks number(10,5);
alter table employee add(department varchar(20));
2. To modify existing column in the table:
we can change data type and size of column.

ALTER TABLE table_name MODIFY(COLUMN DEFINITION....);

EXAMPLE

alter table student modify(name varchar(50));

alter table employee modify(ename varchar(100));


3. To Delete existing column in the table:
we can change datatype and size of column.

ALTER TABLE table_name drop column coloum_name;


EXAMPLE

1. alter table student drop column rollno;

2. alter table employee drop column empid;


4. To rename column in the table:
we can change datatype and size of column.

ALTER TABLE table_name rename column oldcolumn to newcolumn


EXAMPLE

1. alter table student rename column rollno to rno;

2. alter table employee rename column empid to employeeid;


4. RENAME command
It is used to change the name of existing table
Syntax:
rename old_table_name to new_table_name

Example:

1. rename student to stud;

2. rename employee to emp,


5. TRUNCATE command

It is used to delete all the rows from the table and free the space
containing the table.

Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE EMPLOYEE;
Truncate table student;
6. DROP command
It is used to delete both the structure and record stored in the
table.

Syntax
DROP TABLE ;

Example
1. DROP TABLE EMPLOYEE;
2. Drop table student;
Assignment -1 on DDL Commands

1. Create table student using attributes rollno, studname,


percentage, address.
2. Change stud structure by adding column city.
3. Increase the size of column by 10.
4. Change the column name studname to name.
5. Display the structure of student.
6. Remove all records from table student.
7. Delete the table student.
Assignment -2 on DDL Commands

1. Create table employee having attributes empno,


ename,salary,phoneno.
2. Change the name of table to emp.
3. Change the name of column phoneno to mobileno.
4. Display the structure of table.
5. Change the size of salary column.
6. Change the datatype of ename column to varchar.
7. Add a new column join_date having date datatype.
8. Delete column address from table
9. Delete table.
2. Data Manipulation Language

DML commands are used to modify the database. It is responsible for all
form of changes in the database. The command of DML is not auto-committed that
means it can't permanently save all the changes in the database. They can be
rollback.

DML Commands:

1. INSERT
2. UPDATE
3. DELETE
1. INSERT command

The INSERT statement is a SQL query. It is used to insert data into the
row of a table.

Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES
(value 1, value2, value3, .... valueN);

Example :

1. insert into student values(1,’smitha’);


2. insert into student values(2,’nihal’);
2. UPDATE command
This command is used to update or modify the value of a column in the
table.

Syntax:

UPDATE table_name SET [column_name1= value1,......


column_nameN = valueN] [WHERE CONDITION]

For example:

1. UPDATE student SET name = ‘asha' WHERE rollno= 3;

2. update student set department=‘computer’;


DELETE Command
It is used to remove one or more row from a table.

Syntax:
DELETE FROM table_name [WHERE condition];

For example:

1. DELETE FROM student;

2. delete from student where rollno=10;


Assignment -2 on DML Commands

 Create table student having attributes (rollno, name,


birthdate, place, age, phoneno, marks, department)

1. Add 5 rows in table


2. Change marks of student rahul from 81 to 96.
3. Change the name of student to smith whose rollno is 5.
4. Delete the record of student whose name is smith.
5. Modify the marks of all students to 75 whose department is computer.
Assignment -1 on DML Commands

 Create table employee with following schema.


EMP(empno,ename,deptno,deptname,jobid,salary,hire
date)

1. Insert at least 10 rows.


2. Update salary of employee to 30000 working in sales department.
3. Delete the employee working in sales department having salary below
10000.
4. Change the salary of employee prashant Sali to 40000.
5. Modify the database so sachin now works in department mechanical.

You might also like