alter-table

Add a column if it doesn't exist to all tables?

[亡魂溺海] 提交于 2019-12-03 01:09:49
I'm using SQL Server 2005/2008. I need to add a column to a table if it does not yet exist. This will apply to all tables in a given database. I hoped I was close, but I'm having issues with this solution. How can this be done? Here's what I have: EXEC sp_MSforeachtable ' declare @tblname varchar(255); SET @tblname = PARSENAME("?",1); if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = @tblname and column_name = ''CreatedOn'') begin ALTER TABLE @tblname ADD CreatedOn datetime NOT NULL DEFAULT getdate(); end ' But I get errors: Error 102: Incorrect syntax near '

Alter table to modify default value of column

可紊 提交于 2019-12-02 22:04:45
I have a requirement where we need to modify a column's default value in database table. The table is already an existing table in database and currently the default value of the column is NULL. Now if add a new default value to this column, If I am correct it updates all the existing NULLs of the column to new DEfault value. Is there a way to not to do this but still set a new default value on column. I mean I do not want the existing NULLs to be updated and want them to remain as NULLs. Any help on this is appreciated. Thanks Your belief about what will happen is not correct. Setting a

Is there a way to add column at a specified position in Oracle table? [duplicate]

心已入冬 提交于 2019-12-02 21:22:41
问题 This question already has answers here : How to insert a column in a specific position in oracle without dropping and recreating the table? (4 answers) Closed 3 years ago . Consider this inital table I have created in Oracle 10G: ╔═════════════════════════════════╗ ║ CUSTOMER_ID ACC_NO ACC_BALANCE ║ ╠═════════════════════════════════╣ ║ 100 200 1000 ║ ║ 101 150 4000 ║ ║ 102 350 2000 ║ ║ 103 450 2500 ║ ║ 104 550 2200 ║ ╚═════════════════════════════════╝ Now I want to add another column

Change a Nullable column to NOT NULL with Default Value

孤街浪徒 提交于 2019-12-02 19:55:07
I came across an old table today with a datetime column called 'Created' which allows nulls. Now, I'd want to change this so that it is NOT NULL, and also include a constraint to add in a default value (getdate()). So far I've got the following script, which works fine provided that i've cleaned up all the nulls beforehand: ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL Is there any way to also specify the default value as well on the ALTER statement? I think you will need to do this as three separate statements. I've been looking around and everything i've seen seems to

ERROR 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key

孤人 提交于 2019-12-02 17:59:38
问题 I try to "alter Table" I need one more AI field, not key... "List" ID INT(11):PK Not Null AutoIn.. Name VARCHAR UserID INT(11):FK Not Null edit BOOL and now i need one more field "sortpos" as AI. I try it with MySQL Workbench ALTER TABLE `**mydb**`.`List` ADD COLUMN `sortpos` INT(11) NOT NULL AUTO_INCREMENT AFTER `edit`; Can u help me? Thx 回答1: You can't get better error message than this one. You already have ID defined as Auto Increment in your table. Now you are trying to add another field

how to modify the size of a column

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:57:31
I created the table Test_Project2 in Oracle SQL Developer. After that I realized that the column proj_name is of a small size, so I decided to modify the column using the follwoing statement ALTER TABLE TEST_PROJECT2 MODIFY proj_name VARCHAR2(300); but for some reason Oracle SQL Developer underscores the semi-colon with red and I do not what is mistake and how to correct it Test_Project2 : CREATE TABLE Test_Project2 ( proj_id number(30), proj_name VARCHAR2 (30), proj_desc VARCHAR2(300) ); Regardless of what error Oracle SQL Developer may indicate in the syntax highlighting, actually running

ALTER TABLE on dependent column

故事扮演 提交于 2019-12-02 17:51:54
I am trying to alter column datatype of a primary key to tinyint from int.This column is a foreign key in other tables.So,I get the following error: Msg 5074, Level 16, State 1, Line 1 The object 'PK_User_tbl' is dependent on column 'appId'. Msg 5074, Level 16, State 1, Line 1 The object 'FK_Details_tbl_User_tbl' is dependent on column 'appId'. Msg 5074, Level 16, State 1, Line 1 The object 'FK_Log_tbl_User_tbl' is dependent on column 'appId'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE ALTER COLUMN appId failed because one or more objects access this column. Is there any other way other

How to insert values into existing SQL Server tables with foreign keys

大城市里の小女人 提交于 2019-12-02 12:20:12
I have a database and I want to insert new values to the table, the problem is that I have two tables with FK to each other. I know the problem my be something with the Alter Table , however I can't figure out what cause it. First table: Department CREATE TABLE [dbo].[Department] ( [DID] [int] primary key, [Name] [varchar](255) , [Description] [varchar](255) , [ManagerId] [int] ); Second table: OfficialEmployee CREATE TABLE [dbo].[OfficialEmployee] ( [EID] [int] primary key, [StartDate] [date] , [Degree] [varchar](255) , [DepartmentId] [int] , CONSTRAINT [FK_DIDOfficial] FOREIGN KEY([EID])

pyodbc - add column to MS Access with default value

断了今生、忘了曾经 提交于 2019-12-02 12:09:48
问题 I'm trying to add a column to an MS Access database table using pyodbc and Python 3.5. Using the expression self.cursor.execute("ALTER TABLE data ADD COLUMN testColumn TEXT(10)") works fine, but when I try to add a default value (DEFAULT "no"), it throws a Syntax error. I've tried multiple combinations, but no luck. Any help is much appreciated! Cheers 回答1: Sadly, the Access ODBC driver simply does not support the DEFAULT clause for a column in CREATE/ALTER TABLE statements. The ODBC driver

Is there a way to add column at a specified position in Oracle table? [duplicate]

一世执手 提交于 2019-12-02 11:47:56
This question already has an answer here: How to insert a column in a specific position in oracle without dropping and recreating the table? 4 answers Consider this inital table I have created in Oracle 10G: ╔═════════════════════════════════╗ ║ CUSTOMER_ID ACC_NO ACC_BALANCE ║ ╠═════════════════════════════════╣ ║ 100 200 1000 ║ ║ 101 150 4000 ║ ║ 102 350 2000 ║ ║ 103 450 2500 ║ ║ 104 550 2200 ║ ╚═════════════════════════════════╝ Now I want to add another column customer_name into the table. I used: ALTER TABLE BANK_ACCOUNT ADD (CUSTOMER_NAME VARCHAR2(30)); and the column is being inserted