Anda di halaman 1dari 17

Module 4:

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 Property is used for those columns that need automatically


generated unique system values.
This property can be used to generate Sequential Numbers.

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

 Requirements for Using the Identity Property


 Only one identity column is allowed per table
 Use with integer, numeric, and decimal data types
 Retrieving Information About the Identity Property
 Use IDENT_SEED and IDENT_INCR for definition
information
 Use @@identity to determine most recent value
Identity Property

CREATE TABLE Employee


(
EmpCode int IDENTITY (100,1),
EmpName Char(25), NOT NULL,
DeptName Char(25) NOT NULL
)

The Indentity Column with Seed Value 100 and Increment 1.


When Inserting Values no need to pass the value of Identity Column.
Inserting Rows

INSERT Statement is used to add Records to the table.


INSERT INTO {Table_Name} [(column_list)]
VALUES{Default Values | values_list
| (Value1,Value2,……,Valuen)}
Insert into Employee
Values(‘Vidya Vrat Agarwal’,’IT’)
Inserting Partial data

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

UPDATE Statement is used to Modify the Values in a Row


within a table.
The Update Statement required three main parameters :
 The TABLE to be Updated.
 The COLUMNS to be Updated.
 The ROWS to be Updated.
UPDATE Table_Name UPDATE Item
SET Column_Name=Value SET Price = 12000
WHERE Condition Where ItemCode = ‘IOO2’
Deleting Rows

The DELETE Statement is used to Delete/Remove rows


from the table.
DELETE [From] Table_Name

Where Condition
Delete from Item
WhereItemCode = ‘IOO1’
If Where Clause is not Specified all the rows will be
Deleted.
Truncating a Table

The TRUNCATE Table Statement is used to Remove all


the Rows from a Table.

Truncate Table TableName


e.g.- Truncate Table Employees
Removes all the rows from the Employees Table.

TRUNCATE Table Statement is faster.


Deleting a Table

The DROP Table Statement is used to Delete a Table.


All the Data in the Table is Permanently Deleted.
Can Not use the DROP Table Statement to Drop System
Tables.
Drop Table TableName
Adding and
Dropping a Column

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

User-defined data types are based on system-supplied


data types.

They ensure consistency when working with common


data elements in different tables or databases.
A user-defined data type is defined for a specific
database.

Note User-defined data types that you create in the


model database are automatically included in all
databases that are subsequently created. Each user-
defined data type is added as a row in the systypes
table.
Creating and
Dropping 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

Anda mungkin juga menyukai