Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Data Manipulation Using SQL - Lec3

The document discusses SQL statements for data manipulation and describes the basic categories of SQL statements including data statements, schema statements, and transaction statements. It also provides examples of SQL statements like SELECT, INSERT, UPDATE, DELETE and ORDER BY.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Data Manipulation Using SQL - Lec3

The document discusses SQL statements for data manipulation and describes the basic categories of SQL statements including data statements, schema statements, and transaction statements. It also provides examples of SQL statements like SELECT, INSERT, UPDATE, DELETE and ORDER BY.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Manipulation Using

SQL
There are 3 basic categories of SQL
Statements
• SQL-Data Statements -- query and modify
tables and columns
– SELECT Statement -- query tables and views in the
database.
– INSERT Statement -- add rows to tables.
– UPDATE Statement -- modify columns in table
rows.
– DELETE Statement -- remove rows from tables.
There are 3 basic categories of SQL
Statements
• SQL-Schema Statements -- maintain schema
(catalog)
– CREATE TABLE Statement -- create tables .
– CREATE VIEW Statement -- create views .
– DROP TABLE Statement -- drop tables .
– DROP VIEW Statement -- drop views .

– GRANT Statement -- grant privileges on tables and


views to other users.
– REVOKE Statement -- revoke privileges on tables and
views from other users.
There are 3 basic categories of SQL
Statements
• SQL-Transaction Statements -- control
transactions.
– COMMIT Statement -- commit the current
transaction.
– ROLLBACK Statement -- roll back the current
transaction.
Language Structure
• SQL is a keyword based language. Each
statement begins with a unique keyword. SQL
statements consist of clauses which begin
with a keyword. SQL syntax is not case
sensitive.
• Names -- names of database elements:
tables, columns, views, users, schemas; names
must begin with a letter (a - z) and may
contain digits (0 - 9) and underscore (_) .
Language Structure

• Literals -- quoted strings, numeric values,


datetime values.
• Delimiters -- + - , ( ) = < > <= >= <> . * / || ? ;
Back to the Beginning
• The foundation of every Relational Database
Management System is a database object called table.
Every database consists of one or more tables, which
store the database’s data/information. Each table has
its own unique name and consists of columns and
rows.

• The database table columns (called also table fields)


have their own unique names and have a pre-defined
data types. Table columns can have various attributes
defining the column functionality (the column is a
primary key, there is an index defined on the column,
the column has certain default value, etc.).

• While table columns describe the data types, the table


rows contain the actual data for the columns.
Here is an example of a simple database
table, containing customers data. The first
row, listed in bold, contains the names of
the table columns
SQL WHERE
• The SQL WHERE clause is used to select data
conditionally, by adding it to already existing
SQL SELECT query.

• If we want to select all customers from our


database table, having last name 'Smith' we
need to use the above SQL syntax.
Equal
SELECT * FROM Customers WHERE LastName = 'Smith‘

Not Equal
SELECT * FROM Customers WHERE LastName <> 'Smith‘

Greater than
SELECT * FROM Customers WHERE DOB > '1/1/1970'

Greater or Equal
SELECT * FROM Customers WHERE DOB >= '1/1/1970‘

Less than
SELECT * FROM Customers WHERE DOB < '1/1/1970‘

Less or Equal
SELECT * FROM Customers WHERE DOB <= '1/1/1970‘

Similar to
SELECT * FROM Customers WHERE Phone LIKE '626%‘

Defines a range
SELECT * FROM Customers WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975'
SQL INSERT INTO
• The SQL INSERT INTO syntax has 2 main forms
and the result of either of them is adding a
new row into the database table.
SQL INSERT INTO
• The first syntax form of the INSERT INTO SQL
clause doesn't specify the column names
where the data will be inserted, but just their
values:

