S07 Create Tables
S07 Create Tables
Server
2012
Session: 7
User-defined types
• are created using programming languages supported by the .NET
Framework, which is a software framework developed by Microsoft.
Alias data types can be created using the CREATE TYPE statement.
Syntax
:
CREATE TYPE[ schema_name.] type_name{FROM base_type[(
precision[,scale])][NULL|NOT NULL]}[;]
For example
Syntax
:
where,
table_name: is the name of the new table, maximum of 128 characters.
column_name: is the name of a column in the table. up to 128 characters.
column_name are not specified for columns that are created with a timestamp data
type. The default column name of a timestamp column is timestamp.
For example :
CREATE TABLE [dbo].[Customer_1](
[Customer_id number] [numeric](10, 0) NOT NULL,
[Customer_name] [varchar](50) NOT NULL)
ON [PRIMARY]
GO
Syntax
:
where,
ALTER COLUMN: specifies that the particular column is to be changed or modified.
DROP COLUMN ([<column_name>]: specifies that column_name is to be
removed from the table.
ADD: specifies that one or more column definitions are to be added.
Under certain conditions, columns cannot be dropped, such as, if they are used
in a CHECK, FOREIGN KEY, UNIQUE, or PRIMARY KEY constraint,
associated with a DEFAULT definition, and so forth.
Syntax
:
For example:
USE [CUST_DB]
DROP TABLE [dbo].[Table_1]
The nullability feature of a column determines whether rows in the table can contain
a null value for that column.
Null value is not same as zero, blank, or a zero length character string (such as ' ').
When inserting a row, if no value is given for a nullable column, then, SQL Server
automatically gives it a null value unless the column has been given a default value
For example
USE [CUST_DB]
CREATE TABLE StoreDetails ( StoreID int NOT NULL, Name varchar(40) NULL)
GO
assign a default value to the column if no value is given at the time of creation.
When a DEFAULT definition is added to an existing column, SQL Server applies the
default values only to newly added rows of data.
For example
USE [CUST_DB]
CREATE TABLE StoreProduct( ProductID int NOT NULL, Name varchar(40) NOT NULL,
rice money NOT NULL DEFAULT (100))
GO
INSERT INTO dbo.StoreProduct (ProductID, Name) VALUES (111, 'Rivets')
GO
need not have a seed and increment value specified. If they are not specified, a default value of 1 will be
used for both.
A table cannot have more than one column with IDENTITY property.
must not allow null values and must not contain a DEFAULT definition or object.
The values can be explicitly inserted into the identity column only if the IDENTITY_INSERT
option is set ON.
Syntax
:
where,
seed_value: is the seed value from which to start generating identity values.
increment_value: is the increment value by which to increase each time.
For example:
USE [CUST_DB]
GO
CREATE TABLE HRContactPhone ( Person_ID int IDENTITY(500,1) NOT
NULL, MobileNumber bigint NOT NULL )
GO
Only one identifier column and one globally unique identifier column can be
created for each table.
The NEWID() function creates a unique identifier number which is a 16-byte binary
string.
The column can be referenced in a SELECT list by using the ROWGUIDCOL keyword.
The COLUMNPROPERTY function is used to retrieve the name of the ROWGUIDCOL column.
Following figure shows the output where a unique identifier is displayed against a
specific PersonName:
A column constraint is specified as part of a column definition and applies only to that
column.
A table constraint can apply to more than one column in a table and is declared
independently from a column definition. .
Table constraints must be used when more than one column is included in a constraint.
The PRIMARY KEY constraint is used to create a primary key and enforce integrity
of the entity of the table.
Syntax
:
CREATE TABLE <table_name> ( Column_name datatype PRIMARY KEY [
column_list] )
Syntax
:
CREATE TABLE <table_name> ([column_list ] <column_name> <data_type>
UNIQUE [ column_list])
For example:
USE [CUST_DB]
GO
Syntax
:
For example
USE [CUST_DB]
GO
A NOT NULL constraint enforces that the column will not accept null values.
The NOT NULL constraints are used to enforce domain integrity, similar to
CHECK constraints.
INSERT Statement
For example
USE [CUST_DB]
INSERT INTO [dbo].[Table_2] VALUES (101, 'Richard Parker', 'Richy')
GO
UPDATE Statement
where,
<Column_Name>: name of the column in which record is to be updated.
<Value>: specifies the new value for the modified column.
<Search condition>: the condition to be met for the rows to be updated.
For examle:
USE [CUST_DB]
UPDATE [dbo].[Table_2] SET Contact_number = 5432679
WHERE Contact_name LIKE 'Richy'
GO
DELETE Statement
Where,
The WHERE clause is used to specify the condition. If WHERE clause is not included
in the DELETE statement, all the records in the table will be deleted.
For example:
USE [CUST_DB]
DELETE FROM [dbo].[Customer_2] WHERE Contact_number = 5432679
GO
● A data type is an attribute that specifies the storage capacity of an object and the
type of data it can hold, such as numeric data, character data, monetary data, and so
on.
● Most tables have a primary key, made up of one or more columns of the table that
identifies records uniquely.
● The nullability feature of a column determines whether rows in the table can contain
a null value for that column.
● A DEFAULT definition for a column can be created at the time of table creation or
added at a later stage to an existing table.
● The IDENTITY property of SQL Server is used to create identifier columns that can
contain auto-generated sequential values to uniquely identify each row within a
table.
● Constraints are used to apply business logic rules and enforce data integrity.
● A UNIQUE constraint is used to ensure that only unique values are entered in a
column or set of columns.
● A foreign key in a table is a column that points to a primary key column in another
table.