0% found this document useful (0 votes)
23 views13 pages

Assignment 1A

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views13 pages

Assignment 1A

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Group A

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: -

Prerequisite: Basics of RDBMS.

Objective:
● To learn and understand the concept and DDL queries of SQL Table,View
and Index using MYSQL using 2-tier .

New Concepts:

Introduction to MySQL Table DDL Commands.

CREATE Table

Use a CREATE TABLE statement to specify the layout of your table:

mysql> CREATE TABLE Student (roll_no int (3) Primary key


auto_increment , name varchar(20) , age int(3));

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:

mysql> DESC Student;

+ + + + + + +
| 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

Alter table table_name


add/modify/drop
column_name data_type(size)

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

The DROP TABLE statement is used to delete a table.

DROP TABLE table_name

TRUNCATE TABLE

What if we only want to delete the data inside the table, and not the table itself?

Then, use the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name

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.

SQL CREATE VIEW Syntax

CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition

SQL Dropping a View

You can delete a view with the DROP VIEW command.

DROP VIEW view_name

INDEX in MySQL

Create Index Statement

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.

Syntax to create Index:

CREATE UNIQUE INDEX index_name


ON table_name ( column1, column2,...);

ALTER TABLE table_name ADD INDEX index_name (column_name);

Show Index Information

show index from table_name

Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation |


Cardinality | Sub_part | Packed | Null | Index_type | Comment

Drop Index

ALTER TABLE table_name DROP INDEX index_name


Group A: Lab Assignment No. 1
TITLE: Design and Develop SQL DDL statements which demonstrate the
use of SQL objects such as Table, View, Index, Sequence, Synonym

mysql> use Abhi;

Database changed

mysql> show tables;

Empty set (0.02 sec)

mysql> create table client_master(client_no int,client_name varchar(20),address


varchar(50),city varchar(10),pincode int,state varchar(20), bal_due float,primary
key(client_no));

Query OK, 0 rows affected (0.51 sec)

mysql> select * from client_master;

Empty set (0.02 sec)

mysql> insert into client_master


values('001','abhi','nasik','nasik','422004','MH','5000');

Query OK, 1 row affected (0.14 sec)

mysql> insert into client_master values('002','piyu','nasik','nasik','422004','MH','10000');

Query OK, 1 row affected (0.09 sec)

mysql> insert into client_master values('003','abd','nasik','nasik','422003','MH','5000');

Query OK, 1 row affected (0.06 sec)

mysql> insert into client_master values('004','abd','nasik','nasik','422003','MH','5000');

Query OK, 1 row affected (0.05 sec)

mysql> insert into client_master values('005','abc','nasik','nasik','422003','MH','5000');

Query OK, 1 row affected (0.06 sec)


mysql> select * from client_master;
+-----------+-------------+---------+-------+---------+-------
+---------+
| client_no | client_name | address | city
| bal_due |
| pincode | state
+-----------+-------------+---------+-------+---------+-------
+---------+
|
| 1 | abhi
5000 | | nasik | nasik | 422004 | MH
|
| 2 | piyu
10000 | | nasik | nasik | 422004 | MH
|
| 3 | abd
5000 | | nasik | nasik | 422003 | MH
|
| 4 | abd
5000 | | nasik | nasik | 422003 | MH
|
| 5 | abc
5000 | | nasik | nasik | 422003 | MH
+-----------+-------------+---------+-------+---------+-------
+---------+
5 rows in set (0.00 sec)

mysql> select client_name,client_no from client_master;

+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| abhi | 1 |
| piyu | 2 |
| abd | 3 |
| abd | 4 |
| abc | 5 |+-------------+ ---------- +

5 rows in set (0.00 sec)

mysql> insert into client_master values('006','xyz','nasik','nasik','422004','MH','6000');

Query OK, 1 row affected (0.15 sec)

mysql> select client_name,client_no from client_master;

+-------------+-----------+
| client_name | client_no |
+-------------+-----------+
| abhi | 1 |
| piyu | 2 |
| abd | 3 |
| abd | 4 |
| abc | 5 |
| xyz | 6 |

