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.