Database Tables 
Tables are database objects that contain all the data in a database. A table definition is a collection of columns. In tables, data is organized in a row-and-column format similar to a spreadsheet. Each row represents a unique record, and each column represents a field within the record.
After you have designed the database, the tables that will store the data in the database can be created. The data is usually stored in permanent tables. Tables are stored in the database files until they are deleted and are available to any user who has the appropriate permissions.
Temporary Tables
You can also create temporary tables. Temporary tables are similar to permanent tables, except temporary tables are stored in tempdb and are deleted automatically when no longer in use.
The two types of temporary tables, local and global, differ from each other in their names, their visibility, and their availability. Local temporary tables have a single number sign (#) as the first character of their names; they are visible only to the current connection for the user; and they are deleted when the user disconnects from instances of Microsoft® SQL Server™ 2000. Global temporary tables have two number signs (##) as the first characters of their names; they are visible to any user after they are created; and they are deleted when all users referencing the table disconnect from SQL Server.
For example, if you create a table named employees, the table can be used by any person who has the security permissions in the database to use it, until the table is deleted. If you create a local temporary table named #employees, you are the only person who can work with the table, and it is deleted when you disconnect. If you create a global temporary table named ##employees, any user in the database can work with this table. If no other user works with this table after you create it, the table is deleted when you disconnect. If another user works with the table after you create it, SQL Server deletes it when both of you disconnect.
 Table Properties
You can define up to 1,024 columns per table. Table and column names must follow the rules for identifiers; they must be unique within a given table, but you can use the same column name in different tables in the same database. You must also define a data type for each column.
Although table names must be unique for each owner within a database, you can create multiple tables with the same name if you specify different owners for each.
Creating a Table using Enterprise Manager:

  1. Open the Enterprise Manger Tool from the Start menu.
  2. Expand the server Group and the corresponding server.
  3. Expand the corresponding database
  4. Right click on Tables and select New Table… item
5. In the window displayed enter a name to the column, data type and lengh

  1. Click on the save icon of the toolbar to save the table.
 Creating a Table using Query Analyzer:
 We can make use of the following syntax to create a table:

CREATE TABLE tablename
(colname datatype (size) [NOT NULL | DEFAULT <constant value> | IDENTITY(seed,increment)], colname2 ……..)

Are keywords that determine if null values are allowed in the column. NULL is not strictly a constraint but can be specified in the same manner as NOT NULL.
Specifies the value provided for the column when a value is not explicitly supplied during an insert.
Is a constant, NULL, or a system function used as the default value for the column.
Indicates that the new column is an identity column. When a new row is added to the table, Microsoft® SQL Server™ provides a unique, incremental value for the column. The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns. Only one identity column can be created per table. Bound defaults and DEFAULT constraints cannot be used with an identity column. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1).
Is the value used for the very first row loaded into the table.
Is the incremental value added to the identity value of the previous row loaded.
Example: Crating a table with identity column

( dept_no int identity (10,10),
  dept_name varchar(20),
  location varchar(20)

When an IDENTITY property to define an identifier column, consider that:

  • A table can have only one column defined with the IDENTITY property
  • The seed and increment can be specified. The default for both is 1
  • The identifier column must not allow NULL values and must not contain DEFAULT definition
  • In order to insert the values explicitly into the identity column , SET IDENTITY_INSERT option is used.


Renaming a  Table

SP_RENAME <old_tableName>,<new_tableName>

SP_RENAME employee.emp

Renaming a Column:

SP_RENAME <’ tableName.columnname’>,<’newcolName’>

SP_RENAME ‘emp.basic_salary’,’bsal’


Syntax for ALTER TABLE command:

ALTER TABLE tablename
ADD columnName datatype [not null |identity|default…]
|ALTER COLUMN <column specifications>
|DROP COLUMN columnName
|DROP CONSTRAINT constraintName


Post a Comment