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

Structured Query Language

The document discusses SQL data types and their descriptions. It also covers SQL commands like CREATE, ALTER, DROP and SELECT that are used to define and manipulate database structures and data. The WHERE clause and operators like BETWEEN, IN, LIKE are described which can be used to filter data in SELECT queries.

Uploaded by

Ishå Gûjår
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Structured Query Language

The document discusses SQL data types and their descriptions. It also covers SQL commands like CREATE, ALTER, DROP and SELECT that are used to define and manipulate database structures and data. The WHERE clause and operators like BETWEEN, IN, LIKE are described which can be used to filter data in SELECT queries.

Uploaded by

Ishå Gûjår
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

SQL

Structured Query Language


SQL Datatypes
• A data type defines what kind of value a
column can hold: integer data, character data,
monetary data, date and time data, binary
strings, and so on.
• Each column in a database table is required to
have a name and a data type.
• An SQL developer must decide what type of
data that will be stored inside each column
when creating a table.
Commands in RunSQL
Connect system
Password : tiger
Create user
CREATE USER books_admin IDENTIFIED BY MyPassword;
Grant Permissions
GRANT CONNECT, RESOURCE, DBA TO books_admin;
Data Type Description
CHAR(size) Holds a fixed length string (can contain letters, numbers, and special
characters). The fixed size is specified in parenthesis. Can store up to
255 characters
VARCHAR(size) Holds a variable length string (can contain letters, numbers, and
special characters). The maximum size is specified in parenthesis.
Can store up to 255 characters. Note: If you put a greater value than
255 it will be converted to a TEXT type
TEXT Holds a string with a maximum length of 65,535 characters
numeric(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1
to 10^38 –1.
The p parameter indicates the maximum total number of digits that
can be stored (both to the left and to the right of the decimal point).
p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to
the right of the decimal point. s must be a value from 0 to p. Default
value is 0

INT(size) -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*.


The maximum number of digits may be specified in parenthesis
Data Type Description
FLOAT(size,d) A small number with a floating decimal point. The maximum
number of digits may be specified in the size parameter. The
maximum number of digits to the right of the decimal point is
specified in the d parameter
DOUBLE(size,d) A large number with a floating decimal point. The maximum
number of digits may be specified in the size parameter. The
maximum number of digits to the right of the decimal point is
specified in the d parameter
DATE() A date. Format: YYYY-MM-DDNote: The supported range is from
'1000-01-01' to '9999-12-31'

DATETIME() *A date and time combination. Format: YYYY-MM-DD


HH:MI:SSNote: The supported range is from '1000-01-01 00:00:00'
to '9999-12-31 23:59:59'

TIME() A time. Format: HH:MI:SSNote: The supported range is from '-


838:59:59' to '838:59:59'
YEAR() A year in two-digit or four-digit format.Note: Values allowed in
four-digit format: 1901 to 2155. Values allowed in two-digit
format: 70 to 69, representing years from 1970 to 2069

TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of


seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format:
YYYY-MM-DD HH:MI:SSNote: The supported range is from '1970-01-
01 00:00:01' UTC to '2038-01-09 03:14:07' UTC
Data Definition Language
• These SQL commands are used for creating,
modifying and dropping the structure of
database objects.
• The commands are CREATE, ALTER, DROP,
RENAME and TRUNCATE
SQL CREATE TABLE Statement

The CREATE TABLE statement is used to create new table in a database.


• Syntax :CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
• The column parameters specify the names of the columns of the table.
• The datatype parameter specifies the type of data the column can hold
(e.g. varchar, integer, date, etc.).
SQL CREATE TABLE Example

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Create Table Using Another Table

• A copy of an existing table can be created using a combination


of the CREATE TABLE statement and the SELECT statement.
• The new table gets the same column definitions. All columns
or specific columns can be selected.
• If you create a new table using an existing table, the new table
will be filled with the existing values from the old table.
Syntax :CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
SQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.


• Syntax: DROP TABLE table_name;
Eg: DROP TABLE Shippers;
SQL TRUNCATE TABLE

• The TRUNCATE TABLE statement is used to delete the data


inside a table, but not the table itself.
Syntax
TRUNCATE TABLE table_name;
SQL ALTER TABLE Statement
• The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
• The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTER TABLE - ADD Column
• To add a column in a table, use the following syntax:
• ALTER TABLE table_name
ADD column_name datatype;
Eg:ALTER TABLE Persons ADD DateOfBirth date;
ALTER TABLE - DROP COLUMN
• To delete a column in a table, use the following syntax (notice that
some database systems don't allow deleting a column):
• ALTER TABLE table_name
DROP COLUMN column_name;
Eg: ALTER TABLE Persons DROP COLUMN DateOfBirth;
• ALTER TABLE - ALTER/MODIFY COLUMN
• To change the data type of a column in a table, use the
following syntax: ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Eg: ALTER TABLE Persons MODIFY COLUMN DateOfBirth year;
RENAME
• RENAME command is used to rename a table. Syntax:
RENAME <OLDTABLENAME> TO <NEWTABLENAME>
• Example: RENAME college To Col
DML ( Data Manipulation Language)
• DML modifies the database instance by inserting,
updating and deleting its data. DML is responsible
for all forms data modification in a database.
• SQL contains the following set of commands in its
DML section -
1. SELECT/FROM/WHERE
2. INSERT INTO/VALUES
3. UPDATE/SET/WHERE
4. DELETE FROM/WHERE
SELECT command
• The SELECT statement is used to select data
from a database.
• The data returned is stored in a result table,
called the result-set.
• SELECT Syntax:
SELECT column1, column2, ...
FROM table_name;
Eg: SELECT CustomerName,City FROM Customers;
• If you want to select all the fields available in
the table, use the following syntax:
SELECT * FROM table_name;
SELECT * FROM Customers;
SELECT DISTINCT Statement

• The SELECT DISTINCT statement is used to


return only distinct (different) values.
• Inside a table, a column often contains many
duplicate values; and sometimes you only
want to list the different (distinct) values.
• Syntax: SELECT DISTINCT column1, column2, ...
FROM table_name;
• SELECT DISTINCT Country FROM Customers;
SQL WHERE Clause
• The WHERE clause is used to filter records.
• The WHERE clause is used to extract only those
records that fulfill a specified condition.
• Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Eg:SELECT * FROM Customers
WHERE Country='Mexico';
SELECT * FROM Customers
WHERE CustomerID=1;
Operators in WHERE clause
Operators Decription
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written
as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
BETWEEN Operator
• BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND v
alue2;
• Example :SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
NOT BETWEEN

• To display the products outside the range of


the previous example, use NOT BETWEEN:
• Example: SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
SQL LIKE Operator

• The LIKE operator is used in a WHERE clause


to search for a specified pattern in a column.
• There are two wildcards used in conjunction
with the LIKE operator:
1. % - The percent sign represents zero, one, or
multiple characters
2. _ - The underscore represents a single
character
• LIKE Syntax: SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%_%' Finds any values that start with "a" and
are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and
ends with "o"
• SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
• SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
• SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
• SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
SQL IN Operator

• The IN operator allows you to specify multiple


values in a WHERE clause.
• The IN operator is a shorthand for multiple OR
conditions.
• IN Syntax: SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
• SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
• SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', '
UK');
SQL AND, OR and NOT Operators

• The WHERE clause can be combined with AND, OR,


and NOT operators.
• The AND and OR operators are used to filter records
based on more than one condition:
• The AND operator displays a record if all the
conditions separated by AND is TRUE.
• The OR operator displays a record if any of the
conditions separated by OR is TRUE.
• The NOT operator displays a record if the
condition(s) is NOT TRUE.
AND
• AND Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condit
ion3 ...;
Eg:SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
OR
• OR Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition
3 ...;
• SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
NOT
• NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
• SELECT * FROM Customers
WHERE NOT Country='Germany';
Combining AND, OR and NOT

• SELECT * FROM Customers


WHERE Country='Germany' AND (City='Berlin'
OR City='München');
• SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Co
untry='USA';
SQL UPDATE Statement
• The UPDATE statement is used to modify the
existing records in a table.
• UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
• Eg: UPDATE Customers
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt‘ WHERE CustomerID = 1;
UPDATE Multiple Records

• It is the WHERE clause that determines how


many records that will be updated.
Example
• UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
Update Warning!

• Be careful when updating records. If you omit


the WHERE clause, ALL records will be
updated!
• Example
• UPDATE Customers
SET ContactName='Juan';
SQL DELETE Statement

• The DELETE statement is used to delete existing records in a


table.
• DELETE Syntax
DELETE FROM table_name
WHERE condition;
Eg: DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
Note: Be careful when deleting records in a table! Notice the
WHERE clause in the DELETE statement. The WHERE clause
specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy