0% found this document useful (0 votes)
3 views

Table creation (1)

The document outlines the creation and manipulation of a MySQL database and tables, specifically for students and employees. It includes commands for creating tables, adding and modifying columns, inserting data, updating records, deleting entries, and performing queries with ordering and pattern matching. Additionally, it demonstrates the use of built-in functions for aggregate calculations on employee salaries.

Uploaded by

sakthibalan107
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Table creation (1)

The document outlines the creation and manipulation of a MySQL database and tables, specifically for students and employees. It includes commands for creating tables, adding and modifying columns, inserting data, updating records, deleting entries, and performing queries with ordering and pattern matching. Additionally, it demonstrates the use of built-in functions for aggregate calculations on employee salaries.

Uploaded by

sakthibalan107
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Table creation

DDL:
mysql> create database student;
Query OK, 1 row affected(0.00 sec)

mysql> use student;


Database changed

mysql> create table stud1(Regno char(10)primary key,Name char(20) not null,Class char(10),
DOB date,Gender char(10),fees int check(fees>1000));
Query OK, 0 rows affected (0.22 sec)

ADD COLUMN

mysql> alter table stud1 add address varchar(50);


Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> describe stud1;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Regno | char(10) | NO | PRI | NULL | |
| Name | char(20) | NO | | NULL | |
| Class | char(10) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| Gender | char(10) | YES | | NULL | |
| fees | int(11) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.08 sec)

MODIFY THE SIZE AND DATA TYPE


mysql> alter table stud1 modify regno int(20);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> describe stud1;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| regno | int(20) | NO | PRI | 0 | |
| Name | char(20) | NO | | NULL | |
| Class | char(10) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| Gender | char(10) | YES | | NULL | |
| fees | int(11) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
DROP COLUMN
mysql> alter table stud1 drop address;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> describe stud1;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| regno | int(20) | NO | PRI | 0 | |
| Name | char(20) | NO | | NULL | |
| Class | char(10) | YES | | NULL | |
| DOB | date | YES | | NULL | |
| Gender | char(10) | YES | | NULL | |
| fees | int(11) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
6 rows in set (0.00 sec)

DROP TABLE
mysql> drop table stud1;
Query OK, 0 rows affected (0.05 sec)

mysql> describe stud1;


ERROR 1146 (42S02): Table 'student.stud1' doesn't exist

DML LANGUAGE
TABLE CREATION AND INSERT VALUES
mysql> create table employee(sno int,empid int primary key,name varchar(20),department varchar(20),salary
int);
Query OK, 0 rows affected (0.07 sec)
mysql>insert into employee values(1,1001,"ram","cs",20000);
Query OK, 1 row affected (0.05 sec)

mysql> insert into employee values(2,1002,"sam","cs",30000);


Query OK, 1 row affected (0.06 sec)

mysql> insert into employee values(3,1003,"kavi","bca",30500);


Query OK, 1 row affected (0.06 sec)

mysql> insert into employee values(4,1004,"tamilselvi","bcom ca",25000);


Query OK, 1 row affected (0.06 sec)

mysql> insert into employee values(5,1005,"ramya","mba",50000);


Query OK, 1 row affected (0.09 sec)

mysql> select * from employee;


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 1 | 1001 | ram | cs | 20000 |
| 2 | 1002 | sam | cs | 30000 |
| 3 | 1003 | kavi | bca | 30500 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
| 5 | 1005 | ramya | mba | 50000 |
+------+-------+------------+------------+--------+
5 rows in set (0.00 sec)

UPDATE COMMAND

mysql> update employee set department="bba" where empid=1005;


Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from employee;


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 1 | 1001 | ram | cs | 20000 |
| 2 | 1002 | sam | cs | 30000 |
| 3 | 1003 | kavi | bca | 30500 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
| 5 | 1005 | ramya | bba | 50000 |
+------+-------+------------+------------+--------+
5 rows in set (0.00 sec)

DATA DELETION
mysql> delete from employee where name="ramya";
Query OK, 1 row affected (0.10 sec)

mysql> select * from employee;


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 1 | 1001 | ram | cs | 20000 |
| 2 | 1002 | sam | cs | 30000 |
| 3 | 1003 | kavi | bca | 30500 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
+------+-------+------------+------------+--------+
4 rows in set (0.00 sec)

ORDERING TUPELS

mysql> select * from employee order by name asc;


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 3 | 1003 | kavi | bca | 30500 |
| 1 | 1001 | ram | cs | 20000 |
| 2 | 1002 | sam | cs | 30000 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
+------+-------+------------+------------+--------+
4 rows in set (0.01 sec)
mysql> select * from employee order by name desc;
+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
| 2 | 1002 | sam | cs | 30000 |
| 1 | 1001 | ram | cs | 20000 |
| 3 | 1003 | kavi | bca | 30500 |
+------+-------+------------+------------+--------+
4 rows in set (0.00 sec)

TUPLE VARIABLE
mysql> select a.name,a.department from employee as a;
+------------+------------+
| name | department |
+------------+------------+
| ram | cs |
| sam | cs |
| kavi | bca |
| tamilselvi | bcom ca |
+------------+------------+
4 rows in set (0.01 sec)

PATTERN MATCHING
mysql> select * from employee where name like's%';
+------+-------+------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------+------------+--------+
| 2 | 1002 | sam | cs | 30000 |
+------+-------+------+------------+--------+
1 row in set (0.00 sec)

mysql> select * from employee where name like'%i';


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 3 | 1003 | kavi | bca | 30500 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
+------+-------+------------+------------+--------+
2 rows in set (0.00 sec)

mysql> select * from employee where name like'_i%';


Empty set (0.00 sec)

mysql> select * from employee where name like'_a%';


+------+-------+------------+------------+--------+
| sno | empid | name | department | salary |
+------+-------+------------+------------+--------+
| 1 | 1001 | ram | cs | 20000 |
| 2 | 1002 | sam | cs | 30000 |
| 3 | 1003 | kavi | bca | 30500 |
| 4 | 1004 | tamilselvi | bcom ca | 25000 |
+------+-------+------------+------------+--------+
4 rows in set (0.00 sec)

BUILT IN FUNCTIONS

mysql> select sum(salary) from employee;


+-------------+
| sum(salary) |
+-------------+
| 105500 |
+-------------+
1 row in set (0.04 sec)

mysql> select avg(salary) from employee;


+-------------+
| avg(salary) |
+-------------+
| 26375.0000 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(*) from employee;


+----------+
| count(*) |
+----------+
| 4|
+----------+
1 row in set (0.00 sec)

mysql> select max(salary) from employee;


+-------------+
| max(salary) |
+-------------+
| 30500 |
+-------------+
1 row in set (0.00 sec)

mysql> select min(salary) from employee;


+-------------+
| min(salary) |
+-------------+
| 20000 |
+-------------+
1 row in set (0.00 sec)

mysql> select upper(name)from employee;


+-------------+
| upper(name) |
+-------------+
| RAM |
| SAM |
| KAVI |
| TAMILSELVI |
+-------------+
4 rows in set (0.03 sec)

SET OPERATIONS

You might also like