Creating and deleting constraints using SQL statements

Source: Internet
Author: User

Create and delete constraint constraint type PRIMARY KEY constraints using SQL statements (Primary key constraint)--: Requires primary key column data to be unique and is not allowed to be empty. Unique constraint (UNIQUE constraint)--: Requires the column to be unique, allowed to be empty, but only one null value can occur. CHECK constraints (CHECK constraint)---A column value range limit, formatting restrictions, and so on, such as The age, mailbox (must have @) constraints. Default constraint--: The default value of a column, such as a database where there is a lot of duplication of data, can be set to the default value. FOREIGN KEY constraint (Foreign key constraint)--: For establishing a relationship between two tables, you need to specify which column of the primary table is referenced. Add constraint: ALTER TABLE TABLENAMEADD constraint Pk_ ColName primary KEY (colname) main constraint ALTER TABLE TABLENAMEADD constraint uq_colname unique (colname) UNIQUE constraint ALTER TABLE Tablenameadd constraint Df_colname default (' Address Unknown ') for colname defaults constraint ALTER TABLE TABLENAMEADD constraint ck_colname Check ( ColName between and 15) CHECK constraint ALTER TABLE TABLENAMEADD constraint fk_colname foreign key (colname) references TableName (col Name) external constraint delete constraint: ALTER TABLE TABLENAMEDROP CONSTRAINT constraint name Create login account/database user create logon account: Exec sp_grantlogin ' Windows domain name/ Domain account ' CREATE database User: Exec sp_grantdbaccess ' login account ', ' database user ' authorization to database: Grant permission [on table name]to database user above statement can be operated directly in Enterprise Manager/Security/Login/ New login fill in name and password Select Database access, and then under "Database roleAllow "db_owner also tick the default constraint enables the user to define a value that is provided to this column whenever the user does not enter a value in a column. If the user does not have a specific requirement for this column, you can use the default constraint to enter a default value for this column.

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.