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

Chapter 6 Introduction To SQL (Part 1)

Uploaded by

ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 6 Introduction To SQL (Part 1)

Uploaded by

ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Introduction to SQL

part 1
Roadmap
• An overview of SQL
• How to login in to Oracle using SQL*Plus
• How to create tables and defining column
data types
• How to insert and delete rows
• How to update rows

2
SQL Overview
• Structured Query Language
– The standard for using relational database
management systems (RDBMS)
– Declarative and high level: user only specifies
what he wants (to create, to query, to change);
DBMS executes it
– Results also in table format
– SQL is case insensitive

3
SQL Environment
• Schema
– The structure that contains descriptions of
objects created by a user (base tables, views,
constraints)
– In Oracle, each user has his own schema
– After login, by default you only see your schema
– Usually you can not see other’s schema unless
other user gives you permission

4
SQL Environment
• Catalog
– A set of schemas that constitute the
description of a database
– Catalog is stored as tables
– Example:
• Tab or user_tables: where the tables user
created are stored
• Can use SQL to find out schema information
(will discuss later)

5
General Steps of Database
Implementation
• Create tables (including columns, primary
key constraints, foreign key constraints)
• Insert rows into those tables
• Implement required business function
using SQL statements (select, insert,
delete, update)

6
General Steps of Database
Implementation
• Given a business function
– A search function (e.g., search for
employees), write select (search) statement
– An update function (e.g., raise the salary of
an employee), write update statement
– A delete function (e.g., delete an employee),
write delete statement
– An insert function (e.g., insert a new
employee), write an insert statement

7
SQL Table & Column Names

• NOTE: SQL names are case insensitive, i.e. you can


use capital or small letters.
• Are the following the same?
Table name: Emp vs. emp
Attribute name: emp_name vs. empname vs. EMP_NAME
Attribute value: 'John' vs. 'john'

8
SQL Table & Column Names

• Names must start with a letter, followed


by letter, digit, or _ (space, - not
allowed)
• Are the following names valid?
(separated by comma)
empname1, emp name 1, 1emp_name,
emp_name1, emp-name1,

9
SQL Table & Column Names

• Make names descriptive, and separate


words with underscores
– Example: cust_id
• Can not use SQL reserve words: select,
from, where, group, order,…
– Is the following table name valid?
Order, Orders, Order_T

10
Column Data Types
• String types
– CHAR(n) – fixed-length character data,
n characters long Maximum length = 2000 bytes
• Usually only used for short strings, otherwise waste space
• Return an error if try to insert a string with size > n
– VARCHAR(n) – variable length character data,
maximum 4000 bytes
• When strings are relatively long and have different lengths

11
Column Data Types
• Use '' to represent the value of a string,
E.g., add IT department to dept table:
insert into dept
values (1,'IT');
• Use notepad or wordpad when writing the single
quote
• Single quote in PowerPoint or word does not
work (wrong format)
• It is a good habit to save all your SQL statements
(especially create table and insert) in a file

12
Column Data Types

• Numeric types
– NUMBER – general purpose numeric data type
– INTEGER – integer
– FLOAT – floating point in scientific notation
• You may specify precision (number of
digits) if necessary (usually not necessary)
• E.g., number(10) means a number with up
to 10 digits

13
Column Data Types
• Date type
create table emp (
eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references dept(did));

14
Column Data Types
• When representing a value of date type
– Oracle use 'DD-MON-YY' format, where DD is
day of month, MON is 3 letter abbreviation of
month, YY is 2 digits year (1950-2050)
insert into emp
values(1,'jeff',1, '01-JAN-05',70000);

15
Column Data Types
• A better format (more natural)
Date 'YYYY-MM-DD', where Date is a prefix,
E.g.,
insert into emp
values(1,'jeff',1,date '2005-1-1',70000);

16
Table Creation
create table table-name(
attribute-name type [not null],

attribute-name type [not null],
integrity_constraint1,
...,
integrity_constraintk);
[] is optional
17
Table Creation
• Where integrity constraint looks like
– Constraint constraint_name primary key
(column1, …)
– Constraint constraint_name foreign key
(column 1, …)
references table_name
(column 1, …)

18
Syntax
– SQL is not case sensitive
– Last column has no comma
– Semi colon represents end of each statement
(can run several statements in one execution)
– Comments:
-- comments on the same line
/* comments that span multiple lines */

19
Example
Drop table emp;
Drop table dept;

