Create and delete database and table: Sql Server2008 Reviewing the Old Series 01

Create a database and table creation sql server commands roughly similar:

. 1  - creating a database: Create Database database_name 
2  Create  Database myDB
 . 3  
. 4  - Create new table in the database: Create Table table_name (field_name1 type constraint, field_name2 type constraint, ............) 
. 5  use mydb
 . 6  Go 
. 7  Create  Table TSTB
 . 8  (
 . 9      ID int  Identity  constraint Pk_myDB_tstbID Primary  Key ,
 10      name nvarchar ( 20 is ) Not  null ,    
 . 11      Agesmallint The  Not  null 
12 is  )
 13 is  - Condition build table (same table does not exist if the library is being used on the new table in) 
14  IF  Not  EXISTS ( SELECT name from the sysobjects WHERE xtype = ' the U- '  and name =  ' TSTB ' )
 15      Create  Table TSTB
 16      (
 . 17          ID int  Identity  constraint Pk_myDB_tstbID Primary  Key ,
 18 is          namenvarchar(20) not null,    
19         age smallint not null
20     )

One detail to note: construction of the table, if there is more than field-- field; you need to add an English comma at the end of each line, except for a final field.

In the design of each field as much as possible not to allow Null is added constraints: not null, because the null value is not very good statistics and processing;

Also try to use the appropriate type and length to save space, despite the current configuration is much larger than the client needs, but good design practice is always necessary;

Of course, in the design of the table and field names, try to use simple meaningful combination of characters to improve readability and ease of use, if it is necessary to design complex name, remember to add the appropriate comments.

This is a great benefit for future access.

 

Built the table, you can view the attribute information table by sp_help table_name (my sql command In contrast, mysql: show create table table_name)

 

 

 If you want to modify and design a table that already exists, can be used directly alter commands, such as to 'tstb' table to add a field - gender (popular terms is the new one):

alter table tstb add sex nchar(1) [constraint cons_name] default '女' 

 

Library and delete tables:

In sql server2008, the command is consistent;

1, delete a library: drop database database_name (admittedly, as a DBA, delete the library on foot is a shameful thing, so to use this command)

2, delete tables: drop table table_name, once using the drop table command structure of the entire table all been destroyed, so if and when you just want to delete

When we remove some of the conditions in line with the contents of the table, we can use the delete from table_name where ......  

Of course, you can use the delete from table_name to delete all the contents of the table, and truncate table table_name This command is similar, but the two

Order differences, the difference is most intuitive to delete only delete a record, the line number is not reset, re-insert (insert, write) the line number before the number of consecutive lines of identity data,

truncate identity reset to the initial setting value, of course, this difference is not purely, subsequent re-enumerate other differences.

 

Guess you like

Origin www.cnblogs.com/azrealer/p/11824475.html
Recommended