6 rows in set (0.08 sec)

mysql> create table product_master(product_no int,description varchar(20),profit_per


float,unit_measure varchar(10),quantity int,reorder int,sell_price float,cost_price
float,primary key(product_no));

Query OK, 0 rows affected (0.77 sec)

mysql> insert into product_master values('001','shampoo','1','one','4','2','10','15');

Query OK, 1 row affected (0.17 sec)

mysql> insert into product_master values('002','oil','13','one','4','2','11','16');

Query OK, 1 row affected (0.06 sec)

mysql> alter table client_master add telephone_no int;

Query OK, 0 rows affected (1.04 sec)

Records: 0
Duplicates: 0
Warnings: 0

mysql> select * from client_master;

+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
| 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)

mysql> select * from product_master;

+------------+-------------+------------+--------------
+----------+---------+------------+------------+
| 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)

mysql> create index client_search on client_master(client_no);

Query OK, 0 rows affected (0.42 sec)

Records: 0
Duplicates: 0
Warnings: 0

mysql> create table auto(roll_no int NOT NULL AUTO_INCREMENT,name


varchar(20),primary key(roll_no));

Query OK, 0 rows affected (0.36 sec)

mysql> select * from auto;

Empty set (0.01 sec)

mysql> insert into auto values('1','abc');

Query OK, 1 row affected (0.07 sec)

mysql> insert into auto values('2','adc');

Query OK, 1 row affected (0.08 sec)

mysql> alter table auto auto_increment=100;

Query OK, 0 rows affected (0.07 sec)


Records: 0
Duplicates: 0

mysql> select * from auto;

+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |

Warnings: 0+---------+ ----- +

2 rows in set (0.00 sec)

mysql> insert into auto values(null,'abd');

Query OK, 1 row affected (0.05 sec)


mysql> select * from auto;

+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |
| 100 | abd |
+---------+------+
3 rows in set (0.00 sec)

mysql> insert into auto values(null,'reh');

Query OK, 1 row affected (0.06 sec)

mysql> select * from auto;

+---------+------+
| roll_no | name |
+---------+------+
| 1 | abc |
| 2 | adc |
| 100 | abd |
| 101 | reh |
+---------+------+
4 rows in set (0.00 sec)

mysql> update client_master set client_name="nut" where client_no='4';

Query OK, 1 row affected (0.09 sec)


Rows matched: 1
Changed: 1
Warnings: 0

mysql> select * from client_master;

+-----------+-------------+---------+-------+---------+-------
+---------+--------------+
| 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)

mysql> create index client_find on client_master(client_name,city);

affected (0.41 sec)


Records: 0
Duplicates: 0

mysql> show tables;

+----------------+
| Tables_in_Abhi |
+----------------+
| auto
|
Warnings: 0
Query OK, 0 rows| client_master
|
| product_master |
+----------------+
3 rows in set (0.08 sec)

mysql> select * from product_master;

+------------+-------------+------------+--------------
+----------+---------+------------+------------+
| 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)

mysql> desc product_master;

+--------------+-------------+------+-----+---------+-------+
| 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)

mysql> alter table client_master rename to c_master;

Query OK, 0 rows affected (0.25 sec)

mysql> insert into product_master values('003','nutela','15','three','40','5','110','123');

Query OK, 1 row affected (0.05 sec)

mysql> alter table product_master modify sell_price float(10,2);

Query OK, 0 rows affected (0.06 sec)


Records: 0
Duplicates: 0
Warnings: 0
mysql> desc product_master;

+--------------+-------------+------+-----+---------+-------+
| 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> create view client as select client_no,client_name from c_master;

Query OK, 0 rows affected (0.05 sec)

mysql> select * from client;

+-----------+ ------------ +| client_no | client_name |


+-----------+-------------+
| 5 | abc |
| 3 | abd |
| 1 | abhi |
| 4 | nut |
| 2 | piyu |
| 6 | xyz |
+-----------+-------------+
6 rows in set (0.23 sec)

mysql>

You might also like