Sunteți pe pagina 1din 6

What is a Database?

• A Database is a collection of related data.


Data - any known facts that can be recorded and have implicit meaning.
Example: Name, Telephone no., Address
Database or Not!
• Identification Card – (Not a database)
• Attendance – (Not a database)
• Phonebook – (Database)
• Cellular phone – (Database)
Implicit Properties of a Database
1. A database is a logically coherent (connected) collection of data with some inherent (derived) meaning.
2. A database is designed, built, and populated with data for a specific purpose. It has an intended group of users
and some preconceived applications (use) in which these users are interested.
3. A database represents some aspect of the real world.
Database Management Systems (D.B.M.S.)
• A Database Management System (DBMS) is a collection of programs that enables users to create and maintain
a database.
Sample of D.B.M.S.
• Microsoft Access
• Microsoft SQL Server
• MySQL
• DB2
• Oracle
• Postgre
Database and D.B.M.S.
• Database is a source of data or information
• Database Management Systems (DBMS) are software tools designed specifically for creating, using,
manipulating and maintaining database.
Database Terminologies
1. Fields – refers to a column of information or a component of collectable data.
2. Records – refers to a complete set of information.
3. Tables – a facility that Database Management Systems use to store a set of Records.
Candidate Keys
• Candidate Keys are any Fields that will always contain a unique value for all records which we may use to refer
to the entire record.
Properties of Candidate Keys
1. Must be unique.
2. Must not contain NULL value. It must be in the nature of a field to be required a value to be a candidate key.
3. Must not change value over time.
Primary Keys
• The objective of identifying the candidate keys are for us to select from among the candidate keys, a suitable
Primary Key.
• Primary Keys are the one field in our table that makes the entire record unique.
• In the next slide, an ID No. is added as the table’s Primary Key.
Applications of Database
• Today, databases are used in many areas of the information age such as:
– E-business / E-commerce programs
– Day to day hardware like mobile phones.
– Games
– Automated business processes.
What are Database Applications
• A program that makes use of a database.
• Example of a database application registration process shown next slide…
Sources
• Fundamentals of database systems by ELMASRI
Basic S.Q.L. Statements
S.Q.L. - Structured Query Language
• SQL statements are commands that allow database users to manipulate records within the database by
Inserting, Updating, Retrieving or Deleting Records within the database.
The Select Command
• We use the Select command in SQL to retrieve records of information.
• The general syntax for constructing the SQL Select statement is:
Select [Field/s] from [Table]
• The following Select statement:
Select FirstName from People
• You may also choose to display multiple fields
Select IDNo, LastName, FirstName from People
• Or you can display all fields using the wildcard character *
Select * from People
The Where Clause
• The Where clause can be used with SQL statements to add criteria as to what records affect the SQL
command.
• The following SQL statement:
Select * from People where FirstName = ‘Jacky’
• Notice how the field value is enclosed in single quotes (‘), you need to enclose field values of type string/text
in a pair or single quotes.
• The SQL statement will only return records that meet the where clause’s criteria.
• You can also use other operators with the where clause. (i.e. >, >=, <=, <>, =)
Select * from People where IDNo < 1000006
• We do not need to enclose the value in a pair of single quotes if the data type of that field is numeric
The Like Operator
• Like has the same effect as = when used with string/text data type in a where clause, however, with like you
can introduce the wildcard character % within the string criteria of your where clause.
Using Like with Strings
• Look at the following SQL statement.
Select * from People where LastName like ‘B%’
The SQL statement returns all records that starts with ‘B’ and may end with anything or nothing at all, this is
what the wildcard character % represents.
• The SQL statement…
Select * from People where FirstName like ‘%o’
• You can also have something like this:
Select * from People where Address like ‘%in%’
Logical Operators
• Logical operators such as “And” or “Or” allow you to construct compound where clauses that gives additional
criteria to your SQL statements.
• For example, you can have something like this.
Select * from People where FirstName = ‘Fei’ And Address = ‘China’
Notice how the “And” logical operator requires both criteria to be true to return a record.
• The “Or” operator on the other hand requires only one of the criteria to be true to return the record. For
example:
Select * from People where FirstName = ‘Fei’ Or Address = ‘China’
With the “Or” logical operator, it is not necessary for both criteria to be true to return the record. If at least one
of the criteria is true the record is considered.
The Insert Into Command
• We can use the Insert Into command to add a new record into your table.
• The general syntax for the SQL Insert Into command is the following:
Insert Into [Table] Values ([Field Values separated by comma])
• You can insert a new record using the Insert Into command
Insert Into People Values (1000009, ‘Sy’, ‘Eisen’, ‘Paranaque’, ‘(0916)911-11-11’)
• This would insert a record into the table
• In the given syntax:
Insert Into [Table] Values ([Field values separated by comma])
• Field values must be given in the same order as they appear in the table.
Notice: Single quotes are required for string type field values
• The Insert Into command also has an alternative syntax.
Insert Into [Table] ([Field Name/s]) Values ([Field Value/s])
• This syntax allows selective field insertion to the record (meaning it only insert values on the specified fields
leaving the others untouched)
• This Insert Into command for example:
Insert Into People (IDNo, LastName, FirstName) Values (1000010, ‘Bartowski’, ‘Chuck’)
• Notice how the field names list matches the order of the field values list
The Update Command
• The Update command may be used to update existing field values within the table.
• The general syntax for the SQL Update command is the following
Update [Table]
Set [Field Name] = [Field Value]
• Try to guess what this Update command will do.
Update People
Set LastName = ‘Something’
This is not what we wanted!!!
• In order to select which record we want to be affected by the update, we will use the Where clause with our
update command.
Update People
Set LastName = ‘Sy’
Where IDNo = 1000009
• It’s also possible to update multiple fields in a record.
Update People
Set LastName = ‘A’, FirstName = ‘B’
Where IDNo = 1000009
The Delete Command
• The Delete command may be used to permanently remove a record from the table
• The syntax for the SQL Delete command is the following:
Delete From [Table] Where […]
Basic SQL Short Quiz Answer Key
Table Definition
• For all items, assume that you have the following table specifications
• Table Name: Customer

