Lab 01b
Data Types – DDL – DML
1
Outlines
Data types
DDL
Create table
Alter table
Drop table
DML
Insert
Update
Delete
Faculty of Science and Technology
Database Fundamentals
2
Data types (1)
Integer
Name
bigint
int
smallint
tinyint
Bytes
Range
-263 (-9,223,372,036,854,775,808) to
8
263 - 1 (9,223,372,036,854,775,807)
4
-231 (-2,147,483,648) to
231 - 1 (2,147,483,647)
2
-215 (-32,768) to 215 - 1 (32,767)
1
Faculty of Science and Technology
0 to 255
Database Fundamentals
3
Data types (2)
Exact numeric
Name
Bytes
Range
decimal[p[,s]] 5 – 17 - 1038 +1 to 1038 - 1.
numeric[p[,s]] 5 – 17 - 1038 +1 to 1038 - 1.
p (precision)
The maximum total number of decimal digits that can be stored, both to the left and
to the right of the decimal point. The precision must be a value from 1 through the
maximum precision of 38. The default precision is 18.
s (scale)
The maximum number of decimal digits that can be stored to the right of the
decimal point. Scale must be a value from 0 through p. Scale can be specified only
if precision is specified. The default scale is 0; therefore, 0 <= s <= p. Maximum
storage sizes vary, based on the precision.
Faculty of Science and Technology
Database Fundamentals
4
Data types (3)
Appropiate numeric
Name
Float[(n)]
real
Bytes
Range
n
- 1.79E+308 to -2.23E-308,
0 and 2.23E-308 to 1.79E+308
- 3.40E + 38 to -1.18E - 38,
4
0 and 1.18E - 38 to 3.40E + 38
Faculty of Science and Technology
Database Fundamentals
5
Data types (4)
Monetary
Name
Money
Bytes
Range
8
-922,337,203,685,477.5808 to
922,337,203,685,477.5807
smallmoney
4
- 214,748.3648 to
214,748.3647
Faculty of Science and Technology
Database Fundamentals
6
Data types (5)
Date and Time
Name
datetime
Bytes
Range
8
January 1, 1753, to
December 31, 9999
smalldatetime
4
January 1, 1900, to
June 6, 2079
Faculty of Science and Technology
Database Fundamentals
7
Data types (6)
Characters
Name
char[(n)]
Bytes
Comments
0-8000 non-Unicode
varchar[(n)]
0-8000 non-Unicode
varchar(max) 0-2 GB non-Unicode, 16 bytes pointer on row,
preferred over text data type
text
0-2 GB non-Unicode, 16 bytes pointer or in
row, obsolete, varchar(max) prefered
Faculty of Science and Technology
Database Fundamentals
8
Data types (7)
Characters (contd.)
Name
nchar[(n)]
Bytes
Comments
0-8000 max 4000 unicode characters
nvarchar[(n)]
0-8000 max 4000 unicode characters
nvarchar(max) 0-2 GB 16 bytes pointer or in row, preferred
over ntext data type
ntext
0-2 GB 16 bytes pointer, obsolete,
nvarchar(max) prefered
Faculty of Science and Technology
Database Fundamentals
9
Data types (8)
Binary
Name
binary[(n)]
Bytes
0-8000
varbinary[(n)]
0-8000
Comments
varbinary(max) 0-2 GB 16 bytes pointer or in row, preferred
over image data type
Faculty of Science and Technology
Database Fundamentals
10
Data types (9)
Image
Name
Image
Bytes
Comments
0-2GB 16 bytes pointer, obsolete,
varbinary(max) prefered
Global identifier
Name
uniqueidentifier
Bytes
16
Comments
XML
Name
xml
Bytes
Comments
0-2GB 16 bytes pointer
Faculty of Science and Technology
Database Fundamentals
11
Data types (10)
Special
Name
bit
cursor
timestamp
sysname
table
Bytes
Range
1
1 byte for every 8 bit columns
0-8
8
one column per table
256
-
sql_variant 0-8016
Faculty of Science and Technology
Database Fundamentals
12
Database
CREATE DATABASE Company ON PRIMARY
( NAME = 'Company',
FILENAME = 'C:\DATA\Company.mdf' ,
SIZE = 2048KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1024KB )
LOG ON
( NAME = 'Company_log',
FILENAME = 'C:\DATA\Company_log.ldf' ,
SIZE = 1024KB ,
MAXSIZE = 2048KB ,
FILEGROWTH = 10%)
GO
DROP DATABASE Company
GO
Faculty of Science and Technology
Database Fundamentals
13
DDL
Create table
Alter table
Drop table
Faculty of Science and Technology
Database Fundamentals
14
Create Table (1)
CREATE TABLE
table_name
( <column_definition> [ <table_constraint> ] [ ,...n ] )
[;]
<column_definition> ::=
column_name <data_type>
[ NULL | NOT NULL ]
[ [ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
| [ IDENTITY [ ( seed ,increment ) ] [ NOT FOR REPLICATION ] ]
Faculty of Science and Technology
Database Fundamentals
15
Create Table (1)
<column_constraint> ::=
[ CONSTRAINT constraint_name ]
{ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] |
[ FOREIGN KEY ] REFERENCES referenced_table_name [ ( ref_column ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
| CHECK ( logical_expression )
}
Faculty of Science and Technology
Database Fundamentals
16
Create table (3)
< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ]
(column [ ASC | DESC ] [ ,...n ] )
| FOREIGN KEY ( column [ ,...n ] )
REFERENCES referenced_table_name [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
| CHECK ( logical_expression )
}
Faculty of Science and Technology
Database Fundamentals
17
Example (1)
CREATE TABLE Department
(
DNumber numeric(4,0) Constraint pk_dep Primary Key,
DName varchar(15) not null,
MgrSSN char(9) Constraint fk_dep Foreign Key References
Employee(SSN),
MgrStartdate datetime,
);
Faculty of Science and Technology
Database Fundamentals
18
Example (2)
Or:
CREATE TABLE Department
(
DNumber numeric(4,0) not null,
DName varchar(15) not null,
MgrSSN char(9),
MgrStartdate datetime,
Constraint pk_dep Primary Key (DNumber),
Constraint fk_dep Foreign Key (MgrSSN) References Employee(SSN)
);
Faculty of Science and Technology
Database Fundamentals
19
Alter Table
ALTER TABLE table_name
{ ALTER COLUMN column_name
{ type_name [ ( { precision [ , scale ] } ) ]
[ NULL | NOT NULL ] | {ADD | DROP }
}
| [ WITH { CHECK | NOCHECK } ]
ADD
{ <column_definition> | <table_constraint> } [ ,...n ]
| DROP
{ [ CONSTRAINT ] constraint_name |
COLUMN column_name } [ ,...n ]
| [ WITH { CHECK | NOCHECK } ] { CHECK | NOCHECK } CONSTRAINT
{ ALL | constraint_name [ ,...n ] }
| { ENABLE | DISABLE } TRIGGER {ALL | trigger_name [ ,...n ] }
}
[;]
Faculty of Science and Technology
Database Fundamentals
20
Example (1)
Column
Adding a new column
ALTER TABLE Department ADD column_b
VARCHAR(20) NULL ;
Dropping a column
ALTER TABLE Department DROP column_b;
Changing a data type of a column
ALTER TABLE Department ALTER COLUMN
column_a NUMERIC(5,
2) ;
Database Fundamentals
Faculty of Science and Technology
21
Example (2)
Table
Primary key
ALTER TABLE Department
ADD Constraint pk_dept PRIMARY KEY (DNumber);
Foreign key
ALTER TABLE Department
ADD Constraint fk_dept Foreign Key (MgrSSN) References
Employee(SSN);
Drop Constraint
ALTER TABLE Department
DROP Constraint pk_dept;
ALTER TABLE Department
DROP Constraint fk_dept;
Faculty of Science and Technology
Database Fundamentals
22
Drop Table
DROP TABLE table_name [ ,...n ] [ ; ]
Example:
Drop table Department
Faculty of Science and Technology
Database Fundamentals
23
DML
Insert
Update
Delete
Faculty of Science and Technology
Database Fundamentals
24
Insert
Insert into table_name [(column_list)]
Values (value_list)
Example
INSERT INTO Department
VALUES (5, 'Research', '333445555', '22-MAY-78');
Or
INSERT INTO Department (DNumber, DName)
VALUES (5, 'Research');
Faculty of Science and Technology
Database Fundamentals
25