0% found this document useful (0 votes)
63 views5 pages

Dbms

The document demonstrates various SQL commands for managing databases and tables. It shows how to view existing databases and create a new database. Tables are created within a database with constraints like primary keys and data types. Records are inserted, updated, deleted from tables and not null constraints are checked. Transactions are committed to save changes or rolled back to undo them. Databases can also be dropped when no longer needed.

Uploaded by

Diksha Naik
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)
63 views5 pages

Dbms

The document demonstrates various SQL commands for managing databases and tables. It shows how to view existing databases and create a new database. Tables are created within a database with constraints like primary keys and data types. Records are inserted, updated, deleted from tables and not null constraints are checked. Transactions are committed to save changes or rolled back to undo them. Databases can also be dropped when no longer needed.

Uploaded by

Diksha Naik
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/ 5

Practical No : 3

Perform the following


 Viewing all databases
 Creating a database
 Viewing all tables in a database
 Creating tables with constraints
 Inserting, updating, deleting records in a table
 Saving(commit) and undoing (rollback)

 Viewing all the database:


mysql> show databases;
+---------------------------+
| Database |
+---------------------------+
| information_schema |
| mysql |
+-------------------------- +
2 rows in set (0.00 sec)

 Creating a database :
mysql> create database Employeedb;
Query OK, 1 row affected (0.05 sec)

Select a database (Working in a particular database) :


mysql> use Employeedb;
Database changed

 Viewing all the tables in a database:


mysql> show tables;
Empty set (0.00 sec)

 Creating tables with constraints :


mysql> create table Employee
-> (
-> EmpId int auto_increment primary key not null,
-> Ename varchar(30) not null,
-> Age int(2) not null,
-> City varchar(20),
-> Salary decimal(7,2) not null,
-> Dob date not null
-> );
Query OK, 0 rows affected (0.13 sec)

View structure of the table:


mysql> desc Employee;
mysql> describe Employee;
+----------+------------------+---------+---------+------------+-----------------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+---------+---------+------------+-----------------------+
| EmpId | int(11) | NO | PRI | NULL | auto_increment |
| Ename | varchar(30) | NO | | | |
| Age | int(2) | NO | | | |
| City | varchar(20) | YES | | NULL | |
| Salary | decimal(7,2) | NO | | | |
| Dob | date | NO | | | |
+----------+-------------------+--------+-------+-----------+--------------------------+
Insert records in a table : (Way 1)
mysql> insert into Employee values(1,'Nitin Joshi',38,'Mumbai',45000,'1995-10-06');
Query OK, 1 row affected (0.09 sec)

Insert records in a table: (Way 2)


mysql> insert into Employee (EmpId, Ename, Age, City, Salary, Dob)
-> values(2,'Jayesh Shah',25,'Pune',23000,'2002-09-06'),
-> (3,'Anil Sane',28,'Jaipur',32000,'2004-12-15'),
-> (4,'Suresh Kapure',32,'Pune',43000,'1998-05-27');
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0

To display data:
mysql> select * from employee;
+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
+-------+--------------------------+--------+----------------+-------------+----------------+
4 rows in set (0.00 sec)

Insert data using auto_increment


mysql> insert into Employee (Ename, Age, City, Salary, Dob) values
-> ('Sachin Kadam',32,'Mumbai',20000,'2001-09-16');
Query OK, 1 row affected (0.03 sec)

To display data:
mysql> select * from employee;
+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
| 5 | Sachin Kadam | 32 | Mumbai | 20000.00 | 2001-09-16 |
+-------+--------------------------+--------+----------------+-------------+----------------+
Modify Records in a table:
mysql> update employee set age=41 where empid=6;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee;


+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
| 5 | Sachin Kadam | 41 | Mumbai | 20000.00 | 2001-09-16 |
+-------+--------------------------+--------+----------------+-------------+----------------+

Delete a data from table :


mysql> delete from employee where empid=6;
Query OK, 1 row affected (0.04 sec)

mysql> select * from employee;


+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
+-------+--------------------------+--------+----------------+-------------+----------------+

Check Not Null Constraints:


mysql> insert into Employee values
-> (6,'Ajay Patil',null,'Nasik',57000,'1998-04-24');
ERROR 1048 (23000): Column 'Age' cannot be null

Saving Transation:
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

Undoing Transaction:
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

Delete database :
mysql> drop database Employeedb;
Query OK, 0 rows affected (0.00 sec)
Rollback :
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from employee;


+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
| 5 | Sachin Kadam | 41 | Mumbai | 20000.00 | 2001-09-16 |
+-------+--------------------------+--------+----------------+-------------+----------------+
5 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into Employee values


-> (6,'Ajay Patil',24,'Nasik',57000,'1998-04-24');
Query OK, 1 row affected (0.00 sec)

mysql> select * from employee;


+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
| 5 | Sachin Kadam | 41 | Mumbai | 20000.00 | 2001-09-16 |
| 6 | Ajay Patil | 24 | Nasik | 57000.00 | 1998-04-24 |
+-------+--------------------------+--------+----------------+-------------+----------------+
6 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.04 sec)

mysql> select * from employee;


+--------+-------------------------+--------+----------------+-------------+----------------+
| EmpId | Ename | Age | City | Salary | Dob |
+--------+-------------------------+--------+----------------+-------------+----------------+
| 1 | Nitin Joshi | 38 | Mumbai | 45000.00 | 1995-10-06 |
| 2 | Jayesh Shah | 25 | Pune | 23000.00 | 2002-09-06 |
| 3 | Anil Sane | 28 | Jaipur | 32000.00 | 2004-12-15 |
| 4 | Suresh Kapure | 32 | Pune | 43000.00 | 1998-05-27 |
| 5 | Sachin Kadam | 41 | Mumbai | 20000.00 | 2001-09-16 |
+-------+--------------------------+--------+----------------+-------------+----------------+
5 rows in set (0.00 sec)
Commit:
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy