Assignment 1A
Assignment 1A
Assignment No 1A
Title of Assignment: SQL DDL Statements
Assignment Name: - Design and Develop SQL DDL statements which demonstrate the use
of SQL objects such as Table, View, Index, Sequence, Synonym, different constraints etc.
Theory: -
Objective:
● To learn and understand the concept and DDL queries of SQL Table,View
and Index using MYSQL using 2-tier .
New Concepts:
CREATE Table
VARCHAR is a good choice for the name, owner, and species columns because the column values
vary in length. The lengths in those column definitions need not all be the same, and need not be
20. You can normally pick any length from 1 to 65535, whatever seems most reasonable to you.
DESCRIBE Table
To verify that your table was created the way you expected, use a DESCRIBE statement:
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| roll_no | int (3) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | int(3) | NO | | NULL | |
+ + + + + + +
Constraints
● NOT NULL - a value is required
Example
Create table stud (name varchar (20) not null);
● UNIQUE - the attribute value must be unique in the table
Example
Create table stud (rollno number(4) constraint uk1 unique key);
● PRIMARY KEY - unique and used to refer to tuples
Example
Create table stud (rollno number(4)constraint pk1 primary key)
● FOREIGN KEY – It refers to the another column which is primary key
Syntax:
Create table table_name(Attr_name Attr_type(size)references table_name(attr_name))
Example
Create table stud1 (rollno number(4) references stud(rollno));
● CHECK (condition) - for general constraints on a table's contents.
Example
CHECK (rollno BETWEEN 1 AND 60)
You can also add constraint after table creation using alter table option
Eg Alter table stud add constraint prk1 primary key(rollno);
You can also drop constraint using Drop command & name of constraint
Eg Drop constraint prk1;
ALTER Table
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table
Syntax
Example
Following example shows use of Alter command. First example add new column Address in
table and second example modify in size of age column. Third example shows how to drop
column age.
1> Alter table Student add Address varchar(30);
2> Alter table Student modify age int(5);
3> Alter table Student drop column age;
DROP Table
TRUNCATE TABLE
What if we only want to delete the data inside the table, and not the table itself?
VIEW in MySQL
CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains
rows and columns, just like a real table. The fields in a view are fields from one or more real
tables in the database.
INDEX in MySQL
Index in SQL is created on existing tables to retrieve the rows quickly. When there are thousands
of records in a table, retrieving information will take a long time. Therefore indexes are created
on columns which are accessed frequently, so that the information can be retrieved quickly.
Indexes can be created on a single column or a group of columns. When a index is created, it
first sorts the data and then it assigns a ROWID for each row.
Drop Index
Database changed
+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| abhi | 1 |
| piyu | 2 |
| abd | 3 |
| abd | 4 |
| abc | 5 |+-------------+ ---------- +
+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| abhi | 1 |
| piyu | 2 |
| abd | 3 |
| abd | 4 |
| abc | 5 |
| xyz | 6 |
Records: 0
Duplicates: 0
Warnings: 0
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
| client_no | client_name | address | city
| bal_due | telephone_no |
| pincode | state
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
|
| 1 | abhi
5000 |
|
| 2 | piyu
10000 |
|
| 3 | abd
5000 |
|
| 4 | abd
5000 |
|
| 5 | abc
5000 |
|
| 6 | xyz
6000 |
| nasik
| nasik | 422004 | MH
| nasik | 422004 | MH
| nasik | 422003 | MH
| nasik | 422003 | MH
| nasik | 422003 | MH
| nasik | 422004 | MH
NULL |
| nasik
NULL |
| nasik
NULL |
| nasik
NULL |
| nasik
NULL |
| nasik
NULL |
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
6 rows in set (0.00 sec)
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
| product_no | description | profit_per | unit_measure |
quantity | reorder | sell_price | cost_price |
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
|
4|
1 | shampoo
|
2|
10 |
1 | one
15 |
||
4|
2 | oil
2|
|
11 |
13 | one
16 |
|
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
2 rows in set (0.00 sec)
Records: 0
Duplicates: 0
Warnings: 0
+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |
+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |
| 100 | abd |
+---------+------+
3 rows in set (0.00 sec)
+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |
| 100 | abd |
| 101 | reh |
+---------+------+
4 rows in set (0.00 sec)
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
| client_no | client_name | address | city
| bal_due | telephone_no |
| pincode | state
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
|
| 1 | abhi
5000 | NULL |
| nasik
|
| 2 | piyu
10000 | NULL |
|
| 3 | abd
5000 | NULL |
|
| 4 | nut
5000 | NULL |
|
| 5 | abc
5000 | NULL |
|
| 6 | xyz
6000 | NULL |
| nasik
| nasik
| nasik
| nasik
| nasik
| nasik | 422004 | MH
| nasik | 422004 | MH
| nasik | 422003 | MH
| nasik | 422003 | MH
| nasik | 422003 | MH
| nasik | 422004 | MH
+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
6 rows in set (0.00 sec)
+----------------+
| Tables_in_Abhi |
+----------------+
| auto
|
Warnings: 0
Query OK, 0 rows| client_master
|
| product_master |
+----------------+
3 rows in set (0.08 sec)
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
| product_no | description | profit_per | unit_measure |
quantity | reorder | sell_price | cost_price |
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
|
4 | 1 | shampoo
|
2|
10 | 1 | one
15 | |
|
4 | 2 | oil
2 | 13 | one
16 | |
|
11 |
+------------+-------------+------------+--------------
+----------+---------+------------+------------+
2 rows in set (0.00 sec)
+--------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| product_no | int(11)
| description | profit_per
| NO
| PRI | NULL | |
| varchar(20) | YES | | NULL | |
| float | YES | | NULL | |
| unit_measure | varchar(10) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| reorder | int(11) | YES | | NULL | |
| sell_price | float | YES | | NULL | |
| cost_price | float | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
8 rows in set (0.05 sec)
+--------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| product_no | int(11)
| description | profit_per
| NO
| PRI | NULL | |
| varchar(20) | YES | | NULL | |
| float | YES | | NULL | |
| unit_measure | varchar(10) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| reorder | int(11) | YES | | NULL | |
| sell_price | float(10,2) | YES | | NULL | |
| cost_price | float | | NULL | |
| YES
+--------------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql>