Creating
Tables and Data Types
Vidya Vrat Agarwal. | MCT, MCSD
Objectives
Creating Tables
Data Types
IDENTITY Property
Inserting Rows
Updating Rows
Deleting Rows
Truncating Table
Deleting a Table
Creating and Dropping User-defined Data Types
Creating a Table
Syntax
CREATE TABLE table_name
(column_name datatype [NULL | NOT NULL]
[IDENTITY (SEED, INCREMENT)]
[DEFAULT constant_expression])
Creating a Table
NULL
NULLoror
Column
Columnname
name Data
Datatype
type NOT
NOTNULL
NULL
CREATE
CREATE TABLE
TABLE Categories
Categories
(( int
int NOT
NOT NULL,
NULL,
CategoryID
CategoryID varchar(15)
varchar(15) NOT
NOT NULL,
NULL,
CategoryName
CategoryName text
text NULL
NULL
Description
Description ))
Datatypes
Character Data
Char, Varchar, Text
Numeric Data
1. Integer data: Bigint, int, smallint, tinyint
2. Exact numeric data: decimal, numeric
3. Approximate numeric data: Float dan real
Monetary Data
Money, Smallmoney
Date and Time Data
Datetime, Smalldatetime
Identity Property
Identity (Seed,Incremant)
Seed is the Starting or Initial Value for the Identity Column.
Increment is the Step value used to generate the next value for column.
Using the Identity Property
SQL Server allows the Insertion of Partial data for a Column that
allows NULL or has a DEFAULT assigned to it.
e.g.- An Item Table has ItemCode,ItemName,Price,Quantity
Columns.
To insert Partial Data in the Item Table use :
Insert [Into] Item (ItemCode,Price,Quantity)
Values (‘I002’,8000,10)
Updating Rows
Where Condition
Delete from Item
WhereItemCode = ‘IOO1’
If Where Clause is not Specified all the rows will be
Deleted.
Truncating a Table
ADD ALTER
ALTER TABLE
TABLE CategoriesNew
CategoriesNew
ADD
ADD Commission
Commission money
money null
null
Customer_name
Customer_name Sales_amount
Sales_amount Sales_date
Sales_date Customer
CustomerIDID Commission
Commission
DROP
ALTER
ALTER TABLE
TABLE CategoriesNew
CategoriesNew
DROP
DROP COLUMN
COLUMN Sales_date
Sales_date
User-defined Data Types
Creating
Creating
EXEC
EXEC sp_addtype
sp_addtype city,
city, 'nvarchar(15)',
'nvarchar(15)', NULL
NULL
EXEC
EXEC sp_addtype
sp_addtype region,
region, 'nvarchar(15)',
'nvarchar(15)', NULL
NULL
EXEC
EXEC sp_addtype
sp_addtype country,
country, 'nvarchar(15)',
'nvarchar(15)', NULL
NULL
Dropping
Dropping
EXEC
EXEC sp_droptype
sp_droptype city
city
Review
Creating Tables
IDENTITY Property
Inserting Rows
Updating Rows
Deleting Rows
Truncating Table
Deleting a Table
Creating and Dropping User-defined Data Types