Class - 15 May
Class - 15 May
Note-
In our training session we will work with MySql.
MySQL : it is RDBMS
It very light weight and open source.
It provide the APIs to work with different programming languages like PHP, Python, Java,
….
How to use MySql : install Xampp server or Install MySql Client Program, OR MySQL
Workbench in your PC
Set path of mySql to your System Environment.
Start Xampp server
You will get the command prompt of mysql , where you can type the sql queries and
work with database.
Commands of MySql :
The SQL commands can be categorised in 4 types:
1. DDL (Data Definition Language) : these commands are related to creating the
structure or schema of the table/database.
Like - name of table, number of columns, type and size of each column, any
constraints in column.
Example of DDL : Create Database, Create Table, Alter Table, Drop Database, Drop
Table.
2. DML : (Data Manipulation Language) - it is the set of such commands which are
used to work with the values inside the table.
Like- insert any new record, delete record, updating record
Example : Insert into, Update , Delete From, Select
3. DCL :- Data Control Language : it is a set of sql commands which are used to
provide the control of different operation to different user.
Example : Grant, Revoke
4. TCL :- Transaction control language : it is a set of sql command which is used to
maintain the transactions atomicity and consistency.
Example : Save Point, Commit , Rollback
- First of all we have to create a database (folder) and inside the database we will
create multiple tables.
Creating database :
Create databse <name of database>;
Ex-
Create database training;
Open/Use Database:
Before creating tables we have to open the database.
Use training;
Creating table:
We have to specify the table name and the column structure for creating table
Column structure = column name + type + size
Some data types have fixed size like int, float, boolean
And some data types need the size to be specified like – char, varchar
Syntax:
Create table <name of table> (
<colname> <type> (<size>),
<colname> <type>(<size>),
…
);
- We can also use constraints with column name but we will discuss it separately.
Example :
MariaDB [(none)]> use training;
Database changed
MariaDB [training]> create table students(
-> rollno int,
-> name varchar(20),
-> age tinyint,
-> mobile varchar(12),
-> city varchar(20)
-> );
Query OK, 0 rows affected (0.061 sec)
MariaDB [training]> create table person(id int, name varchar(20), address varchar(50));
Query OK, 0 rows affected (0.047 sec)
MariaDB [training]>
Example :
MariaDB [training]> select * from students where city='indore';
+--------+--------+------+-----------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+-----------+--------+
| 106 | ramesh | 22 | 88994565 | indore |
| 107 | keshav | 20 | 998994565 | indore |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+-----------+--------+
3 rows in set (0.002 sec)
MariaDB [training]> select * from students where city ='indore' and age>20;
+--------+--------+------+-----------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+-----------+--------+
| 106 | ramesh | 22 | 88994565 | indore |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+-----------+--------+
2 rows in set (0.001 sec)
MariaDB [training]> select * from students where rollno between 101 and 105;
+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 105 | rajesh | 21 | 3399448855 | NULL |
+--------+--------+------+------------+--------+
3 rows in set (0.001 sec)
MariaDB [training]>