Вы находитесь на странице: 1из 8

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Experiment No: 1 Date: 06-08-2010 Rajagiri School

l of Engineering and Technology,

DDL COMMANDS
PROBLEM DEFINITION To study the data definition language (DDL) statements CREATE ALTER DROP THEORETICAL BACKGROUND In relational database systems (DBS) data are represented using tables (relations). A query issued against the DBS also results in a table. A table is uniquely identified by its name and consists of rows that contain the stored information, each row containing exactly one tuple (or record). A table can have one or more columns. A column is made up of a column name and a data type, and it describes an attribute of the tuples. The structure of a table, also called relation schema, thus is defined by its attributes. The type of information to be stored in a table is defined by the data types of the attributes at table creation time. SQL uses the terms table, row, and column for relation, tuple, and attribute, respectively. SQL is the Structured Query language used to communicate with RDMS relational database management system. SQL is composed of DML and DDL. DML(database management language) are the keywords used to access and manipulate data. Examples of DML are SELECT, UPDATE, INSERT, MERGE, DELETE, etcDDL is the Data Definition Language.DDL are the keywords you use to create objects such as views, tables and procedures. Examples of DDL are CREATE TABLE, ALTER TABLE, etc. 1. CREATE This command allows the user to create a table, the basic structure to hold the user data. While creating a table data types for each of its column should be specified. The data types define the domain of values that each column can obtain. Each row represents one piece of data, and each column can be thought of as representing a component of that piece of data. So, for example, if we have a table for recording customer information, then the columns may include information such as First Name, Last Name, Address, City, Country, Birth Date, and so on. As a result, when we specify a table, we include the column headers and the data types for that particular column. Typically, data comes in a variety of forms. It could be an integer (such as 1), a real number (such as 0.55), a string (such as 'sql'), a date/time expression (such as '2000-JAN-25 03:22:22'), or even in binary format. When we specify a table, we need to specify the data type associated with each column Syntax: CREATE TABLE table_name ( column_name1 data_type,
University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

column_name2 data_type, column_name3 data_type, .... ) Tables are created with no data unless a query is specified. After creating a table, additional column and integrity constraints can be defined with the ADD clause of the ALTER TABLE command and can change the definition of a existing column with MODIFY clause of the ALTER TABLE command. 2. ALTER The ALTER command is used to alter or modify the table design. It is used to add new constraint to the table or to the field, new columns can be added or the existing data types of the columns can be modified. ALTER TABLE can be used to drop the constraint. Syntax: To add a column in a table: ALTER TABLE table_name ADD column_name datatype To delete a column in a table: ALTER TABLE table_name DROP COLUMN column_name To change the data type of a column in a table: ALTER TABLE table_name MODIFY COLUMN column_name datatype 3. DROP To remove a relation from SQL database we use the drop table command. The command deletes all information about the dropped relation from the database. Syntax: DROP TABLE<table name>; PROGRAM DEVELOPMENT CREATE THE FOLLOWING TABLES: Manufacturer Column name manufacturer_id Datatype Varchar2 Size 20 20 Default Attributes

manufacturer_nam Varchar2 e

University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

SQL> CREATE table manufacturer ( manufacturer_id varchar2(20), manufacturer_name varchar2(20) ); Category Column name category_id category_name Datatype Varchar2 Varchar2 Size 20 20 Default Attributes

SQL> CREATE table category ( category_id varchar2(20), category_name varchar2(20) ); Products Column name product_id product_name manufacturer_id Datatype Varchar2 Varchar2 Varchar2 Size 20 20 20 20 20 20 Default Attributes

manufacturer_nam Varchar2 e Sales Product_date category_id number date Varchar2

SQL> CREATE table product ( product_id varchar2(20), product_name varchar2(20), manufacturer_id varchar2(20), manufacturer_name varchar2(20), sales number(20), product_date date, category_id varchar2(20) );
University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

Sales_Order Column name Order_no Product_id Qty_ordered Qty_disposed Order_date Datatype Varchar2 Varchar2 int int date Size 20 20 Default Attributes

SQL> CREATE table sales_order ( Order_no Number(10), Product_id varchar2(10), Qty_ordered int, Qty_disposed int, Order_date date ); Product_Master Column name Product_id Product_name Datatype Varchar2 Varchar2 Size 20 20 Default Attributes

