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

Chapter 13 - SQL

The document discusses Structured Query Language (SQL) and the MySQL database management system. It provides an overview of SQL and lists several databases that use SQL. It then describes MySQL in more detail, including its features, types of SQL commands, data types, and common commands used to create and interact with databases and tables. The document also covers constraints, indexes, and different types of joins that can be performed in MySQL queries.

Uploaded by

Krishna Jyotish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Chapter 13 - SQL

The document discusses Structured Query Language (SQL) and the MySQL database management system. It provides an overview of SQL and lists several databases that use SQL. It then describes MySQL in more detail, including its features, types of SQL commands, data types, and common commands used to create and interact with databases and tables. The document also covers constraints, indexes, and different types of joins that can be performed in MySQL queries.

Uploaded by

Krishna Jyotish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 13 :

Informatics
Practices
Class XI ( As per
CBSE Board)
SQL
Commands
New
Syllabus
2018-19

Visit : python.mykvs.in for regular updates


SQL
SQL is an acronym of Structured Query Language.It
is a standard language developed and used for
accessing and modifying relational databases.
The SQL language was originally developed at the
IBM research laboratory in San José, in connection
with a project developing a prototype for a relational
database management system called System R in the
early 70s.
SQL is being used by many database management
systems. Some of them are:
 MySQL
 PostgreSQL
 Oracle
 SQLite
 Microsoft SQL Server
Visit : python.mykvs.in for regular updates
MYSQL
MySQL is currently the most popular open
source database software. It is a multi-user,
multithreaded database management system.
MySQL is especially popular on the web. It is
one of the parts of the very popular LAMP
platform. Linux, Apache, MySQL and PHP or
WIMP platform Windows,Apache,MySQL and
PHP.
MySQL AB was founded by Michael Widenius
(Monty), David Axmark and Allan Larsson in
Sweden in year 1995.

Visit : python.mykvs.in for regular updates


MYSQL
MySQL Features
Open Source & Free of Cost:
It is Open Source and available at free of cost.
Portability:
Small enough in size to instal and run it on any types of
Hardware and OS like Linux,MS Windows or Mac etc.
Security :
Its Databases are secured & protected with password.
Connectivity
Various APIs are developed to connect it with many
programming languages.
Query Language
It supports SQL (Structured Query Language) for handling
database.

Visit : python.mykvs.in for regular updates


MYSQL
Types of SQL Commands
 DDL (Data Definition Language)
To create database and table structure-commands
like CREATE , ALTER , DROP etc.
 DML (Data Manipulation Language)
Record/rows related operations.commands like
SELECT...., INSERT..., DELETE..., UPDATE.... etc.
 DCL (Data Control Language)
Used to control the transactions.commands like
COMMIT, ROLLBACK, SAVEPOINT etc.
 Transactional control Language.
used to manipulate permissions or access
rights to the tables.commands like GRANT , REVOKE
etc.

Visit : python.mykvs.in for regular updates


