Create a table on SQL Server

Source: Internet
Author: User

On the basis of the database created in the previous article, we will continue to create data tables.

 

The syntax for the T-SQL to create a database is as follows:

Create TableTable Name

(

Field name 1 data type constraints,

Field name 1 data type constraints,

...

)

 

The first step is to create stuinfo. Here are some small details: the ID column of identity (starting value, increasing quantity). Numeric (18, 0) represents 18 digits and the decimal place is 0.

 

Use studb -- set the current database to studb

Go

If exists (select * From sysobjects where name = 'stuinfo ')

Drop table stuinfo

Create Table stuinfo/*-create student info table -*/

(

Stuno char (6) Not null, -- Student ID, not empty (required)

Stuname varchar (20) not null, -- name, not empty (required)

Stuage int not null, -- age. The default value of the int type is 4 bytes.

Stuid numeric (), -- ID card number

Stuseat smallint identity (1, 1), -- seat number, automatic number

Stuaddress text -- address, which can be null. Optional.

)

Go

 

Then, create the student token table stumarks.

 

Create Table stumarks

(

Examno char (7) Not null, -- Exam No.

Stuno char (6) Not null, -- Student ID

Writtenexam int not null, -- test score

Labexam int not null -- Test Result

)

Go

 

If the stuinfo table exists in the table, how can we solve the problem of re-creation errors? We will surely think of the same solution as database processing. Where can I detect it? Oh, check the name column in the system table sysobjects (Object table) of the studb database. Now we have created both databases and tables. When creating a table, we specify the data type for each field and impose basic restrictions and differences on the types of values that can be inserted. What data types does SQL server have? Let's take a look at the next article.

 

 

Related Article

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.