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

Csa 5

The document discusses SQL statements used to modify database and table structures. The DELETE statement is used to remove rows from a table based on an optional WHERE clause. The DROP statement removes entire databases, tables, or columns. The ALTER TABLE statement allows adding, modifying, renaming, or deleting columns or keys within a table. It does not remove the entire table structure like DROP.

Uploaded by

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

Csa 5

The document discusses SQL statements used to modify database and table structures. The DELETE statement is used to remove rows from a table based on an optional WHERE clause. The DROP statement removes entire databases, tables, or columns. The ALTER TABLE statement allows adding, modifying, renaming, or deleting columns or keys within a table. It does not remove the entire table structure like DROP.

Uploaded by

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

DELETE AND DROP

STATEMENTS
DELETE
This statement is used to delete row/s ( record/s) from
a given table.
With WHERE clause it will delete only those records which are
satisfying a given condition and without ‘WHERE’ clause it will
delete all the records from the table.
This statement will not delete a structure of a table.
➢mysql> DELETE FROM employees WHERE emp_no = 10;
➢ To delete all the rows from a table.
➢mysql> DELETE FROM employees;
DELETE
To delete record of Roll Number 4 in table STUDENT:
DROP
This statement is used to delete databases, tables from a
databases or columns from a table.
To delete database office.
mysql> DROP database office;
To delete table employees.
mysql> DROP TABLE employees;
To delete column (age) from a table student.
mysql> ALTER TABLE student DROP age;
DIFFERENCE BETWEEN DROP AND
DELETE
Drop Delete
Is used to either delete the Is used to delete a row or
table or any column in the rows of data in the table.
table. Example: DELETE FROM PAY;
Example: DROP TABLE Ex: DELETE FROM PAY where
employees; emp_id=4;
EX: ALTER TABLE STUDENT
DROP age;
ALTER TABLE STATEMENT
ALTER TABLE STATEMENT
Alter table statement is used to add, modify and delete columns. i.e.
To make changes in the structure of the table. It is also used to Rename
the table.
ADDING A COLUMN (ADD)
CHANGING COLUMN SIZE(MODIFY)
CHANGING COLUMN NAME(CHANGE)
DELETING A COLUMN (DROP)
DELETING/ ADDING PRIMARY KEY
RENAMING THE TABLE (RENAME)
25-08-2020 MEENA VAZIRANI
ADDING A COLUMN (ADD)
The ALTER statement is also used to ADD new columns with
respective data types to a table using the keyword ADD.

mysql > ALTER TABLE student


ADD bd_group VARCHAR(5);

mysql > ALTER TABLE employee


ADD gender CHAR(1);

25-08-2020 MEENA VAZIRANI


CHANGING COLUMN SIZE(MODIFY)
If only the data type or other details of a column name
are to be changed and column name is to be retained,
then the keyword MODIFY can be used in the ALTER
statement.
mysql > ALTER TABLE student
MODIFY s_class VARCHAR(12);
mysql > ALTER TABLE employee
MODIFY salary INT;
25-08-2020 MEENA VAZIRANI
CHANGING COLUMN NAME (CHANGE)

To change existing column names and to make changes in


their widths, the ALTER command is used, using the keyword
CHANGE.
Example: To change the column name s_adrs from table
student is to be changed as s_adress and its width is also to
be made as 30, then the required ALTER statement is
mysql > ALTER TABLE student
-> CHANGE s_adrs s_address VARCHAR(30);
25-08-2020 MEENA VAZIRANI
DELETING A COLUMN (DROP)
This statement is used to delete databases, tables from a
databases or columns from a table.
To delete database office.
mysql> DROP database office;
To delete table employees.
mysql> DROP TABLE employees;
To delete columns from a table employees.
mysql> ALTER TABLE employees DROP acno;
DELETING/ ADDING PRIMARY KEY
The ALTER statement can be used to specify a primary key
for a table, after the table is created.

mysql > ALTER TABLE student ADD PRIMARY KEY (s_id);

ALTER statement can also be used to remove a primary key.


mysql > ALTER TABLE student DROP PRIMARY KEY;
RENAMING THE TABLE (RENAME)
The ALTER statement is also used to rename a table.
If the table EMPLOYEE is to be renamed as EMP1
mysql > ALTER TABLE employee RENAME emp1;
RENAME
This statement is used to change the name of one or more
existing tables from the current database.
➢To change the name of table employees to emp1.
mysql> RENAME TABLE employees TO emp1;
➢ To change the name of table department to dept and
emp10 to emp11.
mysql>RENAME TABLE department TO dept, emp10 TO
emp11;

You might also like