Assignment_1
Implementation of DDL commands of SQL with suitable examples
Create table
Alter table
Drop Table
DDL (Data Definition Language)-
The SQL DDL provides command for defining relation schemas,
deleting relations and modifying relation schema.
Data Definition Language-
The SQL DDL allows specification of not only a set of relations
but also information about each relation, including-
Schema for each relation
The domain of values associated with each attribute.
The integrity constraints.
The set of indices to be maintained for each relation.
Domain types in SQL-
The SQL standard supports a variety of built in domain types, including-
Char (n) - A fixed length character length string with user specified length .
Varchar (n)- A variable character length string with user
specified maximum length n.
Int- An integer.
Small integer- A small integer.
Numeric (p, d)-A Fixed point number with user defined precision.
Real, double precision- Floating point and double precision
floating point numbers with machine dependent precision.
Float (n)- A floating point number, with precision of at least n digits.
Date- A calendar date containing a (four digit) year, month and day of the
month.
Time- The time of day, in hours, minutes and seconds Eg. Time ’09:30:00’.
Number- Number is used to store numbers (fixed or floating point).
DDL statement for creating a table-
Syntax-
Create table tablename
(columnname datatype(size),
columnname datatype(size));
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that some database systems
don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE - RENAME COLUMN
To rename a column in a table, use the following syntax:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
SQL DROP DATABASE Statement
The DROP DATABASE statement is used to drop an existing SQL database.
Syntax
DROP DATABASE databasename;
DROP DATABASE Example
The following SQL statement drops the existing database "testDB":
DROP DATABASE testDB;
Q.1 Consider the following table:
Table : Book
Field Name Data type Size Constraint
Book_Code Varchar2 5 Primary key
ISBN No. Varchar2 8 Not Null/
Book_Name Varchar2 8 Unique
Publisher Varchar2 6
Price Number 5,2
Author_Name Varchar2 8
Date_of_Launch Date
Write queries to perform the following:
1. Create table book
2. Increase the size of the Book_Name field to 10 characters.
3. Alter Author_price with name Author-name