Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The design of SQLServer table (building table)

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Knowledge part

1. SQLServer data type

A data type is an attribute of data that defines whether the data is time, numbers, strings (text, letters), etc.

SQLServer provides a set of system data types that defines all the data types that can be used with SQLServer

Common data types:

Int: an integer from-2147483648 to-2147483647 (available for identifier columns)

Money: currency type, which can contain decimals.

Decimal: decimal, larger number of digits

Float: decimal

Datetime: date typ

Char: can contain 8000 characters

Varchar: can store more characters than char

Binary: used to store fixed-length binary data up to 8000 bytes long

Image: can store about 2 billion binary data

2. Default value

When inserting data, if no data is typed (left blank) for a column of a row and the default value is set, then the default value is used for this column.

3. Identifier column

The serial number of the table, which is automatically incremented, has three characteristics:

The data type of a column cannot be decimal.

Control of null is not allowed

Each table can have only one identifier column

4. Check constraint

Domain integrity can be limited by check constraints. Create an check constraint from any logical expression based on the TRUE or FALSE returned by the logical operator. For example, you can set check constraints to restrict data such as age, date of birth, and so on.

Operation part

Graphical table building

1. First expand the following nodes-Click New Table

2. SSMS will pop up the design box of a table.

3. Set up several columns and prepare for operation

4. Everyone knows that each student's student number must be unique, so we can set the column of student number as the primary key, and no duplicates are allowed.

5. When the setting is complete, it will look like this. A key icon will appear, and the ticks that allow null values will be removed automatically.

6. Then we can also make an identifier column and find the "identity specification" in the table designer-just change the option to "Yes"

7. For students in a class, we can set a default value for the column of the class. If you don't write the data in this way, the class system will automatically fill in the default value we specify.

8. Right-click in the space-you can create a new check constraint so that you can restrict the writing of data. For example, the gender limit can only be "male" or "female", and the score is limited to 0 to 100.

9. What I limit here is the grade. Fill in the expression and close it. Other constraints are similar.

Building a table with T-SQL statement

For example:

Create table name (

StudentID varchar (10) NOT NULL

Sname varchar (10) DEFAULT NULL

Sex char (2) DEFAULT NULL

CardID varchar (20) DEFAULT NULL

Birthday datetime DEFAULT NULL

Email varchar (40) DEFAULT NULL

Class varchar (20) DEFAULT NULL

EnterTime datetime DEFAULT NULL)

Go

!!! First of all, create means to create, table is the table, and name is the name given to the table. Followed by (), () is each column of the table; the first field is the name of the column, followed by the data type of the column, followed by whether a null value null is allowed. The GO at the end of the statement is execution. During the operation, we click "New query" in the upper left corner of SSMS-then enter the statement-click "execute"

After execution, you can see

Refresh the "table"

The newly created table named "name" can be seen.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report