Tables are used to store data in the database. Tables are uniquely named within a database and schema. Each table contains one or more columns. And each column has an associated data type that defines the kind of data it can store e.g., numbers, strings, or temporal data.
To create a new table, you use the CREATE TABLE statement as follows:
SYNTAX
|
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
column4 datatype,
column5 datatype
);
|
EXAMPLE
|
CREATE TABLE CSharpHub (
Id INT PRIMARY KEY IDENTITY (1, 1),
FirstName VARCHAR(50),
LastName VARCHAR(50),
CreatedDate DATETIME,
Contact VARCHAR(20)
);
|
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
SYNTAX
|
ALTER TABLE table_name
ADD column_name datatype;
|
EXAMPLE
|
ALTER TABLE CSharpHub
ADD Address varchar(255);
|
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):
SYNTAX
|
ALTER TABLE table_name
DROP COLUMN column_name;
|
EXAMPLE
|
ALTER TABLE CSharpHub
DROP COLUMN Address;
|