create table dept (


did int,
dname varchar(30),
primary key (did)
);
create table emp (
eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references dept(did));

20
Exercise
• Create a table proj with four columns:
– Pid, int type
– Pname, varchar(30) type
– Startdate, date type
– Enddate, date type
– pid is the primary key

21
Exercise
• Create a table emp_proj with two columns
– Eid
– Pid
– Primary key is eid and pid
– Eid is a foreign key refers to emp(eid)
– Pid is a foreign key refers to proj(pid)

22
Common Errors
• If a table already exists, can not create it
again (need to drop it first)
• Once a table is created, it will stay there
until you drop it. So there is no need to
create the same table next time you login

23
Common Errors
• When specify a column, it is
Column-name type
Not type column-name
• Separate columns and constraints using
comma, no comma for last
• Need to specify length for varchar and
char type column

24
Drop A Table
• Drop table dept
• A good habit to drop a table before
create a table
– Otherwise if an old table with same
name exists, can not create the new
table

25
Saving Table Changes
• Changes made to table contents are not physically saved
on disk until:
– Database is closed
– Program is closed
– COMMIT command is used
• Syntax:
– COMMIT;
• Will permanently save any changes made to any table in
the database
• Remember to type commit after changing database

26
Check What Have Been Created
• Select table_name from user_tables;
• Desc table_name;
• select constraint_name, constraint_type
from user_constraints where
table_name='XXX';

27
Insert
• Insert:
– Template:
insert into table-name values(values for all
columns, separated by comma)
– Values in the column order
– Don’t forget ‘values’ keyword
– Don’t forget how to represent strings and date
E.g.,
insert into dept values (1,'IT');
insert into dept values (2, 'HR');

28
Insert
create table emp (
eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references dept(did));
insert into emp
values(1,'jeff',1,date '2005-1-1',70000);

insert into emp


values(2,'susan',2,date '2005-6-1',50000);

29
Check What Have Been Inserted
Select * from table_name
E.g., select * from emp;
Select * from dept

30
Common Errors
• Forget 'values' keyword before ()
• Column values not in the correct order
• Forget to quote string values
• Forget to use appropriate date format
• Forget using comma to separate column
values
• Insert the same row multiple times when
there is primary key constraints

31
Integrity Constraints in Create Table

Example:
create table dept (
did int,
dname varchar(30),
primary key (did)
);

32
Integrity Constraints in Create Table
• Can foreign key be NULL?
– E.g. CID in orders table is null
• Can foreign key points to an non existing
row in the primary table?
– E.g. CID in orders table equals a CID that can
not be found in Customer table

33
Integrity Constraints in Create Table

Example: create constraints the same time when


create table
create table emp (
eid int,
ename varchar(30),
did int, -- department id
hiredate date,
salary number,
primary key (eid),
foreign key (did) references
dept(did));

34
Drop Table
• Drop table table-name
E.g.,
Drop table emp;
Drop table dept;
• Order of drop table
– The reverse order of create or insert

35
Why?
• Can not drop a table when it is referenced
in some foreign key constraints
E.g., suppose emp(did) references
dept(did), can we drop dept?
• Solution: drop table in the reverse order of
creating them

36
Order of Create Table?

37
Create Customer, Product, Order, and then Ordier_line

38
Order of Drop Table?

39
Drop order_line, order, then customer and product

40
Order of Insert/Delete
• Order of insert is the same as create
• Order of delete is the same as drop

41
Delete
• Format: Delete from table-name [where
condition]
– [] represents optional
– Do not forget ‘from’ keyword
• The where clause specifies which row to
delete
• Without where clause, all rows deleted (be
careful!)

42
Example
• Delete from emp where eid = 1
Remove rows in emp table with eid equals 1
• Delete from emp
remove all rows in employee

43
Common Errors
• Wrong:
Delete * from table_name;
Delete column_name from table_name;
Why: whole row is deleted, so no need to specify
which column gets deleted
• Forget date prefix when use a date value in
where clause
• Be careful when using delete without where
clause (all rows get deleted!)

44
Update
• Update table-name
set column-name = new-value, column-name =
new value…
where …
The where condition specifies which row to update
The set clause updates the columns
Separate each column with comma (last column
has no comma)

45
Example
• Increase the salary of 'Jeff' by $1000
Update emp
Set salary = salary + 1000
Where ename = 'jeff';

46

You might also like