Practical 13
MySQL Basic Commands
Database Introduction
A Database is a well-organized collection of data in the form of tables. A database can be
thought of as a collection of tables. A table organizes data in the form of rows and columns.
AIM
To create a database called college which will contain a table called teachers. To
perform the following commands -
create database / table(with constraints) - To create a new database / table
insert - To insert records into the table specified.
select(with various clauses and conditions) - To retrieve records from table
where Clause - test a condition and filter rows on the basis of the conditions.
like - The job of this operator is to look for a specified pattern in a column. This
operator uses wildcard characters % and _ (underscore) . The % sign matches to
zero or more characters whereas the _ (underscore) matches to exact one
character.
distinct Clause–To get a non-repeating set of values for an attribute.
count() – It is a simple function that counts the rows of a table or entries in a
column.
SQL Commands Execution
mysql> create database college;
Query OK, 1 row affected (0.66 sec)
mysql> use atlas;
Database changed
mysql> create table teachers(
->tcode integer not null primary key,
->tnamevarchar(30),
-> dept varchar(10),
-> post varchar(20),
-> gender varchar(6),
-> salary double
-> );
Query OK, 0 rows affected (0.16 sec)
mysql> insert into teachers values(101,'Dharmendra
Kanchan','IT','Lecturer','male',75000.00);
Query OK, 1 row affected (0.06 sec)
mysql> insert into teachers values(103,'Dharam Das','ENG','Lecturer','male',35000.00);
Query OK, 1 row affected (0.03 sec)
mysql> insert into teachers values(104,'Anil Kumar','ENG','Reader','male',65000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into teachers values(105,'Mamta
Shukla','ENG','Professor','female',25000.00);
insert into teachers values(102,'Saurabh Srivastav','IT','Reader','male ',25000.00);
Query OK, 1 row affected (0.03 sec)
mysql> select * from teachers;
+-------+--------------------+------+-----------+--------+--------+
| tcode | tname | dept | post | gender | salary |
+-------+--------------------+------+-----------+--------+--------+
| 101 | Dharmendra Kanchan | IT | Lecturer | male | 75000 |
| 102 | Saurabh Srivastav | IT | Reader | male | 25000 |
| 103 | Dharam Das | ENG | Lecturer | male | 35000 |
| 104 | Anil Kumar | ENG | Reader | male | 65000 |
| 105 | Mamta Shukla | ENG | Professor | female | 25000 |
+-------+--------------------+------+-----------+--------+--------+
5 rows in set (0.00 sec)
mysql> select * from teachers where post = 'Reader';
+-------+--------------------+------+----------+--------+--------+
| tcode | tname | dept | post | gender | salary |
+-------+--------------------+------+----------+--------+--------+
| 102 | Saurabh Srivastav | IT | Reader | male | 25000 |
| 104 | Anil Kumar | ENG |Reader | male | 65000 |
+-------+--------------------+------+----------+--------+--------+
2 rows in set (0.02 sec)
mysql> select tname,dept from teachers where post = 'Reader';
+--------------------+------+
| tname | dept |
+--------------------+------+
| Saurabh Srivastav | IT |
| Anil Kumar | ENG |
+--------------------+------+
2 rows in set (0.01 sec)
mysql> select * from teachers where tname like 'Dhar%';
+-------+--------------------+------+----------+--------+--------+
| tcode | tname | dept | post | gender | salary |
+-------+--------------------+------+----------+--------+--------+
| 101 | Dharmendra Kanchan | IT | Lecturer | male | 75000 |
| 103 | Dharam Das | ENG | Lecturer | male | 35000 |
+-------+--------------------+------+----------+--------+--------+
2 rows in set (0.03 sec)
mysql> select * from teachers where salary >= 30000 and salary <= 70000;
+-------+------------+------+----------+--------+--------+
| tcode | tname | dept | post | gender | salary |
+-------+------------+------+----------+--------+--------+
| 103 | Dharam Das | ENG | Lecturer | male | 35000 |
| 104 | Anil Kumar | ENG | Reader | male | 65000 |
+-------+------------+------+----------+--------+--------+
2 rows in set (0.00 sec)
mysql> select count(*) from teachers;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from teachers where post = 'Lecturer';
+----------+
| count(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) as number from teachers where post = 'Lecturer';
+--------+
| number |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
mysql> select post from teachers;
+-----------+
| post |
+-----------+
| Lecturer |
| Reader |
| Lecturer |
| Reader |
| Professor |
+-----------+
5 rows in set (0.00 sec)
mysql> select distinct post from teachers;
+-----------+
| post |
+-----------+
| Lecturer |
| Reader |
| Professor |
+-----------+
3 rows in set (0.00 sec)