我正在尝试创建一个存储过程来将ntext我的数据库中的所有列转换为nvarchar(max).
这是代码
ALTER PROCEDURE [dbo].[usp_SL_ConvertNtextToNvarchar]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @table_name nvarchar(128)
DECLARE @column_name nvarchar(128)
DECLARE @totalCount int
DECLARE @count int
SET @totalCount = 0;
SET @count = 0;
-- Eventlogic
DECLARE tables_cursor CURSOR FOR
SELECT so.name as table_name, sc.name as column_name
FROM sys.objects so
JOIN sys.columns sc ON so.object_id = sc.object_id
JOIN sys.types stp ON sc.user_type_id = stp.user_type_id
AND stp.name = 'ntext'
WHERE so.type = 'U' -- to show only user tables
OPEN tables_cursor …Run Code Online (Sandbox Code Playgroud) 我有一个包含超过 5000 万行的表。
当我添加一列时:
ALTER TABLE my_table ADD my_column NUMBER;
Run Code Online (Sandbox Code Playgroud)
它非常快。
添加该列后,我将其删除。
ALTER TABLE my_table DROP COLUMN my_column;
Run Code Online (Sandbox Code Playgroud)
需要3分钟!
为什么添加一列如此之快,而删除它而不填充它却很慢?
我想通过添加一列来更改表格。到目前为止,这里没什么可看的,但我想让这个列成为复合键的一部分,即,我现在有一个布局
table_name( Field_1 datatype PK, Field_2 datatype,....)
Run Code Online (Sandbox Code Playgroud)
并且我希望插入的列,比如 Field_k 与现有的单字段 PK 一起成为 PK 的一部分。
我还没有找到有关如何执行此操作或是否可行的任何来源。请问有什么建议吗?
I have a SQL Server Express 2012 database. There is a table which previously had a column of type nvarchar(255). Now my requirements have changed and I am required to store long strings in the same column, so I modified the columns to have size 512.
alter Table [dbo].[mytable] ALTER COLUMN [mycol] nvarchar(512) NULL;
Run Code Online (Sandbox Code Playgroud)
Now when I try updating data in the column whose size was increased, the update goes through fine. But when I query the data, the …
说明:我有一个日期格式为“mm/dd/yyyy”的 oracle 数据库表和一个包含日期列的唯一约束。我的问题是我似乎无法找到更改内部日期格式的方法,我需要深入到第二个(即“mm/dd/yyyy hh12:mi:ss”)。我不想更改输出格式的查询,我想更改要更新的存储值。
问题:甚至可以将数据库精度降低到秒吗?如果是这样,我假设它是更新或更改命令?
我是甲骨文新手。我需要将 SQL Server 命令移植到 Oracle。
我想更改列以添加具有默认值的新约束。
ALTER TABLE <schema_name>.<table_name>
ADD CONSTRAINT [<constraint_name>]
DEFAULT (1) FOR [<column_name>]
Run Code Online (Sandbox Code Playgroud)
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
DEFAULT (1) FOR <column_name>
Run Code Online (Sandbox Code Playgroud)
运行 Oracle 查询时出现以下错误:
Run Code Online (Sandbox Code Playgroud)Error report - SQL Error: ORA-00904: : invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause: *Action:
可能是因为FOR标识符无效?
但是如何为特定列添加具有默认值的约束呢?
我们是否需要更改列并设置默认值?
哪种方法是正确的?
我有带有 id(primary key - int) 和身份规范 (1,1) 的表。
我想将此列更改为uniqueidentifier默认值 ( newid()) 。
我尝试过的是
ALTER TABLE myTable ALTER COLUMN id uniqueidentifier default NEWID();
Run Code Online (Sandbox Code Playgroud)
但我收到了这条消息
Incorrect syntax near the keyword 'default'.
Run Code Online (Sandbox Code Playgroud)
此外,我的数据库的每个表都有id主键列,我想通过循环或其他方式将它们更改uniqueidentifier为默认值。newID()
sql-server primary-key uniqueidentifier sql-server-2008-r2 alter-table
我向这样的某些列添加了 NOT NULL 约束,希望命令非常快,因为NOT VALID. 但是,大约需要 60-100 秒,并且似乎一直锁定表(基于在此期间失败的其他事情)。这是在一个大约有 8,600,000 行的表上。为什么会这样?执行 4 个命令而不是 1 个命令会更好吗?
Postgres 12
ALTER TABLE my_table
ADD CONSTRAINT my_table_column1_not_null CHECK (column1 IS NOT NULL) NOT VALID,
ADD CONSTRAINT my_table_column2_not_null CHECK (column2 IS NOT NULL) NOT VALID,
ADD CONSTRAINT my_table_column3_not_null CHECK (column3 IS NOT NULL) NOT VALID,
ADD CONSTRAINT my_table_column4_not_null CHECK (column4 IS NOT NULL) NOT VALID;
Run Code Online (Sandbox Code Playgroud) 下面是我的表架构
create table employee(
emp_id numeric(4),
fname varchar(10),
lname varchar(10),
mgr_id numeric(4),
hire_date date,
job_id numeric(2),
dept_id numeric(3)
);
Run Code Online (Sandbox Code Playgroud)
现在我想使用ALTER Table命令 withAlter Column而不是Modify从这里

以下是我的命令:-
ALTER TABLE employee ALTER COLUMN dept_id int(8);
但我收到一条错误,指出语法错误:-
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int(8)' at line 1
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用Modify它可以工作,那么为什么 ALTER COLUMN 不起作用。根据官方文档,该命令是正确的。我正在使用 MySQL 8.0 。
我正在尝试将测试系统上的表同步到生产中的表。在生产中,尽管可能很糟糕,但我相信他们使用保留字 SITE 作为列名。
alter table MY_TABLENAME
modify ("SITE" varchar2(50),
LOCATION_ID varchar2(50)
)
;
Run Code Online (Sandbox Code Playgroud)
以上给出了错误:
ORA-00904: "SITE": 无效标识符
没有引号或括号的相同错误。
我想将一列从 转换varchar(50)为bigint或int。
SQL Server 2012中表有20亿条数据有什么缺点?
数据长度为 11,数据只有数字。它们被存储为 varchar。现在我必须为它创建索引。所以我想我必须转换int为创建索引。该表没有约束和索引。目前没有人使用那张桌子。例如:
data
-----
12345678911
12345678915
12345678911
12345678911
12345678914
12345678913
12345678912
Run Code Online (Sandbox Code Playgroud) alter-table ×11
sql-server ×4
oracle ×2
constraint ×1
cursors ×1
date-format ×1
ddl ×1
foreign-key ×1
mysql ×1
mysql-8.0 ×1
oracle-11g ×1
oracle-12c ×1
postgresql ×1
primary-key ×1
update ×1