0% found this document useful (0 votes)
75 views27 pages

3 - Chapter 2 - Relational Model of Data - P2

The document discusses how to define relation schemas in SQL. It covers topics such as defining relations like tables, views and temporary tables in SQL. It describes different data types in SQL like string, date/time, number data types. It explains how to create and modify databases and tables in SQL using commands like CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE. It also covers concepts like primary keys, unique keys, default values, identity columns and how to insert, update and delete data in tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views27 pages

3 - Chapter 2 - Relational Model of Data - P2

The document discusses how to define relation schemas in SQL. It covers topics such as defining relations like tables, views and temporary tables in SQL. It describes different data types in SQL like string, date/time, number data types. It explains how to create and modify databases and tables in SQL using commands like CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE. It also covers concepts like primary keys, unique keys, default values, identity columns and how to insert, update and delete data in tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Database Systems

Session 3
Chapter 2 - Relational Model of Data - Part 2
Defining a Relation Schema in SQL
Objectives

1 Understand how to use SQL to define relation schemas

2
2.3. Defining a relation Schema in SQL
2.3. Defining a Relation Schema in SQL

2.3.1. Relations in SQL


2.3.2. Data Types
2.3.3. Database
2.3.4. Simple Table Declarations
2.3.5. Modifying Relation Schemas
2.3.6. Default Values
2.3.7. Declaring Keys
2.3.1 Relations in SQL Server

3 kinds of relations in SQL:


 Stored relations (tables): a relation that exist in the
database and that can be modified by changing its
tuples, as well as queried
 Views: relations defined by a computation. They
are not stored, but are constructed when needed.
 Temporary tables: constructed by the SQL
language processor when it performs querie
executions or data modifications.
2.3.2 Data Types in SQL Server (1)

String, text data types:


Data type Explication

CHAR(n) Fixed-length string of up to n characters (n<=8000)


(‘foo’ string will be stored as ‘foo ’ for an attribute of CHAR(5))
VARCHAR(n) Variable length string of up to n characters (n<=8000)
Text Variable length string of up to 2GB text data
NCHAR(n) Fixed-length Unicode string of up to n characters (n<=4000)
NVARCHAR(n) Variable length Unicode string of up to n characters (n<=4000)
Ntext Variable length Unicode string of up to 2GB text data
2.3.2 Data Types in SQL Server (2)

Date, time data types:


Data type Explication

Datetime From January 1, 1753 to December 31, 9999


(Format: YYYY-MM-DD hh:mm:ss[.nnn])
Datetime2 From January 1, 0001 to December 31, 9999
(Format: YYYY-MM-DD hh:mm:ss[.nnnnnnn])
smalldatetime From January 1, 1900 to June 6, 2079
(Format: YYYY-MM-DD hh:mm:ss)
Date Store a date only. From January 1, 0001 to December 31, 9999
(Format: YYYY-MM-DD)
Time Store a time only
(Format: hh:mm:ss[.nnnnnnn])

E.g.: ‘2018-12-24’, ‘24 December 2018’, ‘12/24/2018’, ‘20181224’,


‘2018/12/24’ are different ways to represent the date 24 December 2018.
2.3.2 Data Types in SQL Server (3)

Number data types:


Data type Explication
Integer that can be 0, 1, or NULL, could be used as boolean data
bit
(0 (FALSE), 1 (TRUE))
tinyint Allows whole numbers from 0 to 255 (1 byte)
smallint Allows whole numbers between -32,768 and 32,767 (2 bytes)
Allows whole numbers between -2,147,483,648 and
int
2,147,483,647 (4 bytes)
Allows whole numbers between -9,223,372,036,854,775,808 and
bigint
9,223,372,036,854,775,807 (8 bytes)
float Floating precision number data from -1.79E + 308 to 1.79E +
308.
real Floating precision number data from -3.40E + 38 to 3.40E + 38
Decimal(p,s) or Fixed precision and scale number from -10^38 +1 to 10^38 –1 (p
Numeric(p,s) decimal digits and maximum s digits stored to the right of the
decimal point)
2.3.3a Create Database

Every SQL Server database has at least two operating system files: a data file
and a log file.
- Data files contain data and objects (tables, stored procedures, and views,…)
- Log files contain the information that is required to recover all transactions
in the database. 
2.3.3a Create Database

Example:
Create a database named QLSV with 10MB at the
beginning for the primary file and 5MB at the
beginning for the log file. Size of primary file and
log file can be up to 50 MB and 25 MB. The files
are stored in D:\DBI202.
2.3.3a Create Database

