Use SQL statements to create and delete constraints.

Source: Internet
Author: User

Use SQL statements to create and delete constraints.

Use an SQL statement to create or delete a Primary Key constraint of the constraint type (Primary Key constraint) --: The Primary Key column data must be unique and cannot be blank. Unique constraint (Unique constraint) --: This column must be Unique and can be null, but only one null value can appear. Check constraint: the restriction on the value range and format of a column, for example, the restriction on age and email address (@ is required. Default constraint: the Default value of a column. If there is a large number of duplicates in the database, it can be set as the Default value. Foreign Key constraint (Foreign Key constraint) --: used to establish a relationship between two tables. You must specify the column that references the primary table. **************************************** * ***************************** Add constraints: alter table tablename add constraint pk_colname primary key (colname) primary constraint alter table tablename add constraint uq_colname unique (colname) unique constraint alter table tablename add constraint df_colname default ('address unknown ') for colname default constraint alter table tablename add constraint ck_colname check (colname between 12 and 15) check constraint alter table tabl Ename add constraint fk_colname foreign key (colname) references tablename (colname) External creation constraint Delete constraint: alter table tablename drop constraint name create login account/database user create Login Account: exec sp_grantlogin 'windows domain name/domain account' to create a database user: exec sp_grantdbaccess' login account', 'database user' to authorize the database: grant permission [on table name] to database user the preceding statements can be used to operate Enterprise Manager/security/login/New Login directly in Enterprise Manager and enter the name and password to select database access, in the "database role allow" db_owner, the default constraint is also checked so that the user can define a value. Whenever the user does not enter a value in a column, the defined value is provided to this column. If you do not have specific requirements for this column, you can use the default constraint to enter the default value for this column.




Related Article

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.