Sql Server CRUD field of grammar

Add SQL Statement field of wording:

General formula: alter table [table] the Add [field name] field attribute default The default value default is optional
Adding fields: alter table [table] the Add field name smallint The default 0 increasing number field, integer, default value 0
alter table [table name] field name the Add int  default 0 increasing number field, long integer, the default value is 0
alter table [table name] field name the Add SINGLE default 0 increasing number field, single-precision defaults 0
alter table [table] the Add field name Double  default 0 increasing number field, double defaults 0
alter table [table name] field name Tinyint the Add default 0 increasing number field, byte, the default value is 0
alter table [table] the Add field name text [ null ] Memo field increases, [ null ] Optional Parameters
alter table [table] the Add field name Memo [ null ] Memo field increases, [ null ] Optional Parameters
alter table [table] the Add field name VARCHAR (N) [ null ] increases variable length text field type of size N (~ 255. 1 )
alter table [table] the Add field name char [ null ] increase fixed-length text field size is fixed at 255 type
alter table [table name] field name the Add Datetime default function adds date fields, which may be a function now (), date (), etc., indicate the default value

(The above are the most common, there are other properties can be described with reference to the following types of data)
Delete field: alter table [table name] drop field name
Modified variable-length text field type size: alter table [table] ALTER Field Name varchar (N)
Delete tables: drop table [table name]
Create a table:
= SQL " the CREATE TABLE [table] ([field 1, and the primary key] int IDENTITY
(1, 1) NOT NULL CONSTRAINT PrimaryKey PRIMARY KEY,"&
"[Field 2] varchar (50)," &
"[字段3] single default 0,"&
"[Field 4] varchar (100) null," &
"[字段5] smallint default 0,"&
"[Field 6] int default 0," &
"[字段7] date default date(),"&
"[Field 8] int default 1)"
conn.execute sql
There are null indicates field allows zero-length

2 . Modify the table:
A. rename the table:
EXEC sp_rename 'oldname','newname'
B. Modify column properties:
ALTER TABLE student information
ALTER COLUMN name VARCHAR ( 20 ) the NOT NULL
C. Add a column:
ALTER TABLE student information
ADD home address nvarchar ( 20 ) NULL
D. Remove Columns:
ALTER TABLE student information
DROP COLUMN home address
D. Modify the column name:
the sp_rename Exec 'table. [Field formerly]', 'field of the new name,' 'column'

 

3 . Copy the table below:
A. Copy the entire table:
select * into new_table from old_table

B. Copy table structure:
select * into new_table from old_table where 1=2

B. Copy the table of contents:
insert into new_tab select * from old_table

 

4 . Modify identity column

Auto-increment can not be modified directly, you must delete the existing ID column, and then re-add an ID field has the identity attribute. For example, you want to modify field named ID:
alter table 表名 drop column ID
alter table 表名 add ID int identity(1,1)

Guess you like

Origin www.cnblogs.com/yu-shang/p/12185821.html