Database Section (2) - 3CS
Database Section (2) - 3CS
Database Section (2) - 3CS
Lab Content:
- Relational model and relational mapping
(Mapping for Students’ Case Study)
- Introduction to SQL
o What is SQL
o SQL data types, nulls
• Relation is a table with columns & rows. Holds information about entities.
• Attribute is a named column of a relation.
• Tuple is a row of a relation.
• Degree of a relation is the number of attributes it contains.
• Cardinality of a relation is the number of tuples it contains.
ER ➔ Relational Model
Entity Type
• Represent each entity type with a relation.
• Entity type attributes become the relation attributes.
STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)
DEPARTMENT (DeptNo, Department Name, Location)
1:M Relationship
• Identify a participating entity type (S) on the m-side.
• Include the PK of the other entity type (T) as a FK in S.
• Add attributes that describes the relationship to S.
EMPLOYEE(EmpNo, Lname, Fname, DOB, BrnNo)
BRANCH(BrnNo, Name)
M:N Relationship
• Create a relation R to represent the relationship.
• Include the PK of participating entity types (T & S) as FK in R. The combination of the
two FK will form the PK of R.
• Add attributes that describes the relationship to R.
EMPLOYEE(EmpNo, Lname, Fname, DOB)
PROJECT(ProjNo, Name)
Work-on(EmpNo,ProjNo, hours)
n-ary Relationship
• Create a relation R to represent the relationship.
• Include the PK of the participating entities as FK in R. The combination of all FK form
the PK of R.
• Add attributes that describes the relationship to R.
BUSINESS(BizNo) LAWYER(LawNo) SUPPLIER(SupNo)
contract(BizNo, SupNo, LawNo, StartDate, EndDate)
Composite Attribute
Include its simple components in the relation.
EMPLOYEE(EmpNo, Fname, initial, Lname, DOB)
MultiValue Attribute
• Suppose A is a relation that contains the multivalued attribute.
• Create a relation R to represent the attribute.
• Include the PK of A as FK in R.
• The PK of R is the combination of the PK of A (FK) & the multivalued attribute.
EMPLOYEE(EmpNo, DOB)
TELEPHONE(EmpNo, tel_no)
Introduction to SQL
What is SQL
• SQL (pronounced "ess-que-el") stands for Structured Query Language.
• SQL is used to communicate with a database.
• According to ANSI (American National Standards Institute), it is the standard language for
relational database management systems.
• SQL statements are used to perform tasks such as update data on a database, or retrieve data from
a database.
• Some common relational database management systems that use SQL are:
o Oracle,
o Sybase,
o Microsoft SQL Server,
o Access,
o Ingres, etc.
• Although most database systems use SQL, most of them also have their own additional
proprietary extensions that are usually only used on their system.
What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
TIMESTAMP Stores year, month, day, hour, minute, and second values
text Variable width character string. Maximum 2GB of text 4 bytes + number of
data chars
nchar Fixed width Unicode string. Maximum 4,000 characters Defined width x 2
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 bytes
The n parameter indicates whether the field should hold 4 or 8
bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte
field. Default value of n is 53.
datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 100 6-8 bytes
nanoseconds
smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute 4 bytes
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes
DATE - format YYYY-MM-DD
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
timestamp Stores a unique number that gets updated every time a row gets
created or modified. The timestamp value is based upon an internal
clock and does not correspond to real time. Each table may have only
one timestamp variable
TIMESTAMP - format: a unique number
Suppose that the "Address" column in the "Persons" table is optional. This means that if we
insert a record with no value for the "Address" column, the "Address" column will be saved with
a NULL value.
How to test for NULL values?
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
Use the IS NULL and IS NOT NULL operators instead.