View
220
Download
2
Embed Size (px)
Citation preview
Overview Relational Databases and SQL
Pertemuan 1
Matakuliah : T0413/Current Popular IT IITahun : 2007
2
AGENDA:•Review Relational Databases•Overview SQL•Creating a Database•Creating, Changing, and Dropping Tables
Book:Mastering SQL by Martin GruberSybex (2000)Chapter : 1 - 3
3
Review Relational Databases• What is a Relational Database?
– It is a body of persistent information stored in two-dimensional tables (by a computer program), where the data survives the terminations of a program or user session that created it.
• A simple way to picture a relational database is to think of an address book. Suppose you were to format this address book as a tables with rows and columns, each row would corresponds to a certain individual.
4
Review Relational Databases (cont’d)• However, what if there were more than one
individual named Celia Brock? Or Gerry Farish ?• You should assign each person a unique identifier
( a value that is different for each person)• It is called Primary Key• Table with Primary Key added Client Table :
5
The need of Second Table• Suppose we need to add a telephone number column to
Client table.• Most people have more than one telephone number
(fax, cell phone, etc).• Solution : build another table• Client_Phone table :
• Client_Phone table also contains Primary Key from Client table
• The ID_Num in Client_Phone table is called Foreign Key, because it references the Primary Key in Client table
6
The need of Second Table (cont’d)• If all the Foreign Keys values in Client_Phone table
reference values that are actually present in Client table, the system has Referential Integrity.
• We also need a Primary Key for the Client_Phone table.• A Primary or Foreign Key need not be a single column.• We can combine the ID_Num and Phone columns as
the Primary Keys for Client_Phone table.• As long as the combination of both columns are always
unique.• A key of more than one column is referred by :
– Multicolumn Key, or– Composite Key, or– Concatenated Key.
7
Joining Tables
• When we extract the information contained in the two separate tables (Client and Client_Phone), we can link each Foreign Key values that match the values in Primary Key.
• An operation that extracts information from database is called a query.
• A query that extracts data from more than one table at the same time by relating columns in one table to columns in the other(s) is called a Join.
• Queries are implemented in SQL with the SELECT statement.
8
How a Relational Database Fits Together
• Tables were grouped in a database .• It is called Schema ( a group of interrelated tables).• A database can contain any number of Schemas.• Relational systems allow you to work with the data
at a higher level, all operations on the data are handled by a program called DBMS, which responds only to statements expressed in a high-level language.
• DBMS = Database Management System
9
Overview SQL• SQL is a language oriented specifically around
relational databases.• Forms of SQL:
– Interactive SQL– Static SQL– Dynamic SQL
• The Subdivisions of SQL:– DDL (Data Definition Language)– DML (Data Manipulation Language)– DCL (Data Control Language)
10
Overview SQL (cont’d)• Various Types of Data (datatype) can be divided
into the following categories:– Text– Numeric– Datetime– Binary, etc
• Two very important concepts,1. Predicate : a set of criteria use to decide whether a
statement will or will not be executed against a particular row of a table.
2. Query : it is a request for information from the database. In other words, a SELECT statement.
11
Overview SQL (cont’d)
• Basic SQL Statements: SELECT snum, sname, city, comm
FROM SalespeopleWHERE sname = ‘Peel’;
DELETE FROM SalespeopleWHERE snum = 1001;
12
Creating a DatabaseExample :CREATE DATABASE MYDBASEON PRIMARY ( OPTIONAL : primary
filegroup berisi system tablesNAME = MYDBASE_DATA,FILENAME =”C:\mssql\data\mydb.mdf” nama file yang
berlaku pada OS, ditulis full pathnameSIZE=20MB, besar awal dari file dengan satuan MB
(default) atau KB. MAXSIZE=40mb, maksimum besar file yang dapat dicapai
dalam MB atau KBFILEGROWTH=1MB pertumbuhan file dalam MB
13
Modifying a DatabaseExample :
ALTER DATABASE MYDBASEMODIFY FILE ( NAME = MYDBASE_DATA, SIZE = 60MB );
14
Creating, Changing, and Dropping Tables
CREATING TABLES• Tables are being defined using CREATE TABLE
command• It creates an initially empty tables• The command defines a table name as describing a
set of named columns in a specified order.• It also defines the data types and sizes of the
columns.• Each table must have at least one column.• Syntax:
CREATE TABLE tablename({columnname datatype[(size)]},…);
15
Creating, Changing, and Dropping Tables (cont’d)
• Example:CREATE TABLE Salespeople
( snum INTEGER, sname CHAR(10), city CHAR(10), comm DECIMAL);
• Order of columns are determined by the order specified inside the CREATE TABLE command.
• By using INSERT statement, we can insert values into the table that we have created before.
16
Creating, Changing, and Dropping Tables (cont’d)
CHANGING TABLES• ALTER TABLE statement is used to change the
definitions in a table.• ALTER TABLE can do the following:
– Add a column to a table– Drop a column from a table– Add a table constraint to a table– Drop a constraint from a table– Add a default value to a column– Drop a default value from a column
17
Creating, Changing, and Dropping Tables (cont’d)
• Syntax:ALTER TABLE tablename{ ADD [COLUMN] column definition }| { ALTER [COLUMN] column name
{ SET DEFAULT default option } | {DROP DEFAULT } }
| { DROP [COLUMN] column name }| { ADD table constraint definition }| { DROP CONSTRAINT constraint name };
• Example:– ALTER TABLE Salespeople ADD fname CHAR(10);– ALTER TABLE Salespeople ALTER COLUMN city ADD DEFAULT
‘London’;
18
Creating, Changing, and Dropping Tables (cont’d)
DROPPING TABLES• Dropping tables is actually a two-steps process:
– Empty the table of any data by using the DELETE statement– Destroy the definition of the table by using DROP TABLE
statement
• Syntax:– DROP TABLE tablename [RESTRICT | CASCADE];
• The RESTRICT or CASCADE clause refers to what happens when other objects (such as Views) exist that depend on this table.– If RESTRICT is specified then DROP is disallowed.– If CASCADE is specified then dependent objects are automatically
dropped as well.
• In the end, table are no longer recognized in the schema.
19
End ofOverview Relational Databases and SQL
Thank you