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

Class - 15 May

Uploaded by

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

Class - 15 May

Uploaded by

Vikas Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Working with Database :

Database is a collection of inter-related data in the form the tables


It provides the centralize management of data and enable the sharing of data b/w multiple
websites/ application.
To manage and control the different operations over the data a special software is used
called as RDBMS (Relational database management system)
RDBMS provides the commands for creating, editing , searching, modifying the database
as well as to control the Transactions.
There are many RDBMS like : MySql, Oracle, Sqlite, Dbase, MS-Sql Server ,….
The common thing among all the RDBMS is that they use SQL (Structured Query
Language) for working with data.

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

Open command command prompt and type :


 Mysql –u root

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

Working with sql command :

- First of all we have to create a database (folder) and inside the database we will
create multiple tables.

 Show databases : - to show the available databases

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

Some common types:


Int
Tiny int
Medium int
Big int
Float
Double
Char /varchar
Date
Date time
Blob : for binary files like : file, image,

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]> show tables;


+--------------------+
| Tables_in_training |
+--------------------+
| students |
+--------------------+
1 row in set (0.000 sec)

MariaDB [training]> create table person(id int, name varchar(20), address varchar(50));
Query OK, 0 rows affected (0.047 sec)

MariaDB [training]> show tables;


+--------------------+
| Tables_in_training |
+--------------------+
| person |
| students |
+--------------------+
2 rows in set (0.000 sec)

MariaDB [training]>

Inserting Record inside Table :


Insert into <tableName> (<list of columns>) values(<list of values for each column>);
Note-
If we don’t specify the column name and it’s value then mysql filled it by - NULL
If we specify column name but don’t put the value, then it is Error.

MariaDB [training]> insert into students(rollno,name, age, mobile,city) values(101,


'ramesh',22,'8899448855', 'bhopal');
Query OK, 1 row affected (0.062 sec)

MariaDB [training]> insert into students(name,rollno, age, mobile,city)


values('suresh',102,22,'8899448855', 'bhopal');
Query OK, 1 row affected (0.006 sec)

MariaDB [training]> insert into students(name,rollno, age, mobile,city)


values(103,'kamlesh',22,'8899448855', 'bhopal');
Query OK, 1 row affected, 1 warning (0.006 sec)

MariaDB [training]> select * from students;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
+--------+--------+------+------------+--------+
3 rows in set (0.001 sec)

MariaDB [training]> insert into students(name,rollno, age, mobile,city)


values('rajesh',105,21,'3399448855');
ERROR 1136 (21S01): Column count doesn't match value count at row 1

MariaDB [training]> insert into students(name,rollno, age, mobile)


values('rajesh',105,21,'3399448855');
Query OK, 1 row affected (0.007 sec)

MariaDB [training]> select * from students;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
| 105 | rajesh | 21 | 3399448855 | NULL |
+--------+--------+------+------------+--------+
4 rows in set (0.001 sec)

Show the records of table:


1. Select * from <table name>
- Here * specify that all columns will be displayed
2. Select <list of columns> from <table name>

MariaDB [training]> select * from students;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
| 105 | rajesh | 21 | 3399448855 | NULL |
+--------+--------+------+------------+--------+
4 rows in set (0.000 sec)

MariaDB [training]> select name, mobile from students;


+--------+------------+
| name | mobile |
+--------+------------+
| ramesh | 8899448855 |
| suresh | 8899448855 |
| 103 | 8899448855 |
| rajesh | 3399448855 |
+--------+------------+
4 rows in set (0.000 sec)

MariaDB [training]> select name, rollno, age from students;


+--------+--------+------+
| name | rollno | age |
+--------+--------+------+
| ramesh | 101 | 22 |
| suresh | 102 | 22 |
| 103 | 0 | 22 |
| rajesh | 105 | 21 |
+--------+--------+------+
4 rows in set (0.000 sec)
MariaDB [training]>
3. Filter records : (searching record) :
To find / filter rows , we have to specify a condition with select statement.
Condition will be specified over the value of column
We use where clause with select statement after <table name>

Select * from <table name> where condition>;

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 rollno=101;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
+--------+--------+------+------------+--------+
1 row in set (0.002 sec)

MariaDB [training]> select name, city from students where rollno=101;


+--------+--------+
| name | city |
+--------+--------+
| ramesh | bhopal |
+--------+--------+
1 row in set (0.000 sec)

MariaDB [training]> select * from students where age>20;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
| 105 | rajesh | 21 | 3399448855 | NULL |
| 106 | ramesh | 22 | 88994565 | indore |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+------------+--------+
6 rows in set (0.002 sec)

MariaDB [training]> select * from students where city!='bhopal';


+--------+--------+------+-----------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+-----------+--------+
| 106 | ramesh | 22 | 88994565 | indore |
| 107 | keshav | 20 | 998994565 | indore |
| 108 | kavita | 20 | 998994565 | rewa |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+-----------+--------+
4 rows in set (0.001 sec)

MariaDB [training]> select * from students where city = NULL;


Empty set (0.001 sec)

MariaDB [training]> select * from students where city is NULL;


+--------+--------+------+------------+------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+------+
| 105 | rajesh | 21 | 3399448855 | NULL |
| 109 | kavita | 20 | 998994565 | NULL |
+--------+--------+------+------------+------+
2 rows in set (0.001 sec)

MariaDB [training]> select * from students where city is not NULL;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
| 106 | ramesh | 22 | 88994565 | indore |
| 107 | keshav | 20 | 998994565 | indore |
| 108 | kavita | 20 | 998994565 | rewa |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+------------+--------+
7 rows in set (0.000 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 city ='indore' or age>20;


+--------+--------+------+------------+--------+
| rollno | name | age | mobile | city |
+--------+--------+------+------------+--------+
| 101 | ramesh | 22 | 8899448855 | bhopal |
| 102 | suresh | 22 | 8899448855 | bhopal |
| 0 | 103 | 22 | 8899448855 | bhopal |
| 105 | rajesh | 21 | 3399448855 | NULL |
| 106 | ramesh | 22 | 88994565 | indore |
| 107 | keshav | 20 | 998994565 | indore |
| 110 | ram | 23 | 998994456 | indore |
+--------+--------+------+------------+--------+
7 rows in set (0.000 sec)

MariaDB [training]> select * from students where rollno>=101 and rollno<=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.000 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]> select city from students;


+--------+
| city |
+--------+
| bhopal |
| bhopal |
| bhopal |
| NULL |
| indore |
| indore |
| rewa |
| NULL |
| indore |
+--------+
9 rows in set (0.000 sec)
MariaDB [training]> select distinct(city) from students;
+--------+
| city |
+--------+
| bhopal |
| NULL |
| indore |
| rewa |
+--------+
4 rows in set (0.000 sec)

MariaDB [training]>

You might also like