SQL server--using code to create databases and constraints

Source: Internet
Author: User
Tags one table

  Before we could learn how to use code to create databases, tables, and constraints, we could only use the mouse to click on them, so that it looked less intuitive (tall).

Know where to write and how to run before writing your code:

  

  Click New Query, and then the white space in the middle is where the code is written.

In addition, here we'd better write a sentence to execute a sentence, and the implementation method and C language those different, here we need to select the code snippet to be executed click on the upper left corner of the execution.

  

 is a sentence to create a database code, first select it, and then click Execute.

  Code Creation Database:

There must be a database before creating tables and constraints, so let's start by saying how to create a database using code.

Created: Create DATABASE name

After creating the database, right-click on the left-hand side of the database to refresh and see the database you just created.

Delete the database if it has been created: drop databases Name

  Code Creation Table:

        Once the database is created, you can create the table, and we'll use a code to refer to the database you just created before creating the table: the Use database name (just created), and the formal creation of the table after the application.

Syntax: CREATE TABLE table name

(

Content.......... (Content is column name, data type, constraint)

)

  

That is, the content in parentheses is divided into three columns, the first column represents the column name, the second column represents the data type, and the third column represents the constraint. You can do it after you've written it.

  Code creation constraints:

Here you need to know the words that correspond to the various constraints:

PRIMARY KEY constraint: Primary key prefix: PK
Unique constraint: Unique prefix: UQ
Default constraint: Defaults prefix: DF
Check constraint: Check prefix: CK
FOREIGN KEY constraint: foeign key prefix: FK

The prefixes here can be ignored because there are two ways to create a code (I'll just say the simple one, and the prefix will be used in the other).

Here is how the various constraints are written:

    

1 Create Databasestudendb2 3  Usestudendb4 5 Create TableStuno6 (7Stuint                    Identity(1,1)/*Self-increment*/ Primary Key/*PRIMARY Key*/,8NameChar(Ten),9Telvarchar( One)Unique/*UNIQUE Constraint*/ Check(Len(tel)= One)/*CHECK Constraints*/,TenSexChar(2)default 'male'/*By default, this is the default for male*/  Check(Sex='male' orSex='female')/*CHECK Constraints*/ One ) A  - Select *  fromStuno--View Table -  the Create Tablescore - ( -Noint                    Identity(1,1)/*Self-increment*/ Primary Key/*PRIMARY Key*/, -Stuint                    ReferencesStuno (Stu),--The foreign key, which references the value in the Stu column in the Stuno table, is: references the primary key table name to reference (primary key column) +Scoreint                    Check(Score>=0  andScore<= -)/*CHECK Constraints*/ - ) + Select *  fromStuno--View Table

Build good one table to view, syntax:select * FROM table name

More detailed questions add my QQ 1289747698, anytime answer Oh!

SQL server--using code to create databases and constraints

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.