SQL database constraint behavior---Prevent data scrambling (that is, data normalization)

Source: Internet
Author: User

To prevent random filling:
One, check constraints.
Check the data according to a certain rule.
Action: In the table design interface, right-click the appropriate column, select Check Constraint in the popup dialog box, set the name and expression of the constraint.

Code implementation:

CREATE TABLE Car

(

code int Check (CODE>0)

)

Second, foreign KEY constraints. The main table, from the table.
The primary table is used to constrain the from table. The foreign key should be built from the table.
Use the primary key of the main table to constrain the foreign key from the table.
The foreign key column from the table cannot be arbitrarily filled, it can only fill in the data that exists in the primary key of the main table.

once the foreign key relationship is established, the foreign key from the table cannot be filled out, and the primary key column in the main table can not be deleted randomly.

Settings for cascading deletions--sorting

Action: First build the main table, in the table from the design interface, right-click-"Relationship", in the Popup dialog box select "Add", in the right attribute list "table and column Specification"

The second method is to create a new database diagram on the corresponding database diagram.

Code implementation

CREATE TABLE Car

(

Code Varvhar (primary key),

Brand varchar (reference brand) (Brand_Code)

)

The main table is the brand table


Three, type.

Create a data type defined by all columns of a table

Character data: Char;varchar

Date Time Data: Datetime;smalldatetime

Numeric data: Bigint;int;smallint;tinyint

A decimal (A, a, a, A, b) represents the number of digits that contain decimals and decimal points, and a number of decimal places. That is, the number of integer digits is a-b-1 (one decimal point)

SQL database constraint behavior---Prevent data scrambling (that is, data normalization)

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.