Alter table alter constraints and Foreign keys, case when then, SQL statement for joint Transformation

Source: Internet
Author: User

 

Common SQL change and query statements
Select *, (case when type = 1 then
(Select typename from tableatype where type = 1)
When type = 2 then
(Select typename from tableatype where type = 2)
When type = 3 then
(Select typename from tableatype where type = 3)
Else
(Select typename from tableatype where type = 4)
End
) As typetmp from tablea

======================================

Select *, (select typename from tabletype where type = T. Type) as type1
From tablea t

========================================
-- Modify the table to add a primary key
Alter table [tablea] With nocheck add
Primary Key clustered
(
[ID]
) On [primary]
Go
-- Modify the table to add default values and unique constraints
Alter table [tablea] With nocheck add
Constraint [DF _ tableb _ typen _ 6379339d] default ('') for [typename],
Constraint [DF _ tableb _ typer _ 643a57d6] default ('') for [typeremark],
Constraint [uq _ tablea _ type] unique nonclustered
(
[Type]
) On [primary]
Go

-- Modify the type field of the table to add a foreign key constraint to another table.
Alter table [tablec] add
Constraint [fk_tablec_tablea] foreign key
(
[Type]
) References [tablea] (
[Type]
)
Go

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.