100% found this document useful (1 vote)
140 views23 pages

Mysql Commands Notes

SQL is a standard language used for accessing and manipulating databases. It provides statements to query, insert, update, and delete data, as well as create, alter, and drop database objects like tables. SQL statements are categorized into data definition language statements, which define database structures, and data manipulation language statements, which query and manipulate data. SQL supports various data types including character, number, and date types.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
140 views23 pages

Mysql Commands Notes

SQL is a standard language used for accessing and manipulating databases. It provides statements to query, insert, update, and delete data, as well as create, alter, and drop database objects like tables. SQL statements are categorized into data definition language statements, which define database structures, and data manipulation language statements, which query and manipulate data. SQL supports various data types including character, number, and date types.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

Structured Query Language (SQL)

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.

SQL provides statements for a variety of tasks, including:

Querying data

Inserting, updating, and deleting rows in a table

Creating, replacing, altering, and dropping objects (tables)

Controlling access to the database and its objects (tables)

Guaranteeing database consistency and integrity

Advantages of using SQL:

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) statement

Data Manipulation Language (DML) statement

Transaction Control Statement


Data Definition Language (DDL) Statements

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 Manipulation Language (DML) Statements


Data Manipulation Language (DML) statements are used to access and manipulate data
in existing tables. The manipulation includes inserting data into tables, deleting data from
the tables, retrieving data and modifying the existing data. The common DML statements
are SELECT, UPDATE, DELETE and INSERT.

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)

Fixed-length character string having maximum length n.

VARCHAR: Varchar is a variable character string. If we declare data type as


VARCHAR, then it will occupy space for NULL values. It can have maximum of 2000
characters.

Format: VARCHAR (n)

Variable-length character string having maximum length 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.

The following numbers can be stored in a Number data type column:

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:

DECIMAL[ (p[ , s] )] and NUMERIC[ (p[ , s] )]

Square brackets ([ ]) are option.

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;

SQL can retrieve data from a database


SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
Create database name;

Show databases ;

Use name;

Select database();
Drop database

Create database <name>


Use <name>
Show databases;
Drop database <name>
Select database ()

CREATE TABLE Command

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).

The CREATE TABLE command requires:


Name of the table,

Names of fields,

In SQL, we have the following constraints:

PRIMARY KEY - To check that a column have an unique identity which helps to find a
particular record in a table.

Syntax:

CREATE TABLE<table name>

(<column name1> <data type>[size]

<column name2> <data type>[size]

<column name n> <data type>[size]);

Example:

Create the following table:

Table: Student

Column Name Data Type Size


Adno Numeric 3
Name Varchar 20
Class Numeric 2
Section Char 1
Fees Numeric 10, 2

Command:
CREATE TABLE student

(Adno Numeric (3) primary key,

Name varchar (20) ,

Class Numeric (2),

Section char (1),

Fees numeric (10, 2));


Computer Science

INSERT INTO Command:

This command is used to add rows in the table, but can add only one row at a time.

Syntax:

INSERT INTO <table name> [Column_name1,


Column_name2, ......Column_name n] VALUES

(value1,value2,value3,….,value n);

OR

INSERT INTO <table name> VALUES (value1,value2,value3,….,value n);

Example:

Insert the following information to the table student:


Adno Name Class Section Fees
111 Anu Jain 12 A
222 Mohit Sharma 11 B 4500
333 K.P.Gupta 12 B 3000
444 Ajit Kumar 10 A 2000
555 Nandini 12 C 3000
666 Rohan Sharma
Adno Name Class Section Fees
111 Anu Jain 12 A 2500
222 Mohit Sharma 11 B 4500
333 K.P.Gupta 12 B 3000
444 Ajit Kumar 10 A 2000
555 Nandini 12 C 3000
666 Rohan Sharma 11 B 2500

INSERT INTO student VALUES (111,"Anu Jain", 12,"A", 2500);

INSERT INTO student VALUES (222,"Mohit Sharma", 11,"B", 4500);

