- 1.Create a table EMPLOYEE have the following fields (emp_id, name,city,dob,adhar_no)
- mysql> create table EMPLOYEE(emp_id int,name varchar(25),dob date,adhar_no int);
- Query OK, 0 rows affected (0.25 sec
- mysql> DESC EMPLOYEE;
- +----------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-------------+------+-----+---------+-------+
- | emp_id | int(11) | YES | | NULL | |
- | name | varchar(25) | YES | | NULL | |
- | city | varchar(50) | YES | | NULL | |
- | dob | date | YES | | NULL | |
- | adhar_no | int(11) | YES | | NULL | |
- +----------+-------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
- 2.Create a table STUDENT have the fields(roll_no, name, age, address, gender)and add following constraints on it.
- Roll_no – Primary key
- Name – does not allow null values
- Age- must be in between (0-99), does not allow null values
- Gender- default value is ‘m’
- mysql> CREATE TABLE STUDENT(roll_no int primary key,name varchar(25) not null,age int not null,address varchar(25),gender varchar(20) default 'm',check (age<100));
- Query OK, 0 rows affected (0.22 sec)
- mysql> desc STUDENT;
- +---------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+-------------+------+-----+---------+-------+
- | roll_no | int(11) | NO | PRI | NULL | |
- | name | varchar(25) | NO | | NULL | |
- | age | int(11) | NO | | NULL | |
- | address | varchar(25) | YES | | NULL | |
- | gender | varchar(20) | YES | | m | |
- +---------+-------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
- 3.Create a temporary table STUDENT1 of STUDENT relation.
- mysql> create temporary table STUDENT1 select * from STUDENT;
- Query OK, 0 rows affected (0.00 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> desc STUDENT1;
- +---------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+-------------+------+-----+---------+-------+
- | roll_no | int(11) | NO | | NULL | |
- | name | varchar(25) | NO | | NULL | |
- | age | int(11) | NO | | NULL | |
- | address | varchar(25) | YES | | NULL | |
- | gender | varchar(20) | YES | | m | |
- +---------+-------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
- 4.Create a duplicate table of EMPLOYEE.
- mysql> create table if not exists EMPLOYEE_DUP select * from EMPLOYEE;
- Query OK, 0 rows affected (0.18 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> DESC EMPLOYEE_DUP;
- +----------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-------------+------+-----+---------+-------+
- | emp_id | int(11) | YES | | NULL | |
- | name | varchar(25) | YES | | NULL | |
- | city | varchar(50) | YES | | NULL | |
- | dob | date | YES | | NULL | |
- | adhar_no | int(11) | YES | | NULL | |
- +----------+-------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
- 5. Create an INVOICE database and having following tables.
- Customer(id, name, address, city, ph_no)
- Product(pid, pname, price (default 0))
- Invoice_master(inv_id, cust_id(FK), inv_date)
- Invoice_item(inv_id (FK), pid(FK), quantity)
- mysql> create database INVOICE;
- Query OK, 1 row affected (0.00 sec)
- mysql> use INVOICE;
- Database changed
- mysql> create table Customer(id int primary key,name varchar(20),address varchar
- (20),city varchar(20),ph_no varchar(20));
- Query OK, 0 rows affected (0.19 sec)
- mysql> desc Customer;
- +---------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+-------------+------+-----+---------+-------+
- | id | int(11) | NO | PRI | NULL | |
- | name | varchar(20) | YES | | NULL | |
- | address | varchar(20) | YES | | NULL | |
- | city | varchar(20) | YES | | NULL | |
- | ph_no | varchar(20) | YES | | NULL | |
- +---------+-------------+------+-----+---------+-------+
- 5 rows in set (0.01 sec)
- mysql> create table Product(pid int primary key,pname varchar(20),price int default 0);
- Query OK, 0 rows affected (0.19 sec)
- mysql> desc Product;
- +-------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+-------+
- | pid | int(11) | NO | PRI | NULL | |
- | pname | varchar(20) | YES | | NULL | |
- | price | int(11) | YES | | 0 | |
- +-------+-------------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
- mysql> create table invoice_master(inv_id int primary key,id int,inv_date date,foreign key(id) references Customer(id));
- Query OK, 0 rows affected (0.20 sec)
- mysql> desc invoice_master;
- +----------+---------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+---------+------+-----+---------+-------+
- | inv_id | int(11) | NO | PRI | NULL | |
- | id | int(11) | YES | MUL | NULL | |
- | inv_date | date | YES | | NULL | |
- +----------+---------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
- mysql> create table invoice_item(inv_id int,pid int,quantity int,foreign key(inv_id) references invoice_master(inv_id),foreign key(pid) references Product(pid));
- Query OK, 0 rows affected (0.24 sec)
- mysql> desc invoice_item;
- +----------+---------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+---------+------+-----+---------+-------+
- | inv_id | int(11) | YES | MUL | NULL | |
- | pid | int(11) | YES | MUL | NULL | |
- | quantity | int(11) | YES | | NULL | |
- +----------+---------+------+-----+---------+-------+
- 3 rows in set (0
Raw Paste