-
2021-01-20 10:08:06
SQL语句–在表中修改列的数据类型、添加列、删除列、
在表中,修改列的数据类型: alter table 表名 alter column 字段名 数据类型 eg: alter table Student alter column PhoneNumber nvarchar(4000)
在表中添加列: alter table 表名 add 字段名 数据类型 eg: alter table Student add PhoneNumber char(11)
在表中删除列: alter table 表名 drop column 字段名 eg: alter table Student drop column PhoneNumber
更多相关内容 -
SQL Server表中添加新列并添加描述
2020-09-10 01:35:46主要介绍了SQL Server表中添加新列并添加描述的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 -
SQL Server 2008怎样添加自增列实现自增序号
2020-09-10 18:42:04有的表需要添加自增列,在添加新纪录时自动添加一个序号,有两种不错的方法通过T-SQL代码、通过企业管理器在此分享给大家 -
sql添加列_SQL添加列操作
2020-07-17 21:10:15sql添加列 This article explains the SQL add column operation into an existing SQL table. We will also explore different examples of SQL add column operations. 本文介绍了对现有SQL表SQL添加列操作。 ...sql添加列
This article explains the SQL add column operation into an existing SQL table. We will also explore different examples of SQL add column operations.
本文介绍了对现有SQL表SQL添加列操作。 我们还将探讨SQL添加列操作的不同示例。
Sometimes we want to add columns into an existing table. In existing tables, we might have records in it. We do not want to lose existing data as well. In many circumstances, we can drop the tables and recreate them but this is not recommended generally, especially in a production environment, as it can be destructive as it pertains to data. We can still perform a SQL add column operation using Alter Table command, which avoids have to drop tables, delete data, even if only temporarily.
有时我们想将列添加到现有表中。 在现有表中,我们可能有记录。 我们也不想丢失现有数据。 在许多情况下,我们可以删除表并重新创建它们,但是通常不建议这样做,尤其是在生产环境中,因为它与数据有关可能具有破坏性。 我们仍然可以使用Alter Table命令执行SQL添加列操作,这避免了必须删除表,删除数据(即使只是暂时的)。
句法 (Syntax)
We can perform a SQL add column operation on a table with the following transact SQL command.
我们可以使用以下Transact SQL命令在表上执行SQL添加列操作。
ALTER TABLE table_name ADD column_name column_definition;
准备环境 (Prepare the environment)
We need to select a Database table and insert data into it.
我们需要选择一个数据库表并将数据插入其中。
Execute the following query to create an Employee table in SQLShackDemo database.
执行以下查询以在SQLShackDemo数据库中创建Employee表。
USE [SQLShackDemo] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Employee]( [EmpID] [int] IDENTITY(1,1) NOT NULL, [EmpName] [varchar](50) NULL, [City] [varchar](30) NULL, [Designation] [varchar](30) NULL, PRIMARY KEY CLUSTERED ( [EmpID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Execute the following query to insert sample data into it.
执行以下查询以将示例数据插入其中。
USE [SQLShackDemo]; GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (1, N'Charlotte Robinson', N'Chicago', N'Consultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (2, N'Madison Phillips', N'Dallas', N'Senior Analyst' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (3, N'Emma Hernandez', N'Phoenix', N'Senior Analyst' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (4, N'Samantha Sanchez', N'San Diego', N'Principal Conultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (5, N'Sadie Ward', N'San Antonio', N'Consultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (6, N'Savannah Perez', N'New York', N'Principal Conultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (7, N'Victoria Gray', N'Los Angeles', N'Assistant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (8, N'Alyssa Lewis', N'Houston', N'Consultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (9, N'Anna Lee', N'San Jose', N'Principal Conultant' ); GO INSERT INTO [dbo].[Employee] ([EmpID], [EmpName], [City], [Designation] ) VALUES (10, N'Riley Hall', N'Philadelphia', N'Senior Analyst' ); GO SET IDENTITY_INSERT [dbo].[Employee] OFF; GO
In the following screenshot, we can see the existing data in the Employee table.
在以下屏幕截图中,我们可以在Employee表中看到现有数据。
在现有SQL表上执行SQL添加列操作 (SQL add column operation on an existing SQL table)
We want to add the column department in the Employee table. Suppose we have many columns in a table; we need to check if a particular column exists in the SQL table or not. If the specified column does not exist, we want to create it with the appropriate data type.
我们要在Employee表中添加列部门 。 假设一个表中有很多列; 我们需要检查SQL表中是否存在特定的列。 如果指定的列不存在,我们想使用适当的数据类型创建它。
We can use the INFORMATION_SCHEMA view to check tables and their columns within a database. Execute the following code to get a list of columns, their data type in Employee table.
我们可以使用INFORMATION_SCHEMA视图来检查数据库中的表及其列。 执行以下代码以获取列列表,列的类型在Employee表中。
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee';
In this output, we can see the Employee table contains 4 columns.
在此输出中,我们可以看到Employee表包含4列。
Let’s add a new column Department with following Alter Table command.
让我们使用下面的Alter Table命令添加一个新的部门Department。
ALTER TABLE Employee ADD Department Varchar(50)
Execute this query and select records from the Employee table. In the following screenshot, we can look at the new column Department. All existing records contain a NULL value in this column.
执行此查询,然后从Employee表中选择记录。 在下面的屏幕截图中,我们可以查看新列Department。 全部存在 记录在此列中包含NULL值。
Previously, we checked all columns in the Employee table using INFORMATION_SCHEMA view. In the following query, we want to create a Department table only if it does not exist in the Employee table.
以前,我们使用INFORMATION_SCHEMA视图检查Employee表中的所有列。 在下面的查询中,我们只想创建一个Department表(如果它在Employee表中不存在)。
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee' AND COLUMN_NAME = 'Department' ) BEGIN ALTER TABLE Employee ADD Department VARCHAR(50); END;
We can add a column in an existing table if it allows NULL values or have a default value defined on it. We can try to add Not NULL column in the existing SQL table, but it gives the following error message,
如果它允许NULL值或在其上定义了默认值,则可以在现有表中添加一列。 我们可以尝试在现有SQL表中添加Not NULL列,但是会显示以下错误消息,
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee' AND COLUMN_NAME = 'Phone' ) BEGIN ALTER TABLE Employee ADD Phone VARCHAR(15) Not NULL; END;
SQL将列操作添加到具有默认值的现有SQL表中 (SQL add column operation to an existing SQL table with a default value )
Suppose we want to add the column IsActive column into the Employee table. We can have the following values in this column
假设我们要将列IsActive列添加到Employee表中。 我们可以在此列中具有以下值
- Value 1: Employee is active 值1:员工活跃
- Value 0: Employee is not active 值0 :员工未处于活动状态
By default, all existing and new employee should have Value 1 in IsActive column. We can specify a value using default constraint.
默认情况下,所有现有员工和新员工的IsActive列均应具有值1 。 我们可以使用默认约束来指定一个值。
If we try to add a column with a Not NULL value in the existing SQL table, we get following error message,
如果我们尝试在现有SQL表中添加具有非NULL值的列,则会收到以下错误消息,
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee' AND COLUMN_NAME = 'Phone' ) BEGIN ALTER TABLE Employee ADD IsActive bit DEFAULT(1); END;
Execute this query and Select records from a table. For existing records, it does not update the default values.
执行此查询,然后从表中选择记录。 对于现有记录,它不会更新默认值。
If we insert any new record in this table, it gets default value as per the following screenshot.
如果我们在此表中插入任何新记录,则它将按照以下屏幕截图获取默认值。
SQL向具有标识列的现有SQL表添加列操作 (SQL add column operation to an existing SQL table with an identity column )
In SQL Server, we use the Identity function to define a default and auto increment value for each new row. We can add an identity column to the existing SQL table as well. Let’s create a new table Employee_new without an identity column.
在SQL Server中,我们使用Identity函数为每个新行定义默认值和自动增量值。 我们也可以在现有SQL表中添加一个标识列。 让我们创建一个没有标识列的新表Employee_new 。
CREATE TABLE [dbo].[Employee_new]( [EmpID] [int] NOT NULL, [EmpName] [varchar](50) NULL, [City] [varchar](30) NULL, [Designation] [varchar](30) NULL )
Once the table is there, we can add an identity column with the following query.
表格到位后,我们可以使用以下查询添加一个身份列。
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee_new' AND COLUMN_NAME = 'ID' ) BEGIN ALTER TABLE Employee_new ADD ID INT IDENTITY(1,1) NOT NULL END;
We created the Identity column in a table without any record in it. Let’s drop the table and recreate it. Insert a few records with the following query.
我们在表中创建了Identity列,但其中没有任何记录。 让我们删除表并重新创建它。 使用以下查询插入一些记录。
INSERT INTO [dbo].[Employee_new] ([EmpID], [EmpName], [City], [Designation] ) VALUES (8, N'Alyssa Lewis', N'Houston', N'Consultant' ); GO INSERT INTO [dbo].[Employee_new] ([EmpID], [EmpName], [City], [Designation] ) VALUES (9, N'Anna Lee', N'San Jose', N'Principal Conultant' );
We have data in the Employee_new table. Let’s add an Identity column with Alter table command.
我们在Employee_new表中有数据。 让我们用Alter table命令添加一个Identity列。
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Employee_new' AND COLUMN_NAME = 'ID' ) BEGIN ALTER TABLE Employee_new ADD ID INT IDENTITY(1,1) NOT NULL END;
In the following screenshot, we can see it updates existing records as well.
在以下屏幕截图中,我们可以看到它也更新了现有记录。
具有标识列的现有SQL表的多个SQL添加列操作 (Multiple SQL add column operations for an existing SQL table with an identity column)
We might need to add multiple columns to an existing SQL table. We can do it within the same Alter table command.
我们可能需要将多个列添加到现有SQL表中。 我们可以在同一Alter table命令中完成此操作。
In the following query, we added two columns ZipCode and StateCode in a single Alter Table command. We need to specify all columns to add in a similar format.
在以下查询中,我们在一个Alter Table命令中添加了两列ZipCode和StateCode。 我们需要指定所有列以类似的格式添加。
ALTER TABLE Employee_new ADD ZipCode INT NULL, StateCode INT NULL; GO
We can get details of all columns and their properties using sp_help command.
我们可以使用sp_help命令获取所有列及其属性的详细信息。
sp_help 'Employee_new'
SQL使用SSMS中的表设计器将列操作添加到现有SQL表中 (SQL add column operation to an existing SQL table with the table designer in SSMS)
In previous examples, we used t-SQL to add columns in the existing table. We might not be familiar with writing t-SQL code. We can use the SSMS GUI as well to add a column.
在前面的示例中,我们使用t-SQL在现有表中添加列。 我们可能不熟悉编写t-SQL代码。 我们也可以使用SSMS GUI添加列。
Right click on the table and click on Design.
右键单击表格,然后单击“ 设计” 。
It opens a table designer. We can see all existing column, their data types, default values and other properties for a specified table in a table designer
打开表设计器。 我们可以在表设计器中查看指定表的所有现有列,它们的数据类型,默认值和其他属性
Provide a column name and select data types from the drop-down. We can add multiple columns in this with appropriate data types.
提供列名,然后从下拉列表中选择数据类型。 我们可以在其中添加具有适当数据类型的多个列。
Once done, Save and exit the table designer in SSMS. If you try to close it without saving changes, we get a warning message as well.
完成后, 保存并退出SSMS中的表设计器。 如果您尝试在不保存更改的情况下关闭它,我们也会收到一条警告消息。
Click on Yes to save new column in the existing table. We can either run a Select statement to verify the new column or use sp_help command to list all columns and their properties.
单击是将新列保存在现有表中。 我们可以运行Select语句以验证新列,也可以使用sp_help命令列出所有列及其属性。
sp_help 'Employee.'
In the following screenshot, we can see a new column in the Employee table.
在以下屏幕截图中,我们可以在Employee表中看到一个新列。
结论 (Conclusion)
In this article, we explored SQL add column operations to add a a new column to an existing SQL table. We can use both the GUI and transact SQL method to do it. I hope you found this article helpful. You can provide feedback or comments in the comments section below.
在本文中,我们探讨了SQL添加列操作,以向现有SQL表添加新列。 我们可以同时使用GUI和事务SQL方法。 希望本文对您有所帮助。 您可以在下面的评论部分中提供反馈或评论。
sql添加列
-
SQL在表中增加一列
2021-12-08 17:13:01语法: alter table 表名 add 列明 数据类型; 举例: 在Uni表中增加continent列。 代码: alter table Uni add continent nvarchar(50); 结果:语法:
alter table 表名 add 列明 数据类型;
举例:
在Uni表中增加continent列。代码:
alter table Uni add continent nvarchar(50);结果:
-
sqlserver语句添加列(简单)
2021-02-04 21:14:15展开全部 语句为... GO 2、删除列 alter table tableName drop column columnName 3、指定表中某列默认数据 ALTER TABLE dbo.doc_exz ADD CONSTRAINT col_b_def DEFAULT 50 FOR column_b ;展开全部
语句为32313133353236313431303231363533e78988e69d8331333431373235:
alter table A
add B VARCHAR(50)
create table C
(D varchar(10),
E varchar(10),
F varchar(10),
G varchar(10),
constraint pk_D primary key (D)
)
扩展资料:
注意事项
一、增加列:
1、alter table tableName add columnName varchar(30)
2、ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
二、修改列类型:
alter table tableName alter column columnName varchar(4000)
三、修改列的名称:
1、EXEC sp_rename 'tableName.column1' , 'column2' (把表名为tableName的column1列名修改为column2
下面的示例将TerritoryID表中的Sales.SalesTerritory列重命名为TerrID。
USE AdventureWorks2012;
GO EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO
2、删除列
alter table tableName drop column columnName
3、指定表中某列默认数据
ALTER TABLE dbo.doc_exz ADD CONSTRAINT col_b_def DEFAULT 50 FOR column_b ;
-
sql 添加列、删除列、修改列属性以及大小
2022-01-25 16:48:38sql 添加列、删除列、修改列属性以及大小 --删除列 ALTER TABLE 表名 drop column 列名; --添加列 ALTER TABLE 表名 add 列名 VARCHAR(40); COMMENT ON COLUMN "表名"."列名" IS '注释'; --修改列 alter table ... -
sql语句中查询出的数据添加一列,并且添加默认值
2021-01-14 04:40:43查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的select app_id,app_secret from wx_ticket group by app_id;查询出的数据是app_id | expires_in--------------------+------------wxeec89cdf... -
sql 语句 增加列,在指定列后面添加列
2019-06-11 09:04:00Alter table send add column type2 int NOT NULL DEFAULT '0' after id; //在send表 增加列type2 int类型 不为空 默认值为0 在列id后面加 ... -
SQL Server向表添加列
2020-12-28 21:02:59在本教程中,将学习如何使用SQL Server ALTER TABLE ADD语句将一个或多个列添加到表中。以下ALTER TABLE ADD语句将新列添加到表中:ALTER TABLE table_nameADD column_name data_type column_constraint;在上面语句... -
如何给查询出的SQL记录添加序号列
2020-09-27 19:07:04--给查询出的SQL记录添加序号列,解决方法有以下两种 --第一:这种方法是每条数据序号不同 select ROW_NUMBER() OVER (ORDER BY 字段名 ASC) AS 别名 from 表名 --第二:这种方法是相同数据序号相同 select ... -
sql语句里添加一列自然序号
2020-09-15 11:08:03SELECT @rownum:=@rownum+1 AS rownum, a.* FROM (SELECT @rownum:=0) r, a; -
SQL SERVER 添加序号列
2018-09-22 13:13:56SQL SERVER 添加序号列 select ROW_NUMBER() OVER(ORDER BY Convey_Fee desc) as z, rank() OVER(ORDER BY Convey_Fee desc) as z, DENSE_RANK() OVER(ORDER BY Convey_Fee desc) as z, NTILE(4) OVER(ORDER BY ... -
SQL 中的生成列/计算列以及主流数据库实现
2020-02-04 13:40:33在 SQL 数据库中,生成列(Generated Column)是指由表中其他字段计算得到的列,因此也称为计算列(Computed Column)。 本文介绍各种主流数据库对于生成列/计算列的实现,包括 Oracle、MySQL、SQL Server、... -
SQL添加一列到表中
2015-12-03 20:37:04有如下数据库表: tb_studentselect * from tb_student(1)在tb_student表额外添加一列 L3VPN,并且该L3VPN列无需含有默认值ALTER TABLE tb_student ADD l3vpn VARCHAR(10) (2)在tb_student表额外添加一列 L3VPN,... -
sqlserver 添加一列并添加默认
2016-04-18 12:18:20结果只是添加了列,默认值未添加上 分析: 既然该列有默认值,那么该列肯定非空 执行sql alter table 表名 add 列名 类型 not null default 默认值 结果ok 附加: 删除列约束 alter table ... -
MySQL数据库——使用SQL语句在指定列后添加列
2018-06-13 16:11:53alter table tablename add column columnname varchar(20)...注:这是MySQL的,tablename是要修改的表名,columnname是新增列,varchar(20) 是新增字段类型,not null是指新增字段不能为空值,somecolumn是原来的列... -
mysql如何给表中添加列(字段)?
2021-01-19 00:19:07mysql给表中添加列的方法:1、使用“ALTER TABLE 表名 ADD 新字段名 数据类型 [约束条件];”在末尾添加列;2、使用“ALTER TABLE 表名 ADD 新字段名 数据类型 [约束条件] FIRST;”在开头添加列。(推荐教程:mysql... -
SQL命令向表中添加列
2016-09-13 20:06:54SQL语言用ALTER TABLE语句修改基本表,其一般格式为: ALTER TABLE <表名> [ADD [COLUMN] <新列名><数据类型>[完整性约束]] [ADD <表级完整性约束>] ... -
SQL表增加列
2020-06-05 17:54:53作为测试工程师,当开发环境的数据库更新,比如加入了新字段,这就需要用到 alter 语句 语法: ALTER TABLE 表名 ADD ...加注释可读性更强, 这样子在Navicat里面表的对象信息可查看注释说明 大功告成。 2020年6月5日 -
Sql语句(添加列,修改列,删除列)
2018-07-06 14:54:15添加列: 执行下列命令,列会默认添加到表字段末尾 alter table 表名 add column 列名 varchar(30); 如果想指定字段位置,可以使用mysql提供的first(设定为第一列)和after(设定位于某个字段之后)first和 ... -
sql server 数据库表中增加列,增加字段,插入列,插入字段,修改列,修改字段,
2021-01-28 17:09:05sql server 数据库表中增加列,增加字段,插入列,插入字段,修改列,修改字段, -
SQL中add添加列语句
2017-02-28 18:55:29 -
sql语句添加列带注释默认为null
2018-12-05 15:09:13alter table usr_org_feedback add service_type TINYINT(4) NOT NULL default 0 COMMENT '1小类,2项目' AFTER username/first; 修改字段类型 ALTER TABLE user10 MODIFY email VARCHAR(50) NOT NULL DEFAULT '... -
Sql Server添加列并使用默认值填充已有记录
2019-03-03 15:26:20alter 语法 ALTER TABLE {TABLENAME} ADD {COLUMNNAME} { TYPE ...https://stackoverflow.com/questions/92082/add-a-column-with-a-default-value-to-an-existing-table-in-sql-server -
在数据表中添加一个字段的SQL语句怎么写
2019-08-26 13:46:45ALTER TABLE tb_s(表名) ADD keycode(字段) varchar(500) NOT NULL DEFAULT '' COMMENT ''; -
SqlServer 添加列并赋值
2016-03-17 14:35:00有个需求,需要给某张表添加一列...添加列 赋值 两个功能都不难,很快实现。 --add column alter table Med_Summary_Template add SummaryTypeID varchar(10); --set column value declare @ProgramName v... -
SQL Server向数据库表中添加主键列
2018-02-28 19:47:50SQL 目录:... SQL Server 数据库,向已设置主键的数据库表中插入新一列,并设为主键。 首先从基础知识开始看, 建表: create table 表名 ( 字段名1 int not null, …………, ... -
SQL查询结果自己添加一列自增字段
2019-05-21 17:21:43自行添加一列连续自增的ID,可用如下查询语句: (presto可用,亲测) SELECT Row_Number() over ( ) as init , * FROM 表名 或 使用关键字IDENTITY创建临时表 SELECT IDENTITY(int,1,1) as Nid,* INTO #T FROM ...