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:
- Use studb -- set the current database to studb for table creation in this database
- Go
- Create Table stuinfo
- (
- Stuname varchar (20) not null
- , Stuno char (6) Not null
- , Stuage int not null
- , Stuid int not null
- , Stuseat smallint identity (1, 1)
- , Stuaddress text
- )
- 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:
- Use studb
- Go
- If exists (select * From sysobjects where name = 'stuinfo ')
- Drop table stuinfo
- Create Table stuinfo
- (
- ---............
- )