Mysql Commands Notes
Mysql Commands Notes
Structured Query Language (SQL) is a standard language used for accessing databases.
This is a special purpose programming language used to create a table, manage data and
mainipulate data.
Querying data
SQL is portable: SQL is running in all servers, mainframes, PCs, laptops, and even
mobilephones.
High speed: SQL queries can be used to retrieve large amounts of records from a
database quickly and efficiently.
Easy to learn and understand: SQL generally consists of English statements and as such,
it is very easy to learn and understand. Besides, it does not require much coding unlike in
programming languages.
SQL is used with any DBMS system with any vendor: SQL is used by all the vendors
who develop DBMS. It is also used to create databases, manage security for a database,
etc. It can also be used for updating, retrieving and sharing data with users.
SQL is used for relational databases: SQL is widely used for relational databases.
SQL acts as both programming language and interactive language: SQL can do both the
jobs of being a programming language as well as an interactive language at the same
time.
Supports object based programming: SQL supports the latest object based programming
and is highly flexible.
Types of SQL Statements
The SQL statements are categorized into different categories based upon the purpose.
They are;
Data Definition Language (DDL) or Data Description Language (DDL) is a standard for
commands that defines the different structures in a database. DDL statements are used to
create structure of a table, modify the existing structure of the table and remove the
existing table. Some of the DDL statements are CREATE TABLE, ALTER TABLE and
DROP TABLE.
Data Types
Each value manipulated by SQL Database has a data type. The data type of a value
associates a fixed set of properties with the value. In SQL there are three main data types:
Character, Number, and Date types.
Character
Character data types stores character (alphanumeric) data, which are words and free-form
text. They are less restrictive than other data types and consequently have fewer
properties. For example, character columns can store all alphanumeric values, but number
columns can store only numeric values. Character data types are;
CHAR
VARCHAR
CHAR: CHAR should be used for storing fix length character strings. String values will
be space/blank padded (The adding of meaningless data [usually blanks] to a unit of data
to bring it up to some fixed size) before they are stored on the disk. If this type is used to
store variable length strings, it will waste a lot of disk space (always allocate fixed
memory) . If we declare data type as CHAR, then it will occupy space forNULL values.
Format: CHAR(n)
Example:
CHAR(10) has fixed length, right padded with spaces. VARCHAR(10) has fixed length,
right padded with NULL .
Name char (10): Suppose if we store Name is as "Ravi", then first four places of the ten
characters are filled with Ravi and the remaining 6 spaces are also allocated to Name.
Thus, the size of name is always ten. Name varchar (10): Suppose if we store Name is as
"Ravi", then first four places of the ten characters are filled with Ravi and the remaining 6
spaces are filled with NULL.
Numeric data type: Numeric data types are mainly used to store number with or without
fraction part. The numeric data types are:
NUMBER,DECIMAL,NUMERIC,INT,FLOAT
NUMBER: The Number data type stores fixed and floating-point numbers. The Number
data type is used to store integers (negative, positive, floating) of up to 38 digits of
precision.
Format:
NUMBER (p, s)
Where;
– 'p' is the precision or the total number of significant decimal digits, where the most
significant digit is the left-most nonzero digit and the least significant digit is the right-
most known digit.
– 's' is the scale or the number of digits from the decimal point to the least significant
digit.
DECIMAL and NUMERIC: Decimal and numeric data types have fixed precision and
scale.
Format:
where;
– 'p' is the precision or the total number of significant decimal digits, where the most
significant digit is the left-most nonzero digit and the least significant digit is the right-
most known digit.
– 's' is the scale or the number of digits from the decimal point to the least significant
digit.
INT/INTEGER: The int data type is the integer data type in SQL. This used to store
integer number (without any fraction part).
FLOAT: This data type is used to store number with fraction part(real numbers).
DATE
Date is used to store valid date values, which is ranging from January 1, 4712 BC to
December 31, 9999 AD. The date formats are: YYYY-MM-DD
SQL
SQL (Structured Query Language) is a standard language for accessing and manipulating
databases. SQL commands are used to create, transform and retrieve information from
Relational Database Management Systems and also used to create interface between user
and database. By using SQL commands, one can search any data in the database and
perform other functions like, create tables, add records, modify data, remove rows, drop
table etc. SQL commands are used to implement the following;
Show databases ;
Use name;
Select database();
Drop database
CREATE TABLE command is used to create table structure. In this command, we need
to give full information about table such as number of columns, type of each column and
constraints (primary key).
Names of fields,
PRIMARY KEY - To check that a column have an unique identity which helps to find a
particular record in a table.
Syntax:
Example:
Table: Student
Command:
CREATE TABLE student
This command is used to add rows in the table, but can add only one row at a time.
Syntax:
(value1,value2,value3,….,value n);
OR
Example:
[Note: If we want to insert values from the selective columns then we have to use this
method INSERT INTO student (ADNO, Name, CLASS) VALUES (777,' LEENA', 'B');]
SELECT Command
This command is used to view table information from SQL database. By using SELECT
command, we can get one or more fields information, while using *, one can get all fields
information.
Syntax:
[WHERE <condition>];
Example:
Display student table information.
SELECT *
FROM
student;
This will display all information of the particular table (student) in the database.
FROM student;
FROM student
WHERE class
= 10;
Arithmetic operators:
+ Addition
- Subtraction
Multiplication /
Division
Example (string
join)
Table: Name
Relational operators:
Relational operators are used to implement comparison between two operands. These
operators can be used only in 'where clause'. Relational operators are -
= greater than or
equal to = equal to
! = not equal to
Example:
Table: Student
Output:
Name
Anu Jain
Ajit Kumar
Rohan Sharma
FROM student
WHERE
fees>=3000;
Output:
Name
Mohit Sharma
Nandini
SELECT *
FROM student
WHERE class!
= 10;
Fee
Adno Name Class Section s
250
111 Anu Jain 12 A 0
450
222 Mohit Sharma 11 B 0
300
333 K.P.Gupta 12 B 0
300
555 Nandini 12 C 0
250
666 Rohan Sharma 11 B 0
Logical operators:
Logical operators are also possible only in 'where clause' and are used to merge more
than one condition.
OR
NOT
Example:
Display information of students in class 11B.
SELECT *
FROM
student
Fee
Adno Name Class Section s
450
222 Mohit Sharma 11 B 0
250
666 Rohan Sharma 11 B 0
SELECT *
FROM
student
SELECT * FROM
student WHERE
NOT class = 10;
Fee
Adno Name Class Section s
250
111 Anu Jain 12 A 0
450
222 Mohit Sharma 11 B 0
300
333 K.P.Gupta 12 B 0
300
555 Nandini 12 C 0
250
666 Rohan Sharma 11 B 0
LIKE OPERATOR
LIKE OPERATOR is used to search a value similar to specific pattern in a column using
wildcard operator. There are two wildcard operators - percentage sign (%) and
underscore ( _ ). The percentage sign represents zero, one, or multiple characters, while
the underscore represents a single number or character. The symbols can be used in
combinations.
For example:
FROM student
Name
Anu Jain
Display names, whose name's second letter
is 'o'. SELECT name
FROM student
Here, % replaces one or more than one character and _ replaces only one character.
Name
Mohit Sharma
Rohan Sharma
FROM student
Name
Nandini
BETWEEN Operator
The BETWEEN operator is used to test whether or not a value (stated before the keyword
BETWEEN) is "between" the two values stated after the keyword BETWEEN.
For example:
Display students' information, who are paying fees between 2500 and 3500.
SELECT *
FROM student
Fee
Adno Name Class Section s
250
111 Anu Jain 12 A 0
300
333 K.P.Gupta 12 B 0
200
444 Ajit Kumar 10 A 0
300
555 Nandini 12 C 0
250
666 Rohan Sharma 11 B 0
ORDER BY
For example:
SELECT *
FROM student
'asc' for ascending order. Without asc also the list is displayed with ascending order only.
Fee
Adno Name Class Section s
200
444 Ajit Kumar 10 A 0
250
111 Anu Jain 12 A 0
Rohan 250
666 Sharma 11 B 0
300
333 K.P.Gupta 12 B 0
300
555 Nandini 12 C 0
450
222 Mohit Sharma 11 B 0
'desc' for descending order. If the 'desc' is not given, the list will be displayed with
ascending order.
Fee
Adno Name Class Section s
450
222 Mohit Sharma 11 B 0
300
555 Nandini 12 C 0
300
333 K.P.Gupta 12 B 0
666 Rohan Sharma 11 B
250
0
250
111 Anu Jain 12 A 0
200
444 Ajit Kumar 10 A 0
UPDATE Command
Syntax:
Example:
Fee
Adno Name Class Section s
300
111 Anu Jain 12 A 0
500
222 Mohit Sharma 11 B 0
350
333 K.P.Gupta 12 B 0
250
444 Ajit Kumar 10 A 0
350
555 Nandini 12 C 0
300
666 Rohan Sharma 11 B 0
Fee
Adno Name Class Section s
300
111 Anu Jain 12 A 0
510
222 Mohit Sharma 11 B 0
350
333 K.P. Gupta 12 B 0
250
444 Ajit Kumar 10 A 0
350
555 Nandini 12 C 0
300
666 Rohan Sharma 11 B 0
DELETE Command
This command is used to remove information from a particular row or rows. Please
remember that this command will delete only row information but not the structure of the
table.
Syntax:
DELETE
For example:
DELETE FROM
student WHERE
adno = 444;
Fee
Adno Name Class Section s
300
111 Anu Jain 12 A 0
510
222 Mohit Sharma 11 B 0
350
333 K.P.Gupta 12 B 0
350
555 Nandini 12 C 0
300
666 Rohan Sharma 11 B 0
This command is used to implement modification of the structure of the table. This is a
DDL command. Using this command, we can add a new column, remove the existing
column and modify data type of existing column.
Syntax:
For example:
This command is used to remove the entire structure of the table and information. This is
also from the DDL command.
Syntax:
For example: