SQL Table Structure Operations

Source: Internet
Author: User

Database Knowledge Summary (table structure operation)

1. Create a table scores

1 CREATE TABLE Scores               --table Name 2 (Id int identity (primary) key,--set the primary key, and the line number is self-increasing,
The identity (first) indicates that the ID column is incremented from 1, and each time the Date datetime is NOT NULL, --The setting type is datetime and cannot be empty 4 Name nvarchar (not null,5 score) nvarchar (2) --default state, type is empty 6)

2. Modify the table name scores to Newscores

1 exec sp_rename ' Scores ', ' newscores '

3. Delete Table Scores

1 drop table Scores-delete tables (the structure, properties, and indexes of the table will also be deleted)

4. Clear the table data

1 TRUNCATE TABLE Scores--just delete the data in the table

5. Modify column score to NOT NULL

1 ALTER TABLE Scores2 ALTER COLUMN score nvarchar (2) NOT NULL

6. Adding columns

1 ALTER TABLE Scores 2 add new nvarchar (a) NOT NULL

7. Modify column names

1 exec sp_rename ' scores.name ', ' NewName ', ' column '

8. Delete Columns

1  ALTER TABLE Scores 2  

9. Modify the Identity column

If you create a table, the self-increment column is not set. Because the self-increment column cannot be modified directly, you must drop the original ID column and then add a column with the ID field that has the identity attribute.

1  exec Sp_pkeys @table_name = ' Scores '                               --Query primary KEY Name 2  ALTER TABLE Scores drop constraint pk__scores__ 3214EC074E3D66D2  --delete the PRIMARY KEY constraint first 3  ALTER TABLE Scores drop column ID                                --delete the ID column 4  ALTER TABLE Scores add ID int ID Entity (+)                      -add a self-increment column (the Id column is not the primary key) 5  ALTER TABLE Scores add ID int identity (constraint) PK primary key  -- Add self-increment column, set ID as primary key name PK

SQL Table Structure Operations

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.