1: In SQL statements, there are two classes of temporary tables, local (local) and global temporary tables, local temporary tables are only visible in their sessions (transactions), and global temporary tables can be used by any program in the session (transaction) or
Module access
2: Create a local temporary table
[SQL]View Plaincopyprint?
- Use Db_sqlserver
- Go
- Create table #db_local_table
- (
- ID int,
- name varchar,
- Age int,
- Area int
- )
Use Db_sqlservergocreate table #db_local_table ( ID int, name varchar (+), age Int., area int)
Temporary tables created cannot be shared with other sessions, and the definition of rows and tables will be deleted when the session ends
3: Create a global temporary table
[SQL]View Plaincopyprint?
- Use Db_sqlserver
- Go
- Create table # #db_local_table
- (
- ID int,
- name varchar,
- Age int,
- Area int
- )
Use Db_sqlservergocreate table # #db_local_table ( ID int, name varchar (+), age Int., area INT )
Global temporary tables are visible to all users, and global temporary tables are removed when each user accessing the table disconnects from the server
4: Create a primary key, foreign key associated database table
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Create table Db_table5
- (
- Employee number int primary key,
- The employee number varchar ( a) is unique,
- Warehouse number varchar (a),
- Payroll int
- )
- Go
- Create table Db_table6
- (
- Order number int primary key,
- Order number varchar unique,
- Staff number varchar (references) Db_table5 (employee number),
- Order Date datetime,
- Sales Amount int
- )
Use Db_sqlserver;gocreate table Db_table5 ( employee number int primary key, employee number varchar () unique, warehouse number varchar ($), payroll int) gocreate table Db_table6 ( order number int primary key, order number varchar () unique, Staff number varchar (references) DB_TABLE5 (employee number), Order date datetime, Sales Amount int)
5: Create a database table with a CHECK constraint field
[SQL]View Plaincopyprint?
- USE DB_SQLSERVER;  
- go
- create table db_table7
- (
- warehouse number int primary key,
- Employee number varchar ( unique,
- warehouse number varchar (a),
- salary int,
- area int check (area >=600 and area <=1800)
- )
Use Db_sqlserver;gocreate table Db_table7 ( warehouse number int primary key, employee number varchar () unique, warehouse number varchar ($), payroll int, area int check (area >=600 and area <=1800))
6: Create a database table that contains calculated fields
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Create table Db_table8
- (
- Employee number int primary key,
- The employee number varchar ( a) is unique,
- Warehouse number varchar (a),
- Base pay int check (base pay >=800 and base salary <=2100),
- Overtime pay int,
- Bonus int,
- Deduction rate int,
- Salary should be paid as (base salary + Overtime salary + bonus-deduction rate)
- )
Use Db_sqlserver;gocreate table Db_table8 ( employee number int primary key, employee number varchar () unique, warehouse number varchar (50), c3/> Base Salary int Check (base salary >=800 and base salary <=2100), overtime pay int, bonus int, deduction rate int, pay as (base salary + overtime wage + bonus- Deduction rate))
7: Create a database table with AutoNumber fields
[SQL]View Plaincopyprint?
- USE&NBSP;DB_SQLSERVER;&NBSP;&NBSP;
- go
- create table db_table9
- (
- warehouse number int identity ( primary KEY,&NBSP;&NBSP;
- Warehouse number varchar ( unique,
- city varchar (+) default ( ' Qingdao '),
- area int check (area >=300 and area <=1800)
- )
Use Db_sqlserver;gocreate table Db_table9 ( warehouse number int identity (primary) key, warehouse number varchar () Unique, city varchar (' Qingdao '), area int check (area >=300 and area <=1800))
To add a record to a table:
[SQL]View Plaincopyprint?
- INSERT INTO [db_sqlserver].[ DBO]. [Db_table9] (Warehouse number, area) values (' 400 ', 1600);
insert into [db_sqlserver]. [dbo]. [Db_table9] (Warehouse number, area) VALUES (' 400 ', 1600);
The warehouse number is automatically incremented
8: Create a data table with sort fields
[SQL]View Plaincopyprint?
- Create table Db_table10
- (
- Warehouse number int identity (1, 1) primary key,
- Warehouse number varchar (COLLATE) French_ci_ai not null,
- City varchar (+) default ' Qingdao ',
- Area int Check (area >=300 and area <=1800)
- )
CREATE TABLE Db_table10 ( warehouse number int identity (1, 1) primary key, warehouse number varchar () collate french_ci_ai NOT NULL, City varchar (+) Default ' Qingdao ', area int check (area >=300 and area <=1800))
The warehouse number is a sort field where CI (case insensitive) means case-insensitive, and AI (accent insensitive) denotes accent-insensitive, which creates a case-insensitive
and accent-insensitive sorting. If you want to differentiate between size and sorting, modify the code to: French_cs_as
9: Dynamically determine whether a database table exists
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- if (Exists (select * from sys.sysobjects where id=object_id (' db_table9 ')))
- Print ' database table name already exists '
- Else
- Print ' The database table name does not exist and can be used to create the table '
Use Db_sqlserver;goif (Exists (SELECT * from sys.sysobjects where id=object_id (' Db_table9 '))) print ' database table name already exists ' Else print ' The database table name does not exist, and you can create a table with that name '
10: View the various information of the table, you can view the properties of the specified database table, the field properties in the table, various constraints and other information
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Execute sp_help db_table9;
Use Db_sqlserver;goexecute sp_help db_table9;
11: View the database table's property information with the SELECT statement
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- SELECT * from sysobjects where type=' U '
Use Db_sqlserver;goselect * from sysobjects where type= ' U '
12: Renaming a database table
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Execute sp_rename "Db_table9", "db_renametable"
Use Db_sqlserver;goexecute sp_rename "Db_table9", "db_renametable"
13: Add new fields to the database table
[SQL]View Plaincopyprint?
- USE&NBSP;DB_SQLSERVER;&NBSP;&NBSP;
- go
- alter table db_table1 add e-mail varchar ( );
- alter table db_table1 add Contact varchar (+) default ' 0532-88886396 '
- &NBSP;&NBSP;
- select name field name, xusertype type number, length length from syscolumns where id = object_id ( ' db_table1 ')
Use db_sqlserver;goalter table db_table1 add email varchar (0532-8888) ALTER TABLE DB_TABLE1 add contact varchar (+) Default ' 6396 ' Select Name field name, Xusertype type number, length from syscolumns where id = object_id (' Db_table1 ')
14: Modify the fields of a database table
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Alter table DB_TABLE1 alter column e-mail varchar ($)
- Select Name field name, Xusertype type number, length from syscolumns where id = object_id (' db_table1 ')
Use Db_sqlserver;goalter table db_table1 alter COLUMN email varchar ($) Select Name Field name Xusertype type number length from sys columns WHERE id = object_id (' Db_table1 ')
15: Delete a database table field
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Alter table Db_table1 drop column e-mail
- Select Name field name, Xusertype type number, length from syscolumns where id = object_id (' db_table1 ')
Use Db_sqlserver;goalter table db_table1 drop column e-mail select Name field name, Xusertype type number, length from syscolumns where id = object_id (' Db_table1 ')
16: Delete Database tables
[SQL]View Plaincopyprint?
- Use Db_sqlserver;
- Go
- Drop Table Db_table1
- Drop table Db_table1, Db_table2
Use Db_sqlserver;godrop table Db_table1drop table Db_table1, Db_table2
If you delete a database table that has dependent associations, that is, the primary key, the Foreign key key table, you delete the association constraint between the two tables before you can delete the table. Note that you can also delete a database table that references the table before you delete the table.
Creating tables in SQL Server