Download SQL Introduction

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

Tandem Computers wikipedia , lookup

DBase wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

Functional Database Model wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Null (SQL) wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
SQL
• Structured Query Language (SQL) is an industrystandard language used for creating, updating and
querying relational databases
• SQL operations are based on relational algebra that
underlies relational database design
• SQL is a 4th Generation non-procedural language
(whereas C and COBOL are 3rd generation
procedural languages; Assembler language is 2nd
generation; Machine language is 1st generation)
• SQL is non-procedural – language does not contain
procedural constructs like loops, if-then-else,
functions, etcetera
Extended Versions of SQL
• Standard SQL is available on all relational DBMS
including IBM’s DB2, Oracle Corporation’s Oracle,
Microsoft’s SQL Server and Access, Open Source’s
MySQL
• However most Database Management Systems
(DBMS) provide non-standard extensions to SQL
that include procedural elements
• IBM’s extended versions of SQL include SQL/400
and SQL/DS
• Oracle’s extension to SQL is called PL/SQL
• SQL Server’s extension is called Transact-SQL
SQL Subsets
• 3 subsets of SQL statements:
– Data Definition Language (DDL) : used to define
objects such as collections, tables and views
– Data Manipulation Language (DML): Used to
modify data in a database
– Data Control Language (DCL): used to control
access to database objects
• Main focus in DBS201: DDL to define a database
• Main focus of DBS301: DML to retrieve and
manipulate data in a database
SQL DDL Statements (DBS201)
Data Definition Language (DDL) is used to define
objects:
• CREATE (COLLECTION, TABLE or VIEW): defines
a collection, table, or view to the DBMS
• DROP (COLLECTION, TABLE or VIEW): removes
definition of a collection, table or view from the
DBMS
• ALTER TABLE: modifies the definition of a table
SQL DML Statements (DBS201)
• INSERT: adds a row of data to a table
• SELECT: displays data from one or more tables or
views (presented in part in DBS201 – will mainly
be covered in DBS301)
• DELETE: removes data from a table
Create Table with Primary Key
Syntax:
CREATE TABLE table_name
( fieldname datatype (length, decimals),
fieldname2 datatype (length, decimals,
fieldname3 datatype (length, decimals),
CONSTRAINT constraint_name PRIMARY KEY (column
name) )
Example:
CREATE TABLE STUDENT
(StudNo
NUMERIC (9, 0),
LastName
CHAR (20),
First_Name
CHAR (20),
Program
CHAR (4)
CONSTRAINT Student_PK PRIMARY KEY(StudNo) )
Insert Row of Data into a Table
Syntax:
INSERT INTO table_name VALUES(value1,
value2…) where number, sequence and data-type
of values must correspond exactly to columns in
table
Example:
INSERT INTO Student
VALUES(123456789, ‘Wong’, ‘Susan’, ‘CPD’)
Insert Row of Data into a Table
Syntax:
INSERT INTO table_name(Column_name2,
column_name1) VALUES(value2, value1…) where
number and sequence of values does not have to
correspond to columns in table
Example:
INSERT INTO Student(LastName,StudNo,FirstName)
VALUES(‘Smith’, 111111111, ‘Bill’)
Exercise
• Write the SQL command to create a table called
Supplier. It should have 3 columns, Supplier
Number - a 5-digit number, Supplier Name – max
length 25 characters, Supplier Rating – a number
from 1 to 5. The primary key will be supplier
number.
• Write the SQL command to insert a row of data
into the supplier table.