alter-table

SQL Server conditional CHECK constraint

社会主义新天地 提交于 2019-12-22 11:33:54
问题 I'm using SQL Server 2008 Management Studio. Below is what I have to write, and I'm having some difficulties for the second constraint. It is a little bit confusing me and I would really appreciate some help. Write an ALTER TABLE statement that adds two new check constraints to the Invoices table of the AP database. The first should allow (1) PaymentDate to be null only if PaymentTotal is zero and (2) PaymentDate to be not null only if PaymentTotal is greater than zero. The second constraint

Sqlite ALTER TABLE - add column between existing columns?

荒凉一梦 提交于 2019-12-22 09:22:22
问题 If I have a table with columns: a, b, c and later I do a ALTER TABLE command to add a new column "d", is it possible to add it between a and b for example, and not at the end? I heard that the position of the columns affects performance. 回答1: It's not possible to add a column between two existing columns with an ALTER TABLE statement in SQLite. This works as designed. The new column is always appended to the end of the list of existing columns. As far as I know, MySQL is the only SQL (ish)

Unable to drop constraint in SQL server 2005, “Could not drop constraint. See previous errors”

穿精又带淫゛_ 提交于 2019-12-22 05:22:08
问题 I'm trying to drop a constraint on a DB table, something like: ALTER TABLE MyTable drop CONSTRAINT FK_MyTable_AnotherTable But the execution just runs and runs. If I stop it I see: Msg 3727, Level 16, State 0, Line 2 Could not drop constraint. See previous errors. Web search throws up various pages but note that the constraint is properly named and I am trying to remove it using the correct name 回答1: I was having the same issue on SQL Server 2008 R2 , I solved my problem with below line

SQL Server - Convert VarChar to Date in ALTER TABLE

心已入冬 提交于 2019-12-21 20:28:08
问题 Saw a few threads on this but I have a specific instance where I am trying to do the conversion within an ALTER TABLE statement. ALTER TABLE Leads ALTER COLUMN [Created Date] Date This is throwing an error: Msg 241, Level 16, State 1, Line 34 Conversion failed when converting date and/or time from character string. The statement has been terminated. Created Date is currently set as (varchar(max), null) 回答1: You could standardize the date format. Something like this: UPDATE Leads SET [Created

MySQL - How do I update the decimal column to allow more digits?

我是研究僧i 提交于 2019-12-21 07:14:41
问题 I'm a beginner in MySQL, and I accidentally created a table with a column named (price decimal(2,2)); It needs to be decimal(4,2) to allow 4 digits. Since I've already created it, what is the easiest way to update that decimal value to decimal(4,2) ? Or do I need to drop that column completely, and re-create it with the correct numbers? I can't seem to get the syntax right. Thank you very much. 回答1: ALTER TABLE mytable MODIFY COLUMN mycolumn newtype example: ALTER TABLE YourTableNameHere

How to alter “REFERENCES” in PostgreSQL?

烂漫一生 提交于 2019-12-21 07:12:10
问题 How can I alter the reference to a table in PostgreSQL when the table name has been changed? Say I have: CREATE TABLE example1 ( id serial NOT NULL PRIMARY KEY, name varchar(100) ); CREATE TABLE example2 ( id serial NOT NULL PRIMARY KEY, example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY DEFERRED ); Later I do: ALTER TABLE example1 RENAME TO example3; How to change the definition of the foreign key constraint? example1fk integer REFERENCES example1 (id) DEFERRABLE INITIALLY

Alter multiple columns in a single statement [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-20 15:44:40
问题 This question already has answers here : How do I Alter Table Column datatype on more than 1 column? (2 answers) Closed 6 years ago . I am using a query to alter the charset of a column ALTER TABLE `media_value_report` CHANGE `index_page_body` `index_page_body` TEXT CHARACTER SET utf8 NULL DEFAULT NULL i want to do this for other columns main_title, landing_page_body as well. But am getting a #1064 error while executing. Can i alter-change multiple columns in a single query? I tried but i

ALTER TABLE on dependent column

吃可爱长大的小学妹 提交于 2019-12-20 09:24:29
问题 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 TABLE: Add a new boolean column with default value and checkbox

守給你的承諾、 提交于 2019-12-19 19:57:39
问题 What is the correct Access DDL query to add a Boolean datatype column to a table? So far I've seen examples like the following... ALTER TABLE MyTable ADD MyNewColumName BIT but they do not seem to be 100% correct since Access does not apply the checkbox control to the newly added column, and the allowed values for that column seem to be 0 and -1 回答1: A DAO example. ''Requires reference to Microsoft DAO 3.6 Object Library Dim tdf As DAO.TableDef Dim fld As DAO.Field Dim db As Database Dim

Rename column only if exists

爷,独闯天下 提交于 2019-12-19 17:31:24
问题 PostgreSQL does not allow ALTER TABLE t RENAME COLUMN IF EXISTS c1 TO c2 ...or anything like that. However, it's very convenient to be able to write scripts which modify DB structure which can be run again without first checking if it has already been run. How do I write a PostgreSQL function to do exactly this? 回答1: Better to have two functions, one calling the other: CREATE OR REPLACE FUNCTION column_exists(ptable TEXT, pcolumn TEXT) RETURNS BOOLEAN AS $BODY$ DECLARE result bool; BEGIN --