MYSQL
MySql datatypes
numeric
decimal -decimal(<precision>, [<scale>]) [zerofill] For storing
floating-point numbers where precision is critical.
Int - int(<size>) [auto_increment] [unsigned] [zerofill]
A whole number, 4 bytes, with a maximum range of -2,147,483,648 to
2,147,483,647 (unsigned: 0 to 4,294,967, 295)
string
char-char(<size>) [binary]
Fixed length – for storing strings that won't vary much in size.
Range of 0 to 255, stores that amount in bytes
Varchar-varchar(<size>) [binary]
Variable length – for storing strings that will vary in size.
Range of 0 to 255, stores that amount in bytes, plus 1 byte
date
Date-Format: YYYY-MM-DD ,Example: 2006-09-23,Range of years
1000 to 9999

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
Getting listings of database and tables
mysql> SHOW DATABASES;
mysql> SHOW TABLES;
Creating a database-
mysql> CREATE database myschool;
Deleting a database mysql> DROP database abc;
database;to remove table mysql> drop table abctable;
After we have created the database we use the USE statement to
change the current
mysql> USE myschool;
Creating a table in the database is achieved with CREATE table
statement.
mysql> CREATE TABLE student (lastname varchar(15),firstname
varchar(15), city varchar(20), class char(2));
The command DESCRIBE is used to view the structure of a table.
mysql> DESCRIBE student;
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
To insert new rows into an existing table use the INSERT
command:
mysql>INSERT INTO student values(‘dwivedi’,’freya’,’Udaipur’,’4’);
Similarly we can insert multiple records.With the SELECT
command we can retrieve previously inserted rows:
mysql> SELECT * FROM student;
Selecting rows by using the WHERE clause in the SELECT
command
mysql> SELECT * FROM student WHERE class=“4";
Selecting specific columns(Projection) by listing their names
mysql> SELECT first_name, class FROM student;
To modify or update entries in the table use the UPDATE
command
mysql> UPDATE student SET class=“V" WHERE
firstname=“freya";
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
Deleting selected rows from a table using the DELETE
command
mysql> DELETE FROM student WHERE firstname=“amar";
A general form of SELECT is:
SELECT what to select(field name)
FROM table(s)
WHERE condition that the data must satisfy;
Comparison operators are: < ; <= ; = ; != or <> ; >= ; >
Logical operators are: AND ; OR ; NOT
Comparison operator for special value NULL: IS
mysql> SELECT * FROM Student WHERE City IS NULL ;

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
Pattern Matching – LIKE Operator
A string pattern can be used in SQL using the following wild
card
% Represents a substring in any length
_ Represents a single character
Example:
‘A%’ represents any string starting with ‘A’ character.
‘_ _A’ represents any 3 character string ending with ‘A’.
‘_B%’ represents any string having second character ‘B’
‘_ _ _’ represents any 3 letter string.
A pattern is case sensitive and can be used with LIKE operator.
mysql> SELECT * FROM Student WHERE Name LIKE ‘A%’;
mysql> SELECT * FROM Student WHERE Name LIKE%Singh%’;
mysql> SELECT Name, City FROM Student WHERE Class>=8
AND Name LIKE ‘%Kumar%’ ;
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
Ordering Query Result – ORDER BY Clause
A query result can be orders in ascending (A-Z) or
descending (Z-A) order as per any column. Default is
Ascending order.
mysql> SELECT * FROM Student ORDER BY class;
To get descending order use DESC key word.
mysql> SELECT * FROM Student ORDER BY class
DESC;

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
Creating Table with Constraints
The following constraints are commonly used in SQL:
NOT NULL -It Ensures that a column cannot have a NULL
value
UNIQUE - It Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
FOREIGN KEY - It Uniquely identifies a row/record in another
table
CHECK - It Ensures that all values in a column satisfies a
specific condition
DEFAULT - It Sets a default value for a column when no value
is specified
INDEX - It is Used to create and retrieve data from the
database very quickly
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
Creating Table with Constraints
mysql> CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘Jaipur',
CONSTRAINT CHK_Person CHECK (Age>=18)
);
mysql> CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(ID)
);

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
MySQL - INDEXES
Indexes are used to retrieve data from the database very
fast. The users cannot see the indexes, they are just
used to speed up searches/queries.
The INSERT and UPDATE statements take more time on
tables having indexes, whereas the SELECT statements
become fast on those tables. The reason is that while
doing insert or update, a database needs to insert or
update the index values as well.
CREATE INDEX
Creates an index on a table. Duplicate values are
allowed:
mysql> CREATE INDEX idx_lastname
ON Persons (LastName);
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
MySQL – INDEXES
CREATE UNIQUE INDEX
Creates a unique index on a table. Duplicate values are
not allowed:
mysql> CREATE UNIQUE INDEX idx_lastname
ON Persons (LastName);
DROP INDEX Statement
The DROP INDEX statement is used to delete an index in
a table.
mysql> ALTER TABLE table_name
DROP INDEX index_name;

Visit : python.mykvs.in for regular updates


MYSQL

Database Commands in MySql


MySQL JOINS

In MySQL JOINS are used with SELECT statement. It is used


to retrieve data from multiple tables.

There are three types of MySQL joins:

1. MySQL INNER JOIN (or sometimes called simple join)


2. MySQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
3. MySQL RIGHT OUTER JOIN(or sometimes called RIGHT JOIN)

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
1. MySQL Inner JOIN (Simple Join)
It return all rows from multiple tables where the join
condition is satisfied.

mysql> SELECT columns


FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Visit : python.mykvs.in for regular updates


MYSQL
Database Commands in MySql
2. MySQL Left Outer Join
It returns all rows from the left hand table specified in the
ON condition and only those rows from the other table
where the join condition is fulfilled.

mysql> SELECT columns


FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Visit : python.mykvs.in for regular updates
MYSQL
Database Commands in MySql
2. MySQL Right Outer Join
It returns all rows from the right hand table specified in the
ON condition and only those rows from the other table
where the join condition is fulfilled.

mysql> SELECT columns


FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Visit : python.mykvs.in for regular updates

You might also like