1.) Record Insertion Problem


• Write the SQL statement to insert the record below into that “Customer” table

INSERT INTO Customer


VALUES(1, ‘EisenCorp’,
‘630 Del Pan Street’, ‘Eisen Sy’)
2.) Record Insertion Problem
• Write the SQL statement to insert the record below into that “Customer” table

INSERT INTO Customer (CustomerID)


VALUES (2)
3.) Record Insertion Problem
• Write the SQL statement to insert the record below into that “Customer” table

INSERT INTO Customer


(CustomerID, ContactPerson)
VALUES (3, ‘Ebenezer Uy’)
4.) Record Insertion Problem
• Write the SQL statement to insert the record below into that “Customer” table

INSERT INTO Customer


(CustomerID, CompanyName, Address)
VALUES (4, ‘Almazora Metal Works’, ‘5862 Bulacan’)
5.) Record Updating Problem
• Write the SQL statement to edit the record below such that the new value assigned to one of the fields in the
record

UPDATE Customer
SET ContactPerson = ‘Marie Almazora’
WHERE CustomerID = 4
6.) Record Updating Problem
• Write the SQL statement to edit the record below such that new values are assigned to two of the fields in the
record

UPDATE Customer
SET CompanyName = ‘Uy Industries’, Address = ‘Quezon City’
WHERE CustomerID = 3
7.) Record Updating Problem
• Write the SQL statement to edit the record below such that new values are assigned to three of the fields in the
record

UPDATE Customer
SET CompanyName = ‘Starks Industries’, Address = ‘Malibu’, ContactPerson = ‘Tony Stark’
WHERE CustomerID = 2
8.) Record Selection Problem
• Write the SQL statement to retrieve all records from the “Customer” table, but show only the company names
from the table
SELECT CompanyName FROM Customer
9.) Record Selection Problem
• Write the SQL statement to retrieve all records from the “Customer” table, but show only the company names
and contact person from the table
SELECT CompanyName, ContactPerson FROM Customer
10.) Record Selection Problem
• Write the SQL statement to retrieve all records from the “Customer” table, but show only the customer id,
company names and address from the table
SELECT CustomerID, CompanyName, Address FROM Customer
11.) Record Selection Problem
• Write the SQL statement to retrieve all records and fields from the “Customer” table
SELECT * FROM Customer
12.) Record Deleting Problem
• Write the SQL statement to delete the record below. Use the primary key as the criteria for deleting records
DELETE FROM Customer
WHERE CustomerID = 4
13.) Record Deleting Problem
• Write the SQL statement to delete the record below. Use the primary key as the criteria for deleting records

DELETE FROM Customer


WHERE CustomerID = 2 CustomerID

S-ar putea să vă placă și