0% found this document useful (0 votes)
33 views53 pages

MySQL Database and Table Operations

The document details a series of MySQL commands executed in a database named 'd1', including creating a table 't1', inserting records, and performing various queries. It demonstrates operations such as updating records, altering table structure, and deleting data. The final commands showcase filtering data based on conditions using SQL queries.

Uploaded by

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

MySQL Database and Table Operations

The document details a series of MySQL commands executed in a database named 'd1', including creating a table 't1', inserting records, and performing various queries. It demonstrates operations such as updating records, altering table structure, and deleting data. The final commands showcase filtering data based on conditions using SQL queries.

Uploaded by

pp895578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Enter password: *****

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 1
Server version: 5.1.33-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database d1;


Query OK, 1 row affected (0.00 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| d1 |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> use d1;


Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table t1 (


-> id integer(5) PRIMARY KEY,
-> name char(10),
-> salary integer(8));
Query OK, 0 rows affected (0.11 sec)

mysql> show tables;


+--------------+
| Tables_in_d1 |
+--------------+
| t1 |
+--------------+
1 row in set (0.02 sec)

mysql> desc t1;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(5) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| salary | int(8) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> insert into t1 values(101,'ram',1000);


Query OK, 1 row affected (0.05 sec)

mysql> insert into t1 values(102,'ramu',2000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into t1 values(103,'ram',3000);


Query OK, 1 row affected (0.05 sec)
mysql> insert into t1 values(104,'shyam',3500);
Query OK, 1 row affected (0.05 sec)

mysql> insert into t1 values(105,'sonu',4500);


Query OK, 1 row affected (0.06 sec)

mysql> select * from t1;


+-----+-------+--------+
| id | name | salary |
+-----+-------+--------+
| 101 | ram | 1000 |
| 102 | ramu | 2000 |
| 103 | ram | 3000 |
| 104 | shyam | 3500 |
| 105 | sonu | 4500 |
+-----+-------+--------+
5 rows in set (0.00 sec)

mysql> alter table t1


-> add (city char(10));
Query OK, 5 rows affected (0.22 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+-----+-------+--------+------+
| id | name | salary | city |
+-----+-------+--------+------+
| 101 | ram | 1000 | NULL |
| 102 | ramu | 2000 | NULL |
| 103 | ram | 3000 | NULL |
| 104 | shyam | 3500 | NULL |
| 105 | sonu | 4500 | NULL |
+-----+-------+--------+------+
5 rows in set (0.00 sec)

mysql> update t1
-> set city="satna"
-> where id=101;
Query OK, 1 row affected (0.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t1;


+-----+-------+--------+-------+
| id | name | salary | city |
+-----+-------+--------+-------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | NULL |
| 103 | ram | 3000 | NULL |
| 104 | shyam | 3500 | NULL |
| 105 | sonu | 4500 | NULL |
+-----+-------+--------+-------+
5 rows in set (0.00 sec)

mysql> update t1
-> set city="satna";
Query OK, 4 rows affected (0.03 sec)
Rows matched: 5 Changed: 4 Warnings: 0

mysql> select * from t1;


+-----+-------+--------+-------+
| id | name | salary | city |
+-----+-------+--------+-------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | satna |
| 103 | ram | 3000 | satna |
| 104 | shyam | 3500 | satna |
| 105 | sonu | 4500 | satna |
+-----+-------+--------+-------+
5 rows in set (0.00 sec)
mysql> update t1
-> set city="rewa"
-> where name="ram";
Query OK, 2 rows affected (0.38 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> select * from t1;


+-----+-------+--------+-------+
| id | name | salary | city |
+-----+-------+--------+-------+
| 101 | ram | 1000 | rewa |
| 102 | ramu | 2000 | satna |
| 103 | ram | 3000 | rewa |
| 104 | shyam | 3500 | satna |
| 105 | sonu | 4500 | satna |
+-----+-------+--------+-------+
5 rows in set (0.00 sec)

mysql> desc t1;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(5) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| salary | int(8) | YES | | NULL | |
| city | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> alter table t1


-> modify(salary integer(10));
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
'(sala
ry integer(10))' at line 2
mysql> delete from t1
-> where id=104
-> ;
Query OK, 1 row affected (0.36 sec)

mysql> select * from t1;


+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 101 | ram | 1000 | rewa |
| 102 | ramu | 2000 | satna |
| 103 | ram | 3000 | rewa |
| 105 | sonu | 4500 | satna |
+-----+------+--------+-------+
4 rows in set (0.00 sec)

mysql> delete from t1;


Query OK, 4 rows affected (0.05 sec)

mysql> select * from t1;


Empty set (0.00 sec)

mysql> show tables;


+--------------+
| Tables_in_d1 |
+--------------+
| t1 |
+--------------+
1 row in set (0.02 sec)

mysql> desc t1;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(5) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| salary | int(8) | YES | | NULL | |
| city | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> drop table t1;


Query OK, 0 rows affected (0.08 sec)

mysql> select * from t1;


ERROR 1146 (42S02): Table 'd1.t1' doesn't exist
mysql> show tables;
Empty set (0.00 sec)

mysql> desc t1;


ERROR 1146 (42S02): Table 'd1.t1' doesn't exist
mysql>Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.33-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use d1;


Database changed
mysql> select * from t1;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select city,name from t1;


+----------+-------+
| city | name |
+----------+-------+
| satna | ram |
| rewa | ramu |
| jabalpur | shyam |
| satna | sonu |
| satna | ram |
+----------+-------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where salary>=3000;
+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+------+--------+-------+
2 rows in set (0.00 sec)

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where id>102 and salary<3500;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 103 | shyam | 2500 | jabalpur |
+-----+-------+--------+----------+
1 row in set (0.00 sec)

mysql> select * from t1


-> where not(id>102 and salary<3500);
+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+------+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from t1


-> where id>102 or salary<3500;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where not(id>102 or salary<3500);
Empty set (0.00 sec)

mysql> select * from t1


-> where salary between 2000 and 3500;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
4 rows in set (0.00 sec)

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where city in("satna");
+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 101 | ram | 1000 | satna |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+------+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from t1


-> where city not in("satna");
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
+-----+-------+--------+----------+
2 rows in set (0.00 sec)

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where name like 'r__';
+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 101 | ram | 1000 | satna |
| 105 | ram | 3500 | satna |
+-----+------+--------+-------+
2 rows in set (0.00 sec)

mysql> select * from t1


-> where name like 'r%';
+-----+------+--------+-------+
| id | name | salary | city |
+-----+------+--------+-------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 105 | ram | 3500 | satna |
+-----+------+--------+-------+
3 rows in set (0.00 sec)

mysql> select * from t1


-> where name like 'r___';
+-----+------+--------+------+
| id | name | salary | city |
+-----+------+--------+------+
| 102 | ramu | 2000 | rewa |
+-----+------+--------+------+
1 row in set (0.00 sec)

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> where name like 'r';
Empty set (0.00 sec)

mysql> select * from t1


-> where name like 'r';
Empty set (0.00 sec)

mysql> select * from t1


-> where name like '%a%';
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
4 rows in set (0.00 sec)
mysql> select name from t1;
+-------+
| name |
+-------+
| ram |
| ramu |
| shyam |
| sonu |
| ram |
+-------+
5 rows in set (0.00 sec)

mysql> select distinct name from t1;


+-------+
| name |
+-------+
| ram |
| ramu |
| shyam |
| sonu |
+-------+
4 rows in set (0.00 sec)
mysql> select * from t1;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> order by name;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 105 | ram | 3500 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from t1


-> order by name desc;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 104 | sonu | 3500 | satna |
| 103 | shyam | 2500 | jabalpur |
| 102 | ramu | 2000 | rewa |
| 101 | ram | 1000 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql>
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.33-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select char(97) "ASCII";
+-------+
| ASCII |
+-------+
|a |
+-------+
1 row in set (0.00 sec)

mysql> select concat("ram","shyam");


+-----------------------+
| concat("ram","shyam") |
+-----------------------+
| ramshyam |
+-----------------------+
1 row in set (0.00 sec)

mysql> select lcase("RAM");


+--------------+
| lcase("RAM") |
+--------------+
| ram |
+--------------+
1 row in set (0.00 sec)

mysql> select upper("ram");


+--------------+
| upper("ram") |
+--------------+
| RAM |
+--------------+
1 row in set (0.00 sec)

mysql> select ucase("ram");


+--------------+
| ucase("ram") |
+--------------+
| RAM |
+--------------+
1 row in set (0.00 sec)

mysql> select substr("Data care",5);


+-----------------------+
| substr("Data care",5) |
+-----------------------+
| care |
+-----------------------+
1 row in set (0.00 sec)

mysql> select substr("Data care",3,7);


+-------------------------+
| substr("Data care",3,7) |
+-------------------------+
| ta care |
+-------------------------+
1 row in set (0.00 sec)

mysql> select substr("Data care",3,3);


+-------------------------+
| substr("Data care",3,3) |
+-------------------------+
| ta |
+-------------------------+
1 row in set (0.00 sec)

mysql> select substr("Data care",3,5);


+-------------------------+
| substr("Data care",3,5) |
+-------------------------+
| ta ca |
+-------------------------+
1 row in set (0.00 sec)

mysql> select substr("Data care",3);


+-----------------------+
| substr("Data care",3) |
+-----------------------+
| ta care |
+-----------------------+
1 row in set (0.00 sec)

mysql> select (" Data care");


+----------------+
| Data care |
+----------------+
| Data care |
+----------------+
1 row in set (0.00 sec)

mysql> select ltrim(" Data care");


+-------------------------+
| ltrim(" Data care") |
+-------------------------+
| Data care |
+-------------------------+
1 row in set (0.00 sec)

mysql> select ("datacare ");


+---------------+
| datacare |
+---------------+
| datacare |
+---------------+
1 row in set (0.00 sec)

mysql> select rtrim("datacare ");


+------------------------+
| rtrim("datacare ") |
+------------------------+
| datacare |
+------------------------+
1 row in set (0.00 sec)

mysql> select trim(" datacare ");


+---------------------------------+
| trim(" datacare ") |
+---------------------------------+
| datacare |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select insert("this is a book","is");


ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
')' at
line 1
mysql> select instr("this is a book","is");
+------------------------------+
| instr("this is a book","is") |
+------------------------------+
| 3|
+------------------------------+
1 row in set (0.00 sec)

mysql> select length("this is a book");


+--------------------------+
| length("this is a book") |
+--------------------------+
| 14 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select left("this is a book",4);


+--------------------------+
| left("this is a book",4) |
+--------------------------+
| this |
+--------------------------+
1 row in set (0.00 sec)

mysql> select right("this is a book",4);


+---------------------------+
| right("this is a book",4) |
+---------------------------+
| book |
+---------------------------+
1 row in set (0.00 sec)

mysql> select mid("this is a book",4,3);


+---------------------------+
| mid("this is a book",4,3) |
+---------------------------+
|si |
+---------------------------+
1 row in set (0.01 sec)

mysql> select mod(10,3);


+-----------+
| mod(10,3) |
+-----------+
| 1|
+-----------+
1 row in set (0.00 sec)

mysql> select pow(10,3);


+-----------+
| pow(10,3) |
+-----------+
| 1000 |
+-----------+
1 row in set (0.00 sec)

mysql> select round(10.658,2);


+-----------------+
| round(10.658,2) |
+-----------------+
| 10.66 |
+-----------------+
1 row in set (0.00 sec)

mysql> select round(10.653,2);


+-----------------+
| round(10.653,2) |
+-----------------+
| 10.65 |
+-----------------+
1 row in set (0.00 sec)

mysql> select truncate(10.653,2);


+--------------------+
| truncate(10.653,2) |
+--------------------+
| 10.65 |
+--------------------+
1 row in set (0.00 sec)
mysql> select truncate(10.659,2);
+--------------------+
| truncate(10.659,2) |
+--------------------+
| 10.65 |
+--------------------+
1 row in set (0.00 sec)

mysql> select truncate(10.999,2);


+--------------------+
| truncate(10.999,2) |
+--------------------+
| 10.99 |
+--------------------+
1 row in set (0.00 sec)

mysql> select sqrt(25);


+----------+
| sqrt(25) |
+----------+
| 5|
+----------+
1 row in set (0.00 sec)
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2016-10-02 |
+------------+
1 row in set (0.00 sec)

mysql> select date();


ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
')' at
line 1
mysql> select date('2016-12-12');
+--------------------+
| date('2016-12-12') |
+--------------------+
| 2016-12-12 |
+--------------------+
1 row in set (0.00 sec)
mysql> select month('2016-10-12');
+---------------------+
| month('2016-10-12') |
+---------------------+
| 10 |
+---------------------+
1 row in set (0.00 sec)

mysql> select year('2016-10-12');


+--------------------+
| year('2016-10-12') |
+--------------------+
| 2016 |
+--------------------+
1 row in set (0.00 sec)

mysql> select datname('2016-10-12');


ERROR 1305 (42000): FUNCTION [Link] does not exist
mysql> select dayname('2016-10-12');
+-----------------------+
| dayname('2016-10-12') |
+-----------------------+
| Wednesday |
+-----------------------+
1 row in set (0.02 sec)

mysql> select dayofmonth('2016-10-12');


+--------------------------+
| dayofmonth('2016-10-12') |
+--------------------------+
| 12 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select dayofweek('2016-10-12');


+-------------------------+
| dayofweek('2016-10-12') |
+-------------------------+
| 4|
+-------------------------+
1 row in set (0.00 sec)

mysql> select dayofyear('2016-10-12');


+-------------------------+
| dayofyear('2016-10-12') |
+-------------------------+
| 286 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select now();


+---------------------+
| now() |
+---------------------+
| 2016-10-02 [Link] |
+---------------------+
1 row in set (0.00 sec)

mysql> select sysdate();


+---------------------+
| sysdate() |
+---------------------+
| 2016-10-02 [Link] |
+---------------------+
1 row in set (0.00 sec)

mysql>| id | name | salary | city |


+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
| 106 | ram | NULL | satna |
+-----+-------+--------+----------+
6 rows in set (0.00 sec)

mysql> select count('name') from t1;


+---------------+
| count('name') |
+---------------+
| 6|
+---------------+
1 row in set (0.00 sec)

mysql> select count(salary) from t1;


+---------------+
| count(salary) |
+---------------+
| 5|
+---------------+
1 row in set (0.00 sec)
mysql> select sum(salary) "salary" from t1;
+--------+
| salary |
+--------+
| 12500 |
+--------+
1 row in set (0.00 sec)

mysql> select id,name,salary+500,city from t1;


+-----+-------+------------+----------+
| id | name | salary+500 | city |
+-----+-------+------------+----------+
| 101 | ram | 1500 | satna |
| 102 | ramu | 2500 | rewa |
| 103 | shyam | 3000 | jabalpur |
| 104 | sonu | 4000 | satna |
| 105 | ram | 4000 | satna |
| 106 | ram | NULL | satna |
+-----+-------+------------+----------+
6 rows in set (0.00 sec)

mysql> select id,name,salary+500 "Salary",city from t1;


+-----+-------+--------+----------+
| id | name | Salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1500 | satna |
| 102 | ramu | 2500 | rewa |
| 103 | shyam | 3000 | jabalpur |
| 104 | sonu | 4000 | satna |
| 105 | ram | 4000 | satna |
| 106 | ram | NULL | satna |
+-----+-------+--------+----------+
6 rows in set (0.00 sec)

mysql> select id "Emp_Id",name,salary+500 "Salary",city from t1;


+--------+-------+--------+----------+
| Emp_Id | name | Salary | city |
+--------+-------+--------+----------+
| 101 | ram | 1500 | satna |
| 102 | ramu | 2500 | rewa |
| 103 | shyam | 3000 | jabalpur |
| 104 | sonu | 4000 | satna |
| 105 | ram | 4000 | satna |
| 106 | ram | NULL | satna |
+--------+-------+--------+----------+
6 rows in set (0.00 sec)

mysql> select sum(distinct salary) "salary" from t1;


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

mysql> select sign(-10);


+-----------+
| sign(-10) |
+-----------+
| -1 |
+-----------+
1 row in set (0.00 sec)

mysql> select sign(10);


+----------+
| sign(10) |
+----------+
| 1|
+----------+
1 row in set (0.00 sec)

mysql> select min(salary) "salary" from t1;


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

mysql> select max(salary) "salary" from t1;


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

mysql> select avg(salary) "salary" from t1;


+-----------+
| salary |
+-----------+
| 2500.0000 |
+-----------+
1 row in set (0.01 sec)

mysql> select avg(distinct salary) "salary" from t1;


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

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
| 106 | ram | NULL | satna |
+-----+-------+--------+----------+
6 rows in set (0.00 sec)

mysql> delete from t1


-> where id=106;
Query OK, 1 row affected (0.36 sec)

mysql> select * from t1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select name,sum(salary) "Salary"


-> from t1
-> group by name;
+-------+--------+
| name | Salary |
+-------+--------+
| ram | 4500 |
| ramu | 2000 |
| shyam | 2500 |
| sonu | 3500 |
+-------+--------+
4 rows in set (0.01 sec)

mysql> select name,sum(salary) "Salary"


-> from t1
-> group by name
-> having Salary>2000;
+-------+--------+
| name | Salary |
+-------+--------+
| ram | 4500 |
| shyam | 2500 |
| sonu | 3500 |
+-------+--------+
3 rows in set (0.00 sec)

mysql> create table p1(


-> id integer(5),
-> name char(10));
Query OK, 0 rows affected (0.39 sec)

mysql> create table p1(


-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
'' at
line 1
mysql> create table p2(
-> id integer(5),
-> city char(10));
Query OK, 0 rows affected (0.09 sec)

mysql> insert into p1 values(1,'ram');


Query OK, 1 row affected (0.05 sec)

mysql> insert into p1 values(2,'ramu');


Query OK, 1 row affected (0.05 sec)

mysql> insert into p1 values(3,'shyam');


Query OK, 1 row affected (0.05 sec)
mysql> insert into p2 values(3,'satna');
Query OK, 1 row affected (0.06 sec)

mysql> insert into p2 values(4,'rewa');


Query OK, 1 row affected (0.06 sec)

mysql> insert into p2 values(5,'jabalpur');


Query OK, 1 row affected (0.03 sec)

mysql> select * from p1;


+------+-------+
| id | name |
+------+-------+
| 1 | ram |
| 2 | ramu |
| 3 | shyam |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from p2;


+------+----------+
| id | city |
+------+----------+
| 3 | satna |
| 4 | rewa |
| 5 | jabalpur |
+------+----------+
3 rows in set (0.00 sec)

mysql> NON EQUI JOIN;


ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
'NON E
QUI JOIN' at line 1
mysql> select * frim p1,p2;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
'frim
p1,p2' at line 1
mysql> select * from p1,p2;
+------+-------+------+----------+
| id | name | id | city |
+------+-------+------+----------+
| 1 | ram | 3 | satna |
| 2 | ramu | 3 | satna |
| 3 | shyam | 3 | satna |
| 1 | ram | 4 | rewa |
| 2 | ramu | 4 | rewa |
| 3 | shyam | 4 | rewa |
| 1 | ram | 5 | jabalpur |
| 2 | ramu | 5 | jabalpur |
| 3 | shyam | 5 | jabalpur |
+------+-------+------+----------+
9 rows in set (0.00 sec)

mysql> select * from p2,p1;


+------+----------+------+-------+
| id | city | id | name |
+------+----------+------+-------+
| 3 | satna | 1 | ram |
| 4 | rewa | 1 | ram |
| 5 | jabalpur | 1 | ram |
| 3 | satna | 2 | ramu |
| 4 | rewa | 2 | ramu |
| 5 | jabalpur | 2 | ramu |
| 3 | satna | 3 | shyam |
| 4 | rewa | 3 | shyam |
| 5 | jabalpur | 3 | shyam |
+------+----------+------+-------+
9 rows in set (0.00 sec)

mysql> Equi join;


ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near
'Equi
join' at line 1
mysql> select * from p1,p2
-> where [Link]=[Link];
+------+-------+------+-------+
| id | name | id | city |
+------+-------+------+-------+
| 3 | shyam | 3 | satna |
+------+-------+------+-------+
1 row in set (0.00 sec)

mysql> select * from p1 natural join p2;


+------+-------+-------+
| id | name | city |
+------+-------+-------+
| 3 | shyam | satna |
+------+-------+-------+
1 row in set (0.00 sec)

Enter password: *****


Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.33-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use d1;


Database changed
mysql> create view v1 as select * from t1;
Query OK, 0 rows affected (0.06 sec)

mysql> select * from t1


-> ;
+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.00 sec)

mysql> select * from v1;


+-----+-------+--------+----------+
| id | name | salary | city |
+-----+-------+--------+----------+
| 101 | ram | 1000 | satna |
| 102 | ramu | 2000 | rewa |
| 103 | shyam | 2500 | jabalpur |
| 104 | sonu | 3500 | satna |
| 105 | ram | 3500 | satna |
+-----+-------+--------+----------+
5 rows in set (0.08 sec)

mysql> delete from t1;


Query OK, 5 rows affected (0.05 sec)

mysql> ;
ERROR:
No query specified

mysql> select * from t1;


Empty set (0.00 sec)

mysql> select * from v1;


Empty set (0.00 sec)

mysql> show tables;


+--------------+
| Tables_in_d1 |
+--------------+
| p1 |
| p2 |
| t1 |
| v1 |
+--------------+
4 rows in set (0.03 sec)

mysql> desc v1;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(5) | NO | | NULL | |
| name | char(10) | YES | | NULL | |
| salary | int(8) | YES | | NULL | |
| city | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> drop database d1;


Query OK, 4 rows affected (0.17 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.02 sec)

You might also like