Download SQL Commands Lecture

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft SQL Server wikipedia , lookup

Database wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

PL/SQL wikipedia , lookup

SQL wikipedia , lookup

Functional Database Model wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Join (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
SQL COMMANDS
Insert, Update and the rest…
Lecture Objectives
Utilize the standard query language (SQL) to perform
the following tasks:
Add Rows to the Database
Change Existing Rows in the Database
Delete Rows from the Database
Create, Alter and Remove Tables from the
Database
Insert Command
Allows the insertion of data into a table one row at a
time
Used for new tables, or tables with existing data
Syntax 1:
Name of the table
INSERT INTO <tbl_name>
VALUES (attrib_value1, attrib_value2,…)
Values for all attributes in the table
What about NULL values?
Syntax 2:
Column cannot be specified as not null
INSERT INTO <tbl_name>
VALUES (attrib_value1, attrib_value2, NULL, …)
Insert Command
Alternate syntax
Syntax 3:
INSERT INTO <tbl_name>(col1, col2, …)
VALUES (attrib_value1, attrib_value2,…)
Does not need to be
all columns, just
those columns
specified as not null
with no default value
All other columns will
be inserted as null or
their default value
When will Insert NOT Work?
Update Command
Modify an attribute of one or more rows in
a given table
Syntax:
One or more changes
to attributes
UPDATE <tbl_name>
SET columnname = value [, columnname=value,..]
[WHERE CLAUSE]
Where clause acts
just like a select
where clause with =,
LIKE, sub-queries etc.
When will Update NOT Work?
Delete Command
Removes rows from a single table
Table name to delete
rows from
Syntax:
DELETE FROM <tbl_name>
[WHERE CLAUSE]
Where clause acts
just like a select
where clause with =,
LIKE, sub-queries etc.
Will delete ever remove from more than
one table?
Create Table Command
Creates a new relation/table by giving the
name, attributes and constraints on the
table
This is a “water-downed” version of the
command
Name of New Table
Syntax:
CREATE TABLE <tbl_name> (
column1
datatype
[constraint],
column2
datatype
[constraint],
.
Column Names
.
PRIMARY KEY (column1, column2, …)
Set Primary and
FOREIGN KEY (column1, column2, …)
Foreign Keys
REFERENCES <tbl_name2>)
Create Table Command
Data types – a few examples

Numeric Data types
NUMBER(I,D)
INTEGER
SMALLINT
DECIMAL(I,D)

Character Data types
CHAR(L)
VARCHAR(L)

Date
DATE
Alter Table Command
Change the table structure in a preexisting table
Syntax 1:
Table name to Alter
ALTER TABLE <tbl_name>
[ADD|MODIFY] (columnname datatype)
Add or Change
Column Settings
Syntax 2:
ALTER TABLE <tbl_name>
DROP COLUMN <column_name>
Remove a Column
from the table
Note: more options available for the alter
command
Drop Table Command
Remove the table and its data from the
database
Table name to remove
from the database
Syntax:
DROP TABLE <tbl_name>
Examples
Salesperson
Employee ID
Name
Office
27
Rodney Jones Toronto
44
Goro Azuma
Tokyo
35
Francine Moire Brussels
37
Anne Abel
Tokyo
Manager
Employee ID
Name
Office
12
Brigit Sanchez Toronto
99
Mary Chen
Brussels
37
Anne Abel
Tokyo