CREATE table product_master ( Product_id varchar2(20) check(Product_id='P%'), Product_name varchar2(20) check(Product_name=upper(Product_name)) ); Table created 1) Add the field No of items that can hold a number up to 8 digits in length to the table category ALTER table category add noofitems number(8); Table altered 2) Add a primary key data constraint for the column product_id belonging to the table products

University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

ALTER table product add primary key(product_id); Table altered 3) Drop the field quantity ordered from the sales_order ALTER table sales-order DROP column qty_ordered; Table altered DROP 1)Drop the table CAR SQL> Drop table CAR; Table dropped TESTING STRATEGIES AND SUMMARY OF RESULTS Test Case 1: (i) SQL> desc manufacturer;

Name Null? Type ----------------------------------------- -------- --------------------MANUFACTURER_ID VARCHAR2(20) MANUFACTURE_NAME VARCHAR2(20) (ii) SQL> desc category; Name Null? Type ----------------------------------------- -------- --------------------CATEGORY_ID VARCHAR2(20) CATEGORY_NAME VARCHAR2(20) (iii) SQL> desc product;

Name Null? Type ----------------------------------------- -------- --------------------PRODUCT_ID NOT NULL VARCHAR2(20) PRODUCT_NAME VARCHAR2(20) MANUFACTURER_ID VARCHAR2(20) MANUFACTURER_NAME VARCHAR2(20) SALES NUMBER(20) PRODUCT_DATE DATE CATEGORY_ID VARCHAR2(20)

University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

(iv)

SQL> desc sales_order

Name Null? Type ----------------------------------------- -------- --------------------ORDER_NO NUMBER(10) PRODUCT_ID VARCHAR2(10) QTY_ORDERED INT QTY_DISPOSED INT ORDER_DATE DATE Test Case 2: Table category before adding column no-of-items SQL>desc category; Name Null? Type ----------------------------------------- -------- --------------------CATEGORY_ID VARCHAR2(20) CATEGORY_NAME VARCHAR2(20) Table after adding column no-of-items. SQL>desc category; Name Null? Type -------------------------------- -------- --------------------CATEGORY_ID VARCHAR2(20) CATEGORY_NAME VARCHAR2(20) NOOFITEMS NUMBER(8) Test Case 3: Table product before adding primary key data constraint for column product-id. SQL>desc product; Name Null? Type --------------------------------- -------- --------------------PRODUCT_ID VARCHAR2(20) PRODUCT_NAME VARCHAR2(20) MANUFACTURER_ID VARCHAR2(20) MANUFACTURER_NAME VARCHAR2(20) SALES NUMBER(20) PRODUCT_DATE DATE CATEGORY_ID VARCHAR2(20)

University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

Table after adding primary key data constraint for column product-id. Name Null? ---------------------------------- -------PRODUCT_ID NOT NULL PRODUCT_NAME MANUFACTURER_ID MANUFACTURER_NAME SALES PRODUCT_DATE CATEGORY_ID Test Case 4: Table product_master after creation. SQL> desc product_master; Name Null? Type ----------------------------------- -------- --------------------PRODUCT_ID VARCHAR2(20) PRODUCT_NAME VARCHAR2(20) Test Case 5: Table sales_order before dropping the column qty_ordered SQL>desc sales_order; Name Null? Type ----------------------------------------- -------- --------------------ORDER_NO NUMBER(10) PRODUCT_ID VARCHAR2(10) QTY_ORDERED INT QTY_DISPOSED INT ORDER_DATE DATE Table sales_order after dropping the column qty-ordered Name Null? Type ----------------------------------------- -------- --------------------ORDER_NO NUMBER(10) PRODUCT_ID VARCHAR2(20) QTY_DISPOSED INT ORDER_DATE DATE CONCLUSION The DDL commands Create, Alter and drop were studied and the expected results were obtained successfully. The tables were created using create statement. The required Type --------------------VARCHAR2(20) VARCHAR2(20) VARCHAR2(20) VARCHAR2(20) NUMBER(20) DATE VARCHAR2(20)

University Register No:65086

Department of Computer Science & Engineering R 508 Database Lab Kochi - 39 Rajagiri School of Engineering and Technology,

modifications were done in table structures using the alter statement. The attributes specified were dropped using the drop command.

University Register No:65086

Вам также может понравиться