Powerpoint Presentation on My S

Download Report

Transcript Powerpoint Presentation on My S

CHAPTER:14
Simple Queries in SQL
Prepared By :
VINAY ALEXANDER (विनय अलेक्सजेंड़र)
PGT(CS) ,KV JHAGRAKHAND
Introduction
SQL, Structured Query Language, was
developed in 1970s in an IBM Laboratory.
 SQL sometimes also referred to as
SEQUEL is a 4th generation nonprocedural language.
 SQL, being non-procedural, describes
WHAT all data is to be retrieved or
inserted or modified or deleted, rather
than specifying code describing HOW to
perform the entire operation
SQL, enables the following:
(i) Creating / modifying a database’s
structure
(ii) Changing security settings for system
(iii) Permitting users for working on
databases or tables
(iv) Querying database
(v) Inserting / Modifying/Deleting the
database contents.
SQL Elements in MySQL
The MySQL implementation of SQL has certain
elements that play an important role in defining
/querying a database. These basic elements
are:
(i) Literals
(ii) Data types
(iii) Nulls
(iv) Comments
 Literals: Literals refers to the fixed data value. It
may be Numeric or Character. Numeric literals
may be integer or real numbers and Character
literals must be closed in single quotes like ‘Hello’.
 data types: To identify the type of data
and associated operations for handling it.
Null values: If a column in a row has no
value then it is said to NULL. The Null
should not be used as a Zero value. Nulls
can appear in columns of any data type
provided they are not restricted by NOT
NULL
or
PRIMARY
KEY
integrity
constraints.
Comment:
 /*…. */ => (Multi line comment)
 -- =>(single line comment)
 # =>single line comment from the appearance)
 Numeric Data Types: MySQL uses all the standard
ANSI SQL numeric data types.
 The following list shows the common numeric data
types
 INTEGER or INT: up to 11 digit number without decimal.
 SMALLINT:up to 5 digit number without decimal.
 FLOAT (M,D):Real numbers up to M digit with D
decimal places. Ex. Float (10,2)
 DECIMAL(M,D) or NUMERIC(M,D):Unpacked floating
point up to M length and D decimal places.
 TINYINT: up to 4 digit number without decimal.
 MEDIUMINT: up to 9 digit number without decimal.
 BIGINT: up to 11 digit number without decimal.
 DOUBLE(M.D): A Double precision
floating
numbers up to M digit with D decimal places.default to
16,4
Date & Time Data Types
 DATE : A date in YYYY-MM-DD format, between
1000-01-01 and 9999-12-31. example: December
30th ,1973 would be stored as 1973-12-30.
 DATETIME:A date and time format like YYYYMM-DD HH:MM:SS. Example: 1973-12-30 15:3:00
 TIME :Stores time in HH:MM:SS format.
 YEAR(M): Store a year in 2 or 4 digits format.
Example:
 YEAR(2), year can be 1970 to 2069(70 to 69).
String /text Types:
 CHAR(M):A fixed length string up to 255
characters. (default is 1)
 VARCHAR(M):A variable length string up to 255
characters.
 BLOB :Used to store image data or characters
up to 65535.
 TEXT: Same as BLOB but offers not case
sensitive search.
Difference between Char and Varchar data types
 The difference between CHAR and VARCHAR is
that of fixed length and variable length.
 The CHAR data types specifies a fixed length
character string and VARCHAR data types specifies a
variable length string.
SQL Server Data Types
 int, integer: 4 byte integer
 smallint: 2 byte integer
 tinyint: 1 byte integer
 float: 4 or 8 byte floating point number
 real: 4 byte floating point number
 double precision: 8 byte floating point number
 numeric, decimal(precision, scale): exact
numeric, 2 to 17 bytes. Only difference is that
only numeric types with a scale of 0 can be
used of the IDENTITY column.
 Destroy a table: drop table table_name
SQL COMMAND SYNTAX
The SQL provides a predefined set of
commands that help us work on relational
databases.
Keywords are words that have a special
meaning in SQL. They are understood to be
instructions.
Commands,
or
statements,
are
instructions given by you to a SQL database.
Arguments complete or modify the meaning
of a clause.
SPECIAL OPERATORS
BETWEEN - define range limits
IS NULL - check if attribute value
is null
LIKE - check for similar character
strings
IN - check if attribute value
matches a value within a (sub)set of
listed values
EXISTS - check whether attribute
has a value
Basic MySQL Operations
Create database
 use database
 see data base












Create table
Insert records
Load data
Retrieve records
Update records
Delete records
Modify table
Join table
Drop table
Optimize table
Count, Like, Order by, Group by
More advanced ones (sub-queries, stored procedures,
triggers, views …)
 Some Data Management commands in MySQL
 Creating a Database:
Syntax to Create
database: Create database databasename ;
The following command will create School
database in MySQL.
Ex. mysql> CREATE DATABASE School;
 Opening a database: To open an existing
database you can use the following command.
Ex. mysql> USE school ;
Database changed
 What tables are currently stored in the MyDB
database?
mysql> show tables;
Empty set (0.00 sec)
• What are the current databases at the
server?
mysql> show databases;
+--------------+
| Database |
+--------------+
| mysql
|
| test
|
+--------------+
mysql is a database (stores users’ password) used
by system.
 Creating a table in the database: To create a
table we can use Create Table command. The
following command will create Student table with
Stname and Stcity columns.
 Syntax;
 CREATE TABLE <table-name> (columnname
datatypes[size], columnname data types[size],….);
Ex. mysql> CREATE TABLE student
(Stname char(30),
Stcity char(20),
age Integer );
Making Simple Queries
 Inserting a Record in a table
mysql> INSERT INTO Student VALUES (‘Amit’, ‘
Suratgarh’, 16);
 Deleting a Table and database
mysql> DROP TABLE Student ;
mysql> DROP DATABASE School ;
 Viewing Table Structure
mysql> DESCRIBE Student ;
 The SELECT command of SQL, empower you to make
a request (queries) to retrieve records from the database.
 The syntax of SQL is given below SELECT
<
*|
column
name(s)>
FROM
<table(s)>WHERE <condition> ORDER BY <column
name> [ASC | DESC] ;
SELECT Statement:
for queries on single or
multiple tables
 Clauses of the SELECT statement:
– SELECT: List the columns (and expressions)
that should be returned from the query
– FROM: Indicate the table(s) or view(s) from
which data will be obtained
– WHERE: Indicate the conditions under which a
row will be included in the result
– GROUP BY: Indicate columns to group the
results
– HAVING: Indicate the conditions under which a
group will be included
– ORDER BY: Sorts the result according to
specified columns
18
Selecting From All the Rows-ALL
Keyword: The result retains the
duplicates output rows. It is just the same
as when you specify neither DISTINCT
nor ALL.
 EXAMPLE:
SELECT ALL city FROM Student;
Sorting by Column Alias
 If you have defined column alias in your
query, then you can use this column alias
even for sorting records. This is because,
once a column alias is declared, it can
be used elsewhere in the statement.
