Mysql Record File
Mysql Record File
+--------------------+
| Database |
+--------------------+
| carbrands |
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
Create a new database using SQL command "CREATE DATABASE databaseName"; and delete a
Database changed
+-------------+
| database() |
+-------------+
| automobiles |
+-------------+
+--------------------+
| Database |
+--------------------+
| automobiles |
| carbrands |
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
mysql> create table customer(custid char(5) primary key, car_name varchar(25), engine_id
varchar(10), purchase_year int);
+-----------------------+
| Tables_in_automobiles |
+-----------------------+
| customer |
+-----------------------+
+---------------+-------------+------+-----+---------+-------+
+---------------+-------------+------+-----+---------+-------+
mysql> insert into customer values('C02', 'Mahindra Scorpio-N', '75POY61945', 2022), ('C03', 'Toyota
Fortuner', '86TOZ71464', 2021), ('C04', 'Mahindra Thar', '70QRA82590', 2021), ('C05', 'Hyundai i20',
'30BXP62481', 2018), ('C06', 'Toyota Yaris', '51XCF52945', 2019), ('C07', 'Honda Jazz', '26PYI62012',
2020), ('C08', 'Mahindra XUV700', '70QPC96101', 2022), ('C09', 'Hyundai Creta', '45CXA75019',
2022), ('C10', 'Toyota Innova Crysta', '82TOX81965', 2022);
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+
| custid | car_name |
+--------+----------------------+
+--------+----------------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+---------+
| 100+400 |
+---------+
| 500 |
+---------+
+---------------------+
| now() |
+---------------------+
| 2022-10-19 18:04:36 |
+---------------------+
+---------+------------+
| 100+400 | power(2,3) |
+---------+------------+
| 500 | 8|
+---------+------------+
Comparison operators:
Selecting the car name and customer id where year of purchase is greater than 2018:
+----------------------+--------+
| car_name | custid |
+----------------------+--------+
+----------------------+--------+
+------------+--------+
| car_name | custid |
+------------+--------+
+------------+--------+
1 row in set (0.00 sec)
+--------+--------------------+------------+---------------+
+--------+--------------------+------------+---------------+
+--------+--------------------+------------+---------------+
+--------+---------------+------------+---------------+
+--------+---------------+------------+---------------+
+--------+---------------+------------+---------------+
mysql> select * from customer where car_name like 'H__%' and purchase_year>2020;
+--------+---------------+------------+---------------+
+--------+---------------+------------+---------------+
+--------+---------------+------------+---------------+
mysql> select * from customer where not(car_name like 'H__%' and purchase_year>2020);
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |
+--------+----------------------+------------+---------------+
mysql> select * from customer where purchase_year between 2019 and 2021;
+--------+-----------------+------------+---------------+
+--------+-----------------+------------+---------------+
+--------+-----------------+------------+---------------+
+--------+------------+-----------+---------------+
+--------+------------+-----------+---------------+
+--------+------------+-----------+---------------+
ORDER BY Commands:
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
mysql> select custid as id, car_name as car, engine_id as engine, purchase_year as purchase from
customer;
+-----+----------------------+------------+----------+
+-----+----------------------+------------+----------+
+-----+----------------------+------------+----------+
Concat function:
+---------------------------------+--------+
| cars | custid |
+---------------------------------+--------+
| NULL | C11 |
+---------------------------------+--------+
Group by function:
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
Count(*) function:
+-------+
| total |
+-------+
| 10 |
+-------+
Having Command:
+--------+----------------------+------------+---------------+
+--------+----------------------+------------+---------------+
mysql> create table car(engine_id varchar(10) primary key, car_name varchar(25), service_date
varchar(12), ticket_no varchar(5));
+--------------+-------------+------+-----+---------+-------+
+--------------+-------------+------+-----+---------+-------+
+--------------+-------------+------+-----+---------+-------+
Alter command and joining the two tables using the foreign key :
+--------+----------------------+----------+
+--------+----------------------+----------+
+--------+----------------------+----------+
create table workers(id int, name varchar(20), dob DATE, primary key(id));
insert into workers values(101, 'Kumar', '1976-07-05'), (102, 'Rahul', '1990-10-09'), (103, 'Harsh',
'1984-11-10');
mysql> select * from workers where dob between '1984-05-07' and '1991-08-06';
+-----+-------+------------+
| id | name | dob |
+-----+-------+------------+
+-----+-------+------------+
| id | name | dob |
+-----+-------+------------+
+-----+-------+------------+
+-------------+--------------+------------+-------------+---------------+---------------+
+-------------+--------------+------------+-------------+---------------+---------------+
| 2022 | 10 | 19 | 20 | 15 | 49 |
+-------------+--------------+------------+-------------+---------------+---------------+
mysql> insert into acc values ('Ravi', 19000), ('Aarush', 20000), ('Neha', 27000);
+--------+----------+
| name | balance |
+--------+----------+
| Ravi | 19000.00 |
| Aarush | 20000.00 |
| Neha | 27000.00 |
+--------+----------+
3 rows in set (0.00 sec)
Inner join:
mysql> insert into table1 values(1, 'Hyderabad'), (2, 'Delhi'), (3, 'Bangalore'), (4, 'Mumbai');
mysql> insert into table2 values(3, 'Rajasthan'), (4, 'Gujarat'), (5, 'Goa');
+-----+-----------+-----+-----------+
| sno | x | sno | x |
+-----+-----------+-----+-----------+
| 1 | Hyderabad | 5 | Goa |
| 1 | Hyderabad | 4 | Gujarat |
| 1 | Hyderabad | 3 | Rajasthan |
| 2 | Delhi | 5 | Goa |
| 2 | Delhi | 4 | Gujarat |
| 2 | Delhi | 3 | Rajasthan |
| 3 | Bangalore | 5 | Goa |
| 3 | Bangalore | 4 | Gujarat |
| 3 | Bangalore | 3 | Rajasthan |
| 4 | Mumbai | 5 | Goa |
| 4 | Mumbai | 4 | Gujarat |
| 4 | Mumbai | 3 | Rajasthan |
+-----+-----------+-----+-----------+
+-----+-----------+-----------+
| sno | x |x |
+-----+-----------+-----------+
| 3 | Bangalore | Rajasthan |
| 4 | Mumbai | Gujarat |
+-----+-----------+-----------+
+-----+-----------+------+-----------+
| sno | x | sno | x |
+-----+-----------+------+-----------+
| 3 | Bangalore | 3 | Rajasthan |
| 4 | Mumbai | 4 | Gujarat |
+-----+-----------+------+-----------+
+-----------+-----+------+
|x | sno | sno |
+-----------+-----+------+
| Hyderabad | 1 | NULL |
| Delhi | 2 | NULL |
| Bangalore | 3 | NULL |
| Mumbai | 4 | NULL |
+-----------+-----+------+
+-----+-----------+-----------+
| sno | x |x |
+-----+-----------+-----------+
| 1 | Hyderabad | NULL |
| 2 | Delhi | NULL |
| 3 | Bangalore | Rajasthan |
| 4 | Mumbai | Gujarat |
+-----+-----------+-----------+
+-----+-----------+-----------+
| sno | x |x |
+-----+-----------+-----------+
| 3 | Rajasthan | Bangalore |
| 4 | Gujarat | Mumbai |
| 5 | Goa | NULL |
+-----+-----------+-----------+
+-----------+-----+------+
|x | sno | sno |
+-----------+-----+------+
| Rajasthan | 3 | NULL |
| Gujarat | 4 | NULL |
| Goa | 5 | NULL |
+-----------+-----+------+
+------+-----------+-----+-----------+
| sno | x | sno | x |
+------+-----------+-----+-----------+
| 3 | Bangalore | 3 | Rajasthan |
| 4 | Mumbai | 4 | Gujarat |
+------+-----------+-----+-----------+