Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

LESSON 5

TABLE OF A
DATABASE

RAYMOND S. BERMUDEZ
Instructor, College of Computer Studies - MSEUF

Table of a database
A table is primarily a list of items or a

group of lists. To manage such a list, it


should be meticulously organized. To
organize this information, it is divided in
sections. Here is an example:

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Table Name
To complete the creation of a table, you

must save it. If you are freshly creating


a table and decide to save it, you would
be prompted to name it. The name of a
table:
Can be made of digits only. For

example you can have a table


called 148
Can start with a digit, a letter, or
an underscore
Can be made of letters, digits, and
spaces

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Table Name
Besides these rules, you can make up

yours. To avoid confusion, here are the


rules we will use to name our tables:
A name will start with a letter. Examples are

act or Second
After the first character as an underscore or
a letter, the name will have combinations of
underscores, letters, and digits. Examples
are _n24, act_52_t
Unless stated otherwise, a name will not
include special characters such as !, @, #, $,
%, ^, &, or *
If the name is a combination of words, each
word will start in uppercase. Examples are
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Data types are the most
fundamental constraint
element of a database in that
it restricts the range of
possible values that are
allowed to be stored within a
column.

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type (Numeric


Data)
One of the most common data types

that you will find within SQL Server


are numeric data types. There are
ten numeric data types that ship with
SQL Server 2008.
Four data types are designed to
store integer values of various sizes.
Two data types are designed to store
monetary data.
Three data types are designed to
store decimal-based numbers with
varying accuracy.
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type

Range of Value

Storage
Space

tinyint

0 to 255

1 byte

smallint

-32,768 to 32,767

2 bytes

int

-231 to 231 -1

4 bytes

bigint

-263 to 263 -1

8 bytes

decimal(p,s)
numeric(p,s)

-1038 to 1038 -1

5 to 17 bytes

smallmoney

-214,748.3648 to 214,748.3647

4 bytes

money

-922,337,203,685,477.5808 to
922,337,203,685,477.5807

8 bytes

real

-3.438 to -1.1838 , 0, and 1.1838


to 3.438

4 bytes

Float

-1.79308 to -2.23308 , 0, and


2.23308 to 1.79308

4 bytes or 8
bytes

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Character data type
char(n) - 1 byte per character
defined by n up to a maximum of
8000 bytes
varchar(n)

-1 byte per character


stored up to a maximum of 8000
bytes

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Character data type
text -1 byte per character stored up
to a maximum of 2 GB
nchar(n) -2 bytes per character

defined by n up to a maximum of
4000 bytes

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Character data type
nvarchar(n)- 2 bytes per characters
stored up to a maximum of 4000
bytes
ntext- 2 bytes per character stored

up to a maximum of 2 GB

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Character data type
smalldatetime
01/01/1900
to 06/06/2079
datetime

01/01/1753 to
12/31/9999

date

01/01/0001 to
12/31/9999

time

00:00:00.0000000
to 23:59:59.9999999

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

Data Type
Binary Data
bit
Null, 0 and 1 1 bit

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

The CREATE TABLE


Statement
The CREATE TABLE statement is

used to create a table in a database.


CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

The CREATE TABLE


Statement
Example
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQL NOT NULL Constraint


The NOT NULL constraint enforces a

column to NOT accept NULL values.


The NOT NULL constraint enforces a

field to always contain a value. This


means that you cannot insert a new
record, or update a record without
adding a value to this field.

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQL NOT NULL Constraint


CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQL PRIMARY KEY Constraint


The PRIMARY KEY constraint

uniquely identifies each record in a


database table.
Primary keys must contain unique

values.
A primary key column cannot

contain NULL values.


Each table should have a primary

key, and each table can have only


ONE primary key.

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQL PRIMARY KEY Constraint


CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQL PRIMARY KEY Constraint


on ALTER TABLE
To create a PRIMARY KEY constraint

on the "P_Id" column when the table


is already created, use the following
SQL:
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

DROP a PRIMARY KEY


Constraint
To drop a PRIMARY KEY constraint,

use the following SQL:


ALTER TABLE Persons
DROP PRIMARY KEY

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

The DROP TABLE Statement


The DROP TABLE statement is used

to delete a table.

DROP TABLE table_name

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

The ALTER TABLE


Statement
The ALTER TABLE statement is used

to add, delete, or modify columns in


an existing table.
SQL ALTER TABLE Syntax
To add a column in a table, use the
following syntax:
ALTER TABLE table_name
ADD column_name datatype
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

The ALTER TABLE


Statement
To delete a column in a table, use

the following syntax (notice that


some database systems don't allow
deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column

in a table, use the following syntax:


ALTER TABLE table_name
ALTER COLUMN column_name
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQLAUTO
INCREMENTField
Auto-increment allows a unique

number to be generated when a new


record is inserted into a table.
The MS SQL Server uses the

IDENTITY keyword to perform an


auto-increment feature.
By default, the starting value for

IDENTITY is 1, and it will increment


by 1 for each new record.
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

SQLAUTO
INCREMENTField
Example
CREATE TABLE Persons
(
P_Id int PRIMARY KEY IDENTITY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Note:To specify the column (for example PID)
should start at value 10 and increment by 5,
change the identity to IDENTITY(10,5).
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

INSERT INTO statement


The INSERT INTO statement is

used to insert new records in a table.


The INSERT INTO statement is used

to insert a new row in a table.

BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

INSERT INTO statement


SQL INSERT INTO Syntax
The first form doesn't specify the
column names where the data will be
inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2,
value3,...)
The second form specifies both the
column names and the values to be
inserted:
INSERT INTO table_name
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

INSERT INTO statement


EXAMPLE:
We have the following "Persons"
table:

Now we want to insert a new row in

the "Persons" table.


We use the following SQL statement:
INSERT INTO Persons
VALUES (4,'Nilsen', 'Johan', 'Bakken 2',
BY: Mr.'Stavanger')
RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

INSERT INTO statement


Insert Data Only in Specified
Columns
It is also possible to only add data in
specific columns.
The following SQL statement will add
a new row, but only add data in the
"P_Id", "LastName" and the
"FirstName" columns:
INSERT INTO Persons (P_Id,
LastName, FirstName)
VALUES (5, 'Tjessem', 'Jakob')
BY: Mr. RAYMOND S. BERMUDEZ, - Instructor, College of Computer Studies, MSEUF Lucena City

LESSON 5

TABLE OF A
DATABASE

RAYMOND S. BERMUDEZ
Instructor, College of Computer Studies - MSEUF

You might also like