Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Amrita

Vidyalayams

Class:XII
Sub: Computer science
Chapter No. : 12

Simple queries in SQL


Some Mysql elements
• Literals
• Datatypes
• Nulls
• Comments
Literals
• Fixed data value
• May be of character or numeric literal
• Char – Quotation mark( 4000 bytes)
• Numeric – no need of quotation mark
i) integer literals (53 digits)
ii) Real literals
Data types

Data types are means to identify the type of


data and associated operations for handling it.
Common MySQL data types

CHAR Data Type VARCHAR Data Type


Its full name is CHARACTER Its full name is VARIABLE CHARACTER

It stores values in fixed lengths and are VARCHAR stores values in variable length
padded with space characters to match along with 1-byte or 2-byte length prefix
the specified length and are not padded with any characters

It uses static memory allocation. It uses dynamic memory allocation.

mysql>create table emp(name mysql>create table emp1(name


CHAR(20)); VARCHAR(20));
Query OK, 0 rows affected Query OK, 0 rows affected
Null values

• If a column in a row has no value, then column


is said to be null.
• It is suitable for any data type.
• If the actual value is not known then use null
value.
• Null always evaluates to null only.
Comments

• Not executed text


• Only for documentation purpose
• Multi line comment -> /* */
• Single line comment -> -- or #
SQL command syntax

• Keywords – Special meaning words


• Commands or statements – Instructions
• Commands consist of one or more logically
distinct parts called clauses.
Symbols used in syntax statements

• Table 12. 1
Accessing database in MySQL
Creating Tables in MySQL
Inserting data into Table
Inserting data into Table

Inserting dates:
Default date format is ‘YYYY-MM-DD’
Making simple queries through select command
Selecting All data:
Making simple queries through select command
Selecting particular Rows:
1. Select all pets with gender(sex) as male(“m”).
mysql> SELECT * FROM pet
WHERE sex = ‘m’;
2. Select all pets that were born on or after Jan 1, 2019
mysql> SELECT * FROM pet
WHERE birth>=‘2019-01-01’;
3. Select all female-dogs.
mysql> SELECT * FROM pet
WHERE species=‘dog’ AND sex=‘f’;
4. Select all snakes or birds.
mysql> SELECT * FROM pet
WHERE species=‘snake’ AND species = ‘bird’;
5. Select all male cats.
mysql> SELECT * FROM pet
WHERE (species=‘cat’ AND sex=‘m’;
Making simple queries through select command
Selecting particular Columns:
1. Display names and birth-dates of all pets.
mysql> SELECT name, birth FROM pet;
2. Displaly owners of pets born after Dec 2018
mysql> SELECT owner FROM pet
WHERE birth>’2018-12-31’;
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Using Column Aliases:
Making simple queries through select command
Making simple queries through select command
Making simple queries through select command
Creating tables with SQL constraints
Table is created using CREATE TABLE commands.

 While creating tables, we may need to apply certain conditions on columns.


 To apply conditions on columns, SQL constraints are used.
Creating tables with SQL constraints

S.No. Constraints Description

1. NOT NULL Ensures that a column cannot have NULL value.


Provides a default value for a column when none is
2. DEFAULT specified.
3. UNIQUE Ensures that all values in a column are different.
Makes sure that all the values in a column satisfy certain
4. CHECK
criteria.
5. Primary Key Used to uniquely identify a row in the table.

6. Foreign Key Used to ensure referential integrity of the data.


Creating tables with SQL constraints
Creating tables with SQL constraints
Creating tables with SQL constraints
Creating tables with SQL constraints
Applying Table Constraints
Modifying data in tables
Modifying data in tables

Deleting data from tables


Altering tables
Altering tables
Dropping tables
Altering tables
Altering tables
Dropping tables
SQL JOINS
An SQL join is a query that fetches data from two or
more tables whose records are joined with one
another based on a condition.
Syntax:
SELECT <Column list>
FROM <Table1> ,<Table2>[,<Table3>...]
WHERE <join condition for the tables>
Eg:
SELECT Ename, Loc
FROM Emp, Dept
WEHERE Ename = ‘Anu’
AND Emp.Deptno = Dept.Deptno;
SQL JOINS

Types of JOINS:
There are many types of JOINS possible,
These are:
(i) Cartesian Product
(ii) Equi Join
(iii) Inner Join
(iv) Natural Join
(v) Left Join
(vi) Right Join
SQL JOINS

(i) Cartesian Product (Cross Join) :


An SQL join query without any join
condition returns all the records of joined
with all the records of the other table.
SQL JOINS
SQL JOINS

(ii) Equi Join :


It joins two or more tables based on a
condition using equality operator.
SELECT * FROM table1 JOIN table2 ON (table1.id
= table2.id)
SQL JOINS
SQL JOINS

(iii) Inner Join :


• An Inner Join implements an equijoin.
• In this join, only those rows are returned
from both the tables that satisfy the join
condition.
SQL JOINS

(iii) Inner Join :


SQL JOINS

(iii)Natural Join :
• A Natural join is a type of equi join where
the join condition compares all the same
names columns in both tables.
SELECT * FROM table1 NATURAL JOIN table2
SQL JOINS

(iv)Natural Join :
SQL JOINS

(iv)Left Join :
It selects rows from both left and right
tables that are matched, plus all rows from
the left table even with no matching rows
found in the right table.
SQL JOINS

(iv)Left Join :
SQL JOINS

(iv)Right Join :
It selects rows from both left and right
tables that are matched, plus all rows from
the right table even with no matching rows
found in the left table.
SQL JOINS

(iv)Right Join :
SQL JOINS
Indexes in database

An index is a data structure maintained by a


database that helps it find records within a
table more quickly.
Indexes in database

Creating indexes in MySQL:


(i) Create index at the time of table creation
Syntax:
CREATE TABLE table_name
{
column1 datatype [NULL|NOT NULL],
column2 datatype [NULL|NOT NULL],
...
columnn datatype [NULL|NOT NULL],
...
INDEX Index_name (index_col1 [(length)] [ASC|DESC],
...
index_coln [(length)] [ASC|DESC])
};
Indexes in database

Creating indexes in MySQL:


(i) Create index at the time of table creation
Eg:
CREATE TABLE Players
{
PlayerNo INTEGER NOT NULL,
Name CHAR(15) NOT NULL,
Dob DATE,
Address VARCHAR(100) NOT NULL,
Ph_No INTEGER NOT NULL,
TeamNo CHAR(4) NOT NULL,
INDEX Player_idx (TeamNo)
};
Indexes in database

Creating indexes in MySQL:


(ii) Create index on an already existing table
Syntax 1:
CREATE INDEX <indexname> ON <tablename>
(<column>[ASC|DESC], (<column>[ASC|DESC],...);

Syntax 2: Unique index


CREATE UNIQUE INDEX <indexname> ON <tablename>
(<column>[ASC|DESC], (<column>[ASC|DESC],...);
Eg: Unique index
CREATE UNIQUE INDEX Player_idx ON Players (TeamNo);

You might also like