Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

RDBMS and MySQL

MySQL – Part 1
Introduction to MySQL, SQL Statements, Data types, Basic DDL Commands

Introduction to SQL and MySQL

SQL (Structured Query Language) is a simple query language used for accessing and
manipulating data in relational databases. It is a standard language that can be used with
any RDBMS to create, retrieve and modify data stored in a database.
It is the standard query language used in most RDBMS like MySQL, MS Access, etc.

MySQL is an open-source RDBMS that uses SQL. It provides excellent features for storing,
manipulating and retrieving data stored in relational databases. It can be freely downloaded
and installed from the site www.mysql.org.

Classification of SQL statements

SQL provides various statements to interact with the database. These statements can be
classified into four categories :
1. Data Definition Language (DDL) – These are a set of statements that allow you to
create, modify and remove tables and databases in MySQL. Some of the statements
in this category are Create Table, Create Database, Alter Table, Drop Table, Drop
Database, Create View, etc.
2. Data Manipulation Language (DML) – These are a set of statements that allow you
to create, modify and remove data in tables. Some of the statements in this category
are Select, Insert Into, Update, Delete, etc.
3. * Data Control Language (DCL) – These are statements that allow users access
control over the data in a database. Statements like Grant, Revoke, etc are part of
DCL statements.
4. * Transaction Control Language (TCL) – These are statements that allow you to
create and operate transactions in a database. A transaction is a set of statements
executed as a single batch. Statements like Set Transaction, Commit, Rollback, etc
are part of TCL statements.
Note : * DCL and TCL are not in syllabus. Syllabus covers only DDL and DML commands

SQL data types

Like any programming language, SQL provides various data types to store different types of
data. These data types can be classified into the following categories :
1. Character data types – These are data types that can handle character data and are
of the following types :

Computer Science with Python Class XII Page | 1 © Reeba John, Computer PGT, MTPS
RDBMS and MySQL

a) char(n) – allows to store data of character type of fixed size n. For eg. char(5)
allows you to store a fixed size of 5 character data.
b) varchar(n) – allows to store data of character type of variable size, with a
maximum size n. For eg. varchar(10) allows you to store a max of 10 character
data.
2. Numeric data types – These are data types that can handle numeric data and are of
the following types :
a) int(n) / integer(n) – allows to store data of integer type of the specified n no
of digits. For eg. int(5) allows you to store a max of 5 integer digits.
b) decimal(n,d) – allows you to store fractional data containing total n no of
digits including a decimal point, out of which d digits will be in decimal form.
For eg. decimal(6,2) allows you to store a max of 3 integer digits and 2
decimal digits, and a decimal point.
3. Date and time data types – These are data types that can handle date and time data,
which are of a fixed size and format, and are of the following types :
a) date – allows to store data of date type using the format YYYY-MM-DD.
b) time – allows to store data of time type using the format HH:NN:SS.
Note :
1. All character and date time data should be enclosed either in single quotes ‘ ’ or
double quotes “ ”
2. The int data type can store integers only in the range -2147483648 to 2147483647
(max of 10 digits)
3. The float data type can also be used in the same way as the decimal datatype
4. There are various other data types in MySQL, but not covered in the syllabus

Difference between CHAR and VARCHAR data types


CHAR VARCHAR
1. Stores fixed length character data 1. Stores variable length character data
2. Can store max upto 255 characters 2. Can store max upto 65535 characters
3. CHAR(10) will always take 10 bytes of 3. VARCHAR(10) will take memory bytes
memory storage, no matter the size of according to size of the data entered,
data entered and an additional 2 bytes to hold the
variable length information
4. If the data entered is less than 4. No blank spaces are added, if the data
specified size, additional blank spaces entered is less than the specified size.
are added as per size
For eg. If the string ‘Kiran’ is entered into For eg. If the string ‘Kiran’ is entered into
CHAR(10), then the data stored will be VARCHAR(10), then the data stored will be
‘Kiran ‘ (additional 5 blanks will be added) ‘Kiran‘ (no blanks are added)

Computer Science with Python Class XII Page | 2 © Reeba John, Computer PGT, MTPS
RDBMS and MySQL

Starting MySQL

After installing MySQL, you can start using MySQL by selecting


Start -> All Programs -> MySQL -> MySQL Server -> MySQL Command Line Client.

As soon as you open MySQL, you will be asked to enter the password. By default it is ‘root’,
unless it is otherwise set during installation.
After logging in successfully, you will come to the MySQL prompt :

You can start typing SQL commands at the prompt. Some points to remember are :
● SQL is not case sensitive, no difference between small and capital letters
● Each command should end with a semicolon ;
● A command can be split across multiple lines, then each new line will start with the
arrow prompt ( -> ). Only when you end a line with semicolon ; you will get the
mysql prompt back.

Basic DDL Commands

Some of the basic DDL commands you will be first using are how to create a database or a
table in MySQL. The various DDL commands are :
1) show databases : - This command lists out all the databases that exist in MySQL. Initially,
only the standard databases of MySQL will be shown. Please note, under no circumstances,
should you use these standard databases of MySQL.
For eg.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.47 sec)

Computer Science with Python Class XII Page | 3 © Reeba John, Computer PGT, MTPS
RDBMS and MySQL

2) create database :- This command allows you to create a new database in MySQL. This is a
one-time activity, since a single database can hold many tables within it. The new database
created will be shown in the list when you use the show databases command. The syntax of
this command is :
create database <databasename>;
For eg.
mysql> create database xiib;
Query OK, 1 row affected (0.16 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| xiib |
+--------------------+
4 rows in set (0.47 sec)

3) use :- Once a database is created, you need to log in to the database to do various
activities like creating a table. Each time you login to MySQL, you have to login to a database
also. This command is used in the following syntax :
use <databasename>;
For eg.
mysql> use xiib;
Database changed

4) create table :- This command allows you to create a new table in the currently opened
database. It allows you to define the structure of the table, by specifying the table name,
and the name and datatype of each column in the table. The syntax of this command is :
create table <tablename> (
<column1> <datatype>(<size>),
<column2> <datatype>(<size>),
...
<columnn> <datatype>(<size>));
For eg.
mysql> create table product (
-> pcode int(4),
-> pname varchar(25),
-> price int(5),
-> brand varchar(20));
Query OK, 0 rows affected (0.33 sec)

5) describe / desc :- Once a table is created, you can view the structure of the table using
the describe or desc command. It displays the number of columns, their datatype, size, etc.
This command is used in the following syntax :
describe/desc <tablename>;

Computer Science with Python Class XII Page | 4 © Reeba John, Computer PGT, MTPS
RDBMS and MySQL

For eg.
mysql> desc product;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| pcode | int(4) | NO | | | |
| pname | varchar(25) | YES | | NULL | |
| price | int(5) | YES | | NULL | |
| brand | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.16 sec)

6) show tables : - This command lists out all the tables existing in the current database.
For eg.
mysql> show tables;
+----------------+
| Tables_in_xiib |
+----------------+
| product |
+----------------+
1 row in set (0.00 sec)

ASSIGNMENTS

1. Create a table Bank with the following details :


Bank
Field name Data type Size
Acct_no Int 8
Name Varchar 25
Acct_type Char 1
Start_date Date
Balance_amt Decimal 10,2

2. Create a table Student with the following details :


Student
Field name Data type Size
RollNo Int 2
Name Varchar 25
Class Varchar 5
Age Int 2
Marks Decimal 6,2

Computer Science with Python Class XII Page | 5 © Reeba John, Computer PGT, MTPS

You might also like