310 likes | 425 Vues
Data Definition Language (DDL) is a subset of SQL commands used for creating, modifying, and deleting databases and their objects, such as tables and views. DDL includes predefined syntax for defining data structures. Key commands include CREATE to create tables, ALTER to modify them, and DROP to delete. The CREATE TABLE command mandates a unique table name and column definitions. DDL also allows table creation from existing tables and supports record insertion, updating, and deletion through INSERT, UPDATE, and DELETE commands. Understanding DDL is essential for effective database management.
E N D
DATA MANIPULATION andCONTROL
Data definition language(DDL) • DDL stands for “ data definition language”. • It is the subset of SQL commands used to create, modify and destroy databases and database objects like –tables , views etc. • A data Definition Language has a pre-defined syntax for describing data . • For example – To built a new table using SQL syntax the CREATE command is used, followed by parameters for the table name and column definition . • The DDL can also define the name of each column and the associated data type.
Once a table is created , it can be modified using the ALTER command . • If the table is no longer needed , the DROPcommand will delete the table. • Some of the comman DDL statements in oracle include CREATE, ALTER, DROP, RENAME, TRUNCATE etc.
CREATING TABLES
CREATING TABLES • The CREATE TABLE command is used to create tables to store data. • Tables are owned by the user who creates them. The names of tables owned by a given user must be unique . The column names in the table must be unique . • Specifying table name is compulsory while creating a table.
The syntax for the CREATE TABLE statement is : CREATE TABLE table _name (column _name1 data type, Column _name2 data type, …column _nameN data type);
EXAMPLE: • CREATE TABLE STUDENT _RECORD ( Name varchar2 (20) , Class varchar2 (10), Roll _no number(5), Section char (1), Marks number (5,2) );
CREATING A TABLE WITH DATA FROM ANOTHER TABLE
CREATING A TABLE WITH DATA FROM ANOTHER TABLE A table can be created by using CREATE TABLE statement with data, derived another table . The syntax is : CREATE TABLE NEW _TABLE_NAME [(Column_1, column_2,…. Column_n)] , AS SELECT Column _1 , column_2 ,…. Column _n From OLD_TABLE_NAME ;
FOR EXAMLE • Create a table STUDENT1 from student table having student name and Rollno . • CREATE TABLE STUDENT1 • [( NAME , ROLLNO)] • AS SELECT NAME , CLASS, ROLLNO, AGE • FROM STUDENT
INSERT STATMENT • It is used to insert new rows/records in a table. • Values can be inserted for all the columns or for selected columns of the table. • We can insert data values into a table through different ways: • Inserting the data direct to the table. • Inserting the data to a table through a select statement.
The syntax for INSERT statement is: INSERT INTO TABLE_NAME VALUES (value 1,value 2,value 3,…….value n); Example: class is a table name. Insert into class values(‘ram’, 20, ’bca’);
Following statements would create 4 records in CUSTOMERS table: • INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) • VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); • INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) • VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); • INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) • VALUES (3, 'kaushik', 23, 'Kota', 2000.00 ); • INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) • VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );
UPDATE STATEMENT • To update or modify the exiting rows in a table. • It modifies specific rows if the WHERE clause is specified. • We can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.
The basic syntax of UPDATE query with WHERE clause is as follows: UPDATE table_name SET column1 = value1, column2 = value2...., column N = value N WHERE [condition]; Example: UPDATE EMP SET COMM=500; EMP is the table name.
Following is an example, which would update ADDRESS for a customer whose ID is 6: SQL> UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 4;
DELETE STATEMENT • It is used to delete rows from a table. • To delete rows from a table, table must be in your schema. • We can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.
The basic syntax of DELETE query with WHERE clause is as follows: DELETE FROM table_name WHERE [condition]; Example: DELETE FROM EMP WHERE JOB=‘ CLERK’; EMP is the table name.
Following is an example, which would DELETE a customer, whose ID is 6: SQL> DELETE FROM CUSTOMERS WHERE ID = 4;