[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:

SELECT (*/field list)

FROM <table name>

[WHERE <condition>];
Example:
Display student table information.

SELECT *
FROM
student;

This will display all information of the particular table (student) in the database.

To display name and class of student table


information. SELECT name, class

FROM student;

To display name of 10th class student


information. SELECT name

FROM student
WHERE class
= 10;

Operators used in SQL commands:

Arithmetic operators:

Arithmetic operator takes two operands and performs a mathematical calculation on


them. However, they can be used only in SELECT command. The arithmetic operators
used in SQL are:

+ 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 -

< less than

> greater than

< = less than or equal to

= greater than or
equal to = equal to

! = not equal to

Example:

Table: Student

Adno Name Class Section Fees


111 Anu Jain 12 A 2500
222 Mohit Sharma 11 B 4500
333 K.P.Gupta 12 B 3000
444 Ajit Kumar 10 A 2000
555 Nandini 12 C 3000
666 Rohan Sharma 11 B 2500
Computer Scien
Display students' name, who are paying below
3000 fees. SELECT name FROM student WHERE
fees<3000;

Output:

Name

Anu Jain

Ajit Kumar

Rohan Sharma

Display students' name, who are paying above or equal to


3000 fees. SELECT name

FROM student
WHERE
fees>=3000;

Output:

Name

Mohit Sharma
Nandini

Display students' information, who are not in class 10

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.

Logical operators are: Science


AND

OR

NOT

Example:
Display information of students in class 11B.

SELECT *
FROM
student

WHERE class = 11 AND section = 'B';

Fee
Adno Name Class Section s
450
222 Mohit Sharma 11 B 0
250
666 Rohan Sharma 11 B 0

Display 11th and 12th class students' information.

SELECT *
FROM
student

WHERE class =11 OR class=12;

Adno Name Class Section Fees


111 Anu Jain 12 A 2500
222 Mohit Sharma 11 B 4500
333 K.P.Gupta 12 B 3000
555 Nandini 12 C 3000
666 Rohan Sharma 11 B 2500

Display students' information, who are not in 10th class.

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:

Display the names that start with letter


"A". SELECT name

FROM student

WHERE name LIKE "A%";

Here, % replaces one or more characters.

Name

Anu Jain
Display names, whose name's second letter
is 'o'. SELECT name

FROM student

WHERE name LIKE "_ o%";

Here, % replaces one or more than one character and _ replaces only one character.

Name

Mohit Sharma

Rohan Sharma

Display names, whose name has 7


characters. SELECT name

FROM student

WHERE name LIKE "_______";

Here, _ replaces only one character. As such, 7 underscores replace 7 characters.

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

WHERE fees BETWEEN 2500 AND 3500;

[Note: In the above Query 2500 and 3500 is also included]

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

This command is used to arrange values in ascending or descending order.

For example:
SELECT *
FROM student

ORDER BY fees ASC;

'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

SELECT * FROM student ORDER BY fees DESC;

'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

This command is used to implement modification of the data values.

Syntax:

UPDATE <table name>

SET <column name1>=new value, <column


name>=new value etc [WHERE <condition>];

Example:

Increase fees value by


500. UPDATE
student

SET fees = fees + 500;

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

Increase the fees value by 100 for adno 222.


UPDATE student SET fees = fees+100 WHERE adno = 222;

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

FROM <table name>


[WHERE <condition>];

For example:

Remove adno 444 information.

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

Remove all records.


DELETE
FROM
student;

ALTER TABLE command

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:

ALTER TABLE <table name>

[ADD/MODIFY/DROP] <column name>;

For example:

Add one new column totalfees with number


(10, 2).
ALTER TABLE student

ADD totalfees number(10,2);

Change totalfees datatype as


number(12,2).
ALTER TABLE student

MODIFY totalfees number(12,2);

Add primary Key


alter table <name>
add primary
key(colname)
Add not null
alter table <name>
modify <colname>
datatype not null
Remove totalfees
column.
ALTER TABLE student
DROP totalfees;
Remove primary key
Alter table drop primary
key

Change the name of


column
Alter table <name>
Change <old name>
<newname>

DROP TABLE Command

This command is used to remove the entire structure of the table and information. This is
also from the DDL command.

Syntax:

DROP TABLE<table name>;

For example:

Remove the whole structure of student table.

DROP TABLE student;

You might also like