INSERT INTO Table1 VALUES (value1, value2,


value3)
SQL INSERT INTO
• The second form of the SQL INSERT INTO
command, specifies both the columns and the
values to be inserted in them
INSERT INTO Table1
(Column1, Column2, Column3)
VALUES (Value1, Value2, Value3);
• The number of the columns in the second
INSERT INTO syntax form must match the
number of values into the SQL statement,
otherwise you will get an error.
SQL INSERT INTO
• If we want to insert a new row into our
Customers table, we are going to use one of the
following 2 SQL statements
INSERT INTO Customers VALUES ('Peter', 'Hunt',
'peter.hunt@tgmail.net', '1/1/1974', '626 888-
8888')

INSERT INTO Customers (FirstName, LastName,


Email, DOB, Phone) VALUES ('Peter', 'Hunt',
'peter.hunt@tgmail.net', '1/1/1974', '626 888-
8888')
SQL INSERT INTO
SQL UPDATE
• The SQL UPDATE clause changes the data in
already existing database row(s) and usually we
need to add a conditional SQL WHERE clause to
our SQL UPDATE statement in order to specify
which row(s) we intend to update.
• If we want to update the Mr. Steven Goldfish's
date of birth to '5/10/1974' in our Customers
database table
UPDATE Customers SET DOB = '5/10/1974’ WHERE
LastName = 'Goldfish' AND FirstName = 'Steven'
SQL UPDATE

• If we don’t specify a WHERE clause in the SQL


expression above, all customers' DOB will be
updated to '5/10/1974', so be careful with the
SQL UPDATE command usage.
SQL DELETE
• So far we’ve learnt how to select data from a
database table and how to insert and update
data into a database table. Now it’s time to
learn how to remove data from a database.
Here comes the SQL DELETE statement!
• The SQL DELETE command has the following
generic SQL syntax:
DELETE FROM Table1 WHERE Some_Column =
Some_Value
SQL DELETE
• If you skip the SQL WHERE clause when
executing SQL DELETE expression, then all the
data in the specified table will be deleted. The
following SQL statement will delete all the
data from our Customers table and we’ll end
up with completely empty table:
DELETE FROM Table1
• If you specify a WHERE clause in your SQL
DELETE statement, only the table rows
satisfying the WHERE criteria will be deleted:
SQL DELETE
DELETE FROM Customers WHERE LastName =
'Smith’

• The SQL query above will delete all database


rows having LastName 'Smith‘ on Customers
table.
SQL ORDER BY
• The SQL ORDER BY clause comes in handy
when you want to sort your SQL result sets by
some column(s). For example if you want to
select all the persons from the already familiar
Customers table and order the result by date
of birth, you will use the following statement:

SELECT * FROM Customers ORDER BY DOB


SQL ORDER BY

• Rows are sorted in ascending order by the DOB


column, but what if you want to sort them in
descending order?
• To do that you will have to add the DESC SQL
keyword after your SQL ORDER BY clause:
SELECT * FROM Customers ORDER BY DOB DESC
SQL ORDER BY
• If you don't specify how to order your rows,
alphabetically or reverse, than the result set is
ordered alphabetically, hence the following to SQL
expressions produce the same result:
SELECT * FROM Customers ORDER BY DOB
SELECT * FROM Customers ORDER BY DOB ASC
• You can sort your result set by more than one
column by specifying those columns in the SQL
ORDER BY list. The following SQL expression will
order by DOB and LastName:
SELECT * FROM Customers ORDER BY DOB, LastName
SQL DISTINCT
• In a table, a column may contain many
duplicate values and sometimes you only want
to list the different (distinct) values.
• The DISTINCT keyword can be used to return
only distinct (different) values.

• SQL SELECT DISTINCT Syntax:


SELECT DISTINCT column_name,column_name
FROM table_name;
Constraints
• Constraint specifications add additional restrictions on the contents of
the table. They are automatically enforced by the DBMS. The column
constraints are:

• NOT NULL -- specifies that the column can't be set to null. If this constraint is not
specified, the column is nullable, that is, it can be set to null. Normally, primary key
columns are declared as NOT NULL.

• PRIMARY KEY -- specifies that this column is the only column in the primary key.
There can be only one primary key declaration in a CREATE TABLE. For primary keys
with multiple columns, use the PRIMARY KEY table constraint. See Entity Integrity
below for a detailed description of primary keys.

• UNIQUE -- specifies that this column has a unique value or null for all rows of the
table.

• REFERENCES -- specifies that this column is the only column in a foreign key. For
foreign keys with multiple columns, use the FOREIGN KEY table constraint. See
Referential Integrity below for a detailed description of primary keys.
The Table Constraints
• PRIMARY KEY -- specifies the set of columns that comprise the
primary key. There can be only one primary key declaration in a
CREATE TABLE Statement.

• UNIQUE -- specifies that a set of columns have unique values (or


nulls) for all rows in the table. The UNIQUE specifier is followed by a
parenthesized list of column names, separated by commas.

• FOREIGN KEY -- specifies the set of columns in a foreign key.

The predicate is evaluated as if the INSERT row were added to the


table. For UPDATE Statements, the predicate is evaluated as if the
row were updated. For DELETE Statements, the predicate is
evaluated as if the row were deleted
Note: A check predicate is only useful for DELETE if a self-
referencing sub query is used.

You might also like