CREATE DATABASE QLSV


On Primary
(
Name=QLSV_Data,
Filename= 'D:\DBI202\QLSV_dat.mdf',
Size=10 MB,
MaxSize=50MB
)
Log On
(
Name = QLSVLog,
FileName='D:\DBI202\QLSV_log.ldf',
Size=5MB,
MaxSize=25MB
)
2.3.3b Delete Database

Syntax:
 DROP DATABASE DatabaseName
Example:
 DROP DATABASE QLSV
2.3.3c Modify Database Name

Syntax:
ALTER DATABASE database_name
Modify Name = new_database_name

Example:
ALTER DATABASE QLSV
Modify Name = QLSV_new
2.3.3d Modify Database File

ALTER DATABASE QLSV


MODIFY File(
Name=QLSV_Data,
Filename= 'D:\DBI202\QLSV_dat.mdf',
Size=15 MB,
MaxSize=50MB
)

Note: Give only information that you want to modify


2.3.4. Simple Table Declarations

Use ‘CREATE TABLE’ followed by the name of the


relation and a parenthesized, comma-seperated list
of the attribute names and their types:

[Use Database_Name]
CREATE TABLE TableName(
Attribute1 datatype [options],
Attribute2 datatype [options],
...
AttributeN datatype [options]
)
Example

CREATE TABLE Movies(


title char(100),
year int,
length int,
genre char(10),
studioName char(30),
producerC# int
)
2.3.5. Modifying Relation Schemas

 Delete table:
[USE Database_Name]
DROP TABLE TableName;
 Add attribute:
[USE Database_Name]
ALTER TABLE TableName
ADD
Attribute1 Datatype [Options],
Attribute2 Datatype [Options],
…;
2.3.5. Modifying Relation Schemas

Modify attribute:
[USE Database_Name]
ALTER TABLE TableName
ALTER COLUMN Attribute Datatype [Options];
Delete attribute:
[USE Database_Name]
ALTER TABLE TableName
DROP COLUMN Attribute1, Attribute2,…;
2.3.6. NULL Option

 NULL: attribute could have null value


 NOT NULL: attribute could not have null value
 Defined when creating or modifying table

CREATE TABLE Movies(


title char(100) not null,
year int not null,
length int null,
genre char(10) not null,
studioName char(30) not null,
producerC# int not null

)
2.3.7. Default values

 DEFAULT option: specify a default value for an attribute when no


value is provided
 DEFAULT could be defined when creating or modifying table

CREATE TABLE Movies(


title char(100) not null,
year int not null DEFAULT 1990,
length int null,
genre char(10) not null,
studioName char(30) not null,
producerC# int not null
)

ALTER TABLE Movies


ADD DEFAULT 1990 for year
2.3.8. Declaring Keys

 PRIMARY KEY:
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype PRIMARY KEY,
Attrbute2_Name DataType [OPTIONS],

);
 Or
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],

PRIMARY KEY(AttributeKey1, AttributeKey2,…)
);
 Or
[USE DatabaseName]
ALTER TABLE Table_Name ADD PRIMARY KEY(AttributeKey1, AttributeKey2,
…);
2.3.8. Declaring Keys

UNIQUE KEY:
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],
Attrbute2_Name DataType [OPTIONS] UNIQUE, …);
 Or
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],
Attrbute2_Name DataType [OPTIONS],

UNIQUE(UniqueAttribute1, UniqueAttributeKey2,…)
);
 Or
[USE DatabaseName]
ALTER TABLE Table_Name ADD UNIQUE(UniqueAttribute1,
UniqueAttributeKey2,…));
2.3.9. Identity option

IDENTITY:
IDENTITY (seed_value, increment_value);
 Seed_value: initial value
 Increment_value: increment step

CREATE TABLE Students(


ID int IDENTITY(1,1),
Name nvarchar(30)
)
2.3.10. INSERT data in table

INSERT:
INSERT into Table_Name(Attribute1, Attribute2, …)
VALUES (Attribute1_value1, Attribute2_value1, …),
(Attribute1_value2, Attribute2_value2, …),
…;

 Example
2.3.10. UPDATE data in table

UPDATE:
UPDATE Table_Name
SET AttributeName = value,
[,…, Attribute_k= value_k]
[FROM ...]
[WHERE Conditions]

 Example
2.3.10. DELETE data in table

DELETE:
DELETE From Table_Name
[FROM ...]
[WHERE Conditions]
 Example

You might also like