当前位置:网站首页>SQL -- data definition
SQL -- data definition
2022-04-23 06:25:00 【Wood acridine】
Create table (Create table)
grammar :
Create table Database name Schema name Table name (
Name 1 data type PRIMARY KEY,
Name 2 data type not null,
………
);
If you do not specify a schema, it will be in the current database by default dbo In mode
example :create table stu(
id int identity primary key, -- identity Identity column primary key Primary key
name varchar (20) not null,
birthday datetime
);
Copy table
If the target table exists
grammar :insert into Target table [ Specified field ] select * from The original table ;
If the target table does not exist
grammar :select * into Target table from The original table ;
Identity column
Create identity column (identity)
grammar :identity [( The value of the first line , Incremental value added to the identification value of the previous line )]
Don't write , If you don't write, it defaults to (1,1), You can also customize it .
Allow only one identity column per table
example :create table a(
aid int identity(1,2) primary key,
aname varchar (50) not null
);
Reuse identity values
SQL server Identifiers cannot be reused . If you insert a row into the identifier and execute insert Statement failed or rolled back , Then the identification value will be lost , Will not be generated again . It will result in blank space in the identification column
Reset the identity column of the entire table ( Delete data )
truncate Delete all values in the table and reset the identifier , But the data in the table will be lost and cannot be retrieved .
grammar :truncate table Table name ;
Reset identity column ( Do not delete data )
- dbcc checkident(‘ Table name ’,new_value) Reset new identifier ,new_value For the new value
- select ident_current(‘a’) The maximum value of the current table ID column
- select @@identity The maximum value of the current identity column . notes : Follow the wrong statement to query
Add columns... To the table
grammar :alter table Table name add Name data type constraint
example :alter table stu
add tel nvarchar (11) not null;
Modify the column
You can modify the data type , Column size 、not null constraint
grammar :alter table Table name alter column Name data type ;
example :alter table stu alter column tel varchar(22)
When changing the data type when there is data in the table , The data should be able to be transformed into the target type , If it doesn't change, it will report an error . The two data types must be compatible .
Delete column
grammar :alter table Table name drop column Name
example :alter table stu drop column tel;
Delete table (drop)
grammar :drop table [ if exists] Table name ;
add to if exists You can't report an error , Whether that table exists or not
Delete tables with foreign key constraints
Constraint referenced table . To delete this table , You must first delete the reference foreign key constraint or reference table .
grammar :
drop table Reference table ;
drop table Referenced table ;
If you use a single drop table Delete two tables , You must put the reference table first
grammar :drop table Reference table , Referenced table ;
Truncation table (truncate table)
truncate table Similar to no where Clause delete sentence . however truncate Statements execute faster , And use less system and transaction log resources .
And delect Comparative advantage :
- Use fewer transaction logs
delete Statement Deletes one line at a time , And insert an entry in the transaction log for each Deleted Row .truncate table Statement deletes data by releasing the data page used to store table data , And only insert page deallocation in the transaction log .
- Use fewer locks
When executing a statement with a row lock , Every row in the table is locked for deletion .truncate table Lock tables and pages , Not every line
- Reset identification
If the table to be truncated has an identity column , When you use truncate table Statement after deleting data , The calculator with the identity column will be reset to the starting value ( It's usually :1).
版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616404940.html
边栏推荐
- [leetcode 202] happy number
- 1. Calculate a + B
- Linear algebra Chapter 1 - determinant
- POI and easyexcel exercises
- St table template
- Calculation (enter the calculation formula to get the result)
- lambda expressions
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- POJ - 2955 brackets interval DP
- [leetcode 954] double pair array
猜你喜欢
[leetcode 19] delete the penultimate node of the linked list
線性代數第二章-矩陣及其運算
Linear algebra Chapter 2 - matrices and their operations
线性代数第三章-矩阵的初等变换与线性方程组
Implementation of displaying database pictures to browser tables based on thymeleaf
Import of data
Advanced operation of idea debug
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
Graphic numpy array matrix
MySQL table constraints and table design
随机推荐
Collection and map thread safety problem solving
Supply chain service terms
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
PHP processing JSON_ Decode() parses JSON stringify
Contrôle automatique (version Han min)
Animation - Introduction to keyframes
H. Are You Safe? Convex hull naked problem
Consistent hash algorithm used for redis cache load balancing
Best practices for MySQL storage time
DBCP usage
@Problems caused by internal dead loop of postconstruct method
Implementation of displaying database pictures to browser tables based on thymeleaf
List segmentation best practices
Rainbow (DP)
Substring Inversion (Easy Version)
Use of multithreaded executors
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
RPC must know and know
检测技术与原理