You are on page 1of 6
PRACTICAL - 4 AIM: Implement various DDL (Data Definition Language) commands (DESC, RENAME, ALTER, DROP) © DDLis abbreviation of "Data Definition Language’, a subset of SQL statements that can change the structure of the database schema in some way, typically by creating, deleting, or modifying schema objects such as databases, tables, and views. ‘+ Itsimply deals with descriptions of the database schema ‘© When DDL statements are executed, data dictionary and system catalog also be updated, + It is mainly used to create files, databases, data dictionary and tables within databases. Examples of DDL commands: ‘DDLCommands | SS! ‘CREATE = Tt is used to create the database or its objects (like table, index, function, views, store procedure and triggers) DESC = Itis used to describe the structure of a table. RENAME = Itis used to rename the existing objects of database. ALTER “Tt is used to change or modify the structure of database and its objects. DROP = Tis used to delete the database or its objects TRUNCATE = Ttis used to erase all records from the table WNote: There are six different DDL commands as explained in above table, among which CRAETE command we have seen in practical no. 3, and the use of TRUNCATE command we will see in next practical. + DESCRIBE or DESC (describing a table schema) ‘+ Once the table is created if we want to check the schema or structure of the table (ie column names, data types, size of the fields etc), ‘describe’ keyword shows this. + We can also write ‘desc’ command for the same purpose (in SQL” Plus). OR DESCRIBE tablename; + It describes a given table, ie. displays information about columns defined in the table ‘+ Itdisplays column names, NOT NULL constraints and data type with its size of each column of a table Example: ‘Show table schema of a table ‘test’. Input command —sql> DESC test; output runoen(s) anew (25) unbeR(2) 4 RENAME (renaming a table) # Sometimes there isa need to rename a table. ‘rename’ command used to rename the existing table and give anew name. Syntax: RENAME tablename TO Newtablenam Example: Rename the table tos!” and set a nein name as ‘tosineni Input command —> ‘5 Output —* RA-B4043; object test does not exist QL> dese testnens Nane eo unER(5) Me vancnara(20) at unae(e) Using ‘rename’ and ‘alter’ commands we can change the column name also. en ALTER TABLE tablename RENAME COLUMN OldColumnName to NewColumnName ; Example: Rename the column eno of table ‘test' to enroll. Input command—» [SQL> ALTER TABLE test 2. RENAME COLUNN eno TO enroll; ‘Output ——> Table altered. 1 DESC tact Nane NUMBER(S) \VARCHARQ(1@) NUMBER(4) * ALTER TABLE (altering or modifying table schema) + The ALTER TABLE statement is a DDL command which is used to add, delete, or modify columns in an existing table. It is used to modify structures of a table. © The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ~ For adding new column in the table: Syntax: ALTER TABLE tablename ADD newcolumn_name datatype(size), newcolumn_name datatype(size)...5 ‘Example: Add a new column (‘gender’) to a test table having data type character of size 2: Input command —>PS™> ALTER TABLE est ADD gender char(a}s output —* Pat» Desc ven mua? type eno NuMBeRc) Rane Varenaneess) noe NUMBERC2) ‘crn criaca) en i YW. (Note: now if we want to check that the above command execitted or nol, then we have to check the table using ‘dese’ command.) = For removing (dropping) an existing column from the table: Syntax: ALTER TABLE tablename DROP COLUMN column_name ; Example: Remove (or Drop) a column ‘gender’ from table’ test’ IL> DESC test; Name Null? Type ENO NUMBER(S) NAME VARCHAR2(15) AGE NUMBER(2) GENDER CHAR(2) input commana —» Output —> NUMBER(S) VARCHAR2(15) NUNBER(2) For modifying an existing column from the table: Syntax: ALTER TABLE tablename | MODIFY column_name NewDatatype(Newsize); Example: Change the size of the field ‘age’ from 2 to 4 in the table ‘est’ NUnBER(S) ane varchar2(a5) AGE NUMBER(2) Input command —> Output —* ainneRcs) vaRcuan2 (15) UMBER(4) + DROP (dropping or removing table from database) DROP TABLE command is used to delete or destroy table from a database, ‘The DROP TABLE command drops the specified table, This means, all records along with structure of the table will be destroyed. Care must be taken while using this command, as all the records held within the table are lost and cannot be recovered. Syntax: Example: Remove the table ‘test’ from the database. Input command —+ [SQL> DROP TABLE test: WWote: after dropping the table, when we are checking the table structure using ‘desc’ command, we can’t find that table as it has been removed already.) Write SQL queries to perform following tasks: (Consider the tables created in practical no. 3) 1. WAQ (Write a Query) to show the structure of faculty table. 2. WAQ to rename the table employee to employee_new. 3. WAQ toadd one column ST MOBILENO in student table with number data type with size of 10. 4, WAQ to delete the table person from database. 5. WAQ to modify the size of a column ‘name’ in test table from 10 to 15. 6. WAQ to change the name of a column ‘age’ in test table from age to new_age. 7. WAQ to delete the column “p_city’ from person table.

You might also like