Creating a data table with a T-SQL

Source: Internet
Author: User

Let's review the data table CreationProcedure

(1) determine the columns in the table

(2) determine the Data Type of each column

(3) add various constraints to the table

(4) create relationships between tables

Syntax for creating a table:

Create Table data table name

(

Feature of Field 1 data type column,

Feature of Field 2 data type column

)

The features of a column include whether the column is null, whether it is an identifier column, whether it has a default value, and whether it has a primary key.

Example:

Code:
  1. Use studb -- set the current database to studb for table creation in this database
  2. Go
  3. Create Table stuinfo
  4. (
  5. Stuname varchar (20) not null
  6. , Stuno char (6) Not null
  7. , Stuage int not null
  8. , Stuid int not null
  9. , Stuseat smallint identity (1, 1)
  10. , Stuaddress text
  11. )
  12. Go

The stuseat column of the Identity (start value, increment) is automatically numbered, also known as the ID column.

Note that fields must be separated by commas.

When a column is set as an ID column, the column is automatically set as non-empty.

Similar to creating a database, if the stuinfo table exists in the current database, an error is returned when you create a database again.

The solution also flies over. We need to check whether the table exists in the Database in advance. If yes,

Delete. Otherwise, create.

The syntax for deleting a data table is as follows:

Drop Table Name

Where is the list of tables in the database stored?

The answer is the system table sysobjects of the database.

Example:

Code:
  1. Use studb
  2. Go
  3. If exists (select * From sysobjects where name = 'stuinfo ')
  4. Drop table stuinfo
  5. Create Table stuinfo
  6. (
  7. ---............
  8. )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.