The document discusses various SQL statements used to modify and manipulate data in database tables. It describes UPDATE statements which are used to change existing data in tables. It also covers DELETE statements that remove rows from tables and ALTER TABLE statements that modify the structure of tables by adding, removing or changing columns.
The document discusses various SQL statements used to modify and manipulate data in database tables. It describes UPDATE statements which are used to change existing data in tables. It also covers DELETE statements that remove rows from tables and ALTER TABLE statements that modify the structure of tables by adding, removing or changing columns.
The document discusses various SQL statements used to modify and manipulate data in database tables. It describes UPDATE statements which are used to change existing data in tables. It also covers DELETE statements that remove rows from tables and ALTER TABLE statements that modify the structure of tables by adding, removing or changing columns.
STATEMENTS UPDATE AND SET STATEMENTS These statements are used to make changes in the data of the table. e.g. Consider the table student: UPDATE AND SET STATEMENTS
e.g. to change the age of Roll number 1( Asha) to 25 in table
STUDENT the command is : UPDATE AND SET STATEMENTS UPDATE AND SET STATEMENTS To increase the marks of all the students by 3, whose age is 20, the command is: UPDATE AND SET STATEMENTS WHERE CLAUSE • The WHERE clause allows to choose rows which match the given condition like exact matching with a given string value or a numeric value compared with another number etc. • The WHERE clause is used in many statements such as UPDATE, DELETE, SELECT etc. • It filters out the unwanted records from the list of words and the query / command is performed on only those records, which match the conditions given in WHERE clause. 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; 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) 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 VARCHAR(10); 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 > ALTERTABLE student MODIFY s_class VARCHAR(12); mysql > ALTERTABLE employee MODIFY salary INT; 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 > ALTERTABLE student -> CHANGE s_adrs s_address VARCHAR(30); 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;