alter-table

Does adding a null column to a postgres table cause a lock?

你说的曾经没有我的故事 提交于 2019-12-31 17:51:37
问题 I think I read somewhere that running an ALTER TABLE foo ADD COLUMN baz text on a postgres database will not cause a read or write lock. Setting a default value causes locking, but allowing a null default prevents a lock. I can't find this in the documentation, though. Can anyone point to a place that says, definitively, if this is true or not? 回答1: The different sorts of locks and when they're used are mentioned in the doc in Table-level Locks. For instance, Postgres 11's ALTER TABLE may

How to delete a column from a table in MySQL

时光怂恿深爱的人放手 提交于 2019-12-29 10:03:55
问题 Given the table created using: CREATE TABLE tbl_Country ( CountryId INT NOT NULL AUTO_INCREMENT, IsDeleted bit, PRIMARY KEY (CountryId) ) How can I delete the column IsDeleted ? 回答1: ALTER TABLE tbl_Country DROP COLUMN IsDeleted; Here's a working example. Note that the COLUMN keyword is optional, as MySQL will accept just DROP IsDeleted . Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one. ALTER TABLE tbl_Country DROP COLUMN IsDeleted, DROP

ALTER table - adding AUTOINCREMENT in MySQL

狂风中的少年 提交于 2019-12-28 03:29:04
问题 I created a table in MySQL with on column itemID . After creating the table, now I want to change this column to AUTOINCREMENT . How can this be done using ALTER statements? Table definition: ALLITEMS (itemid int(10) unsigned, itemname varchar(50)) I am using the following code but it is throwing error: syntax incorrect . ALTER TABLE allitems MODIFY itemid INT(10) UNSIGNED AUTOINCREMENT; 回答1: CREATE TABLE ALLITEMS( itemid INT(10)UNSIGNED, itemname VARCHAR(50) ); ALTER TABLE ALLITEMS CHANGE

sqlite alter table add MULTIPLE columns in a single statement

[亡魂溺海] 提交于 2019-12-27 13:03:50
问题 Is it possible to alter table add MULTIPLE columns in a single statement in sqlite? The following would not work. alter table test add column mycolumn1 text, add column mycolumn2 text; 回答1: No, you have to add them one at a time. See the syntax diagram at the top of SQLite's ALTER TABLE documentation: There's no loop in the ADD branch so no repetition is allowed. 回答2: alter table test add column mycolumn1 text; alter table test add column mycolumn2 text; use the above redifined query 来源:

ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint [duplicate]

怎甘沉沦 提交于 2019-12-25 08:04:44
问题 This question already has an answer here : FOREIGN KEY SAME TABLE error - but foreign key doesn't exist (1 answer) Closed 2 years ago . I like to know if it's possible to create a self refering constraint using an ALTER TABLE statement. (tsql SQL Server 2012) expected I have a table with just a Primary Key called ID and column called parent_id I want to do something like this: ALTER TABLE myTable ADD CONSTRAINT FK_myTablemyTable FOREIGN KEY (parent_Id) REFERENCES myTable(Id) but I just get

How to add foreign key to MySQL table?

北城以北 提交于 2019-12-25 07:29:37
问题 I use MySQL with InnoDB engine. I double-checked type of columns. But always have: Error Code: 1215. Cannot add foreign key constraint I tried: ALTER TABLE `mail`.`boxes` ADD CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES `mail`.`users` (id) ON UPDATE NO ACTION ON DELETE NO ACTION; and ALTER TABLE `mail`.`boxes` ADD FOREIGN KEY (id) REFERENCES `mail`.`users` (id) Nothing works((( Please, help, what I am doing wrong (except choosing MySQL :-) )? 回答1: If table contains data then you are not able

Execution time of ALTER COLUMN

老子叫甜甜 提交于 2019-12-23 17:54:33
问题 Having a table with 60 columns, and 200 rows. Altering a BIT column from NULL to NOT NULL , now has a running execution time of over 3 hours. Why is this taking so long? This is the query that I'm execution: ALTER TABLE tbl ALTER COLUMN col BIT NOT NULL Is there a faster way to do it, besides creating a new column, updating it with values from the old column, then dropping the old column and renaming the new one? This is on MS SQL Server 2005. 回答1: IS the ALTER blocked by metadata shared

Is it possible to use Linq to ALTER a database table?

旧时模样 提交于 2019-12-23 12:36:37
问题 I am trying to programmatically drop a column in a table inside of an access database and found myself unable to do so! Is it at all possible? it makes me think that i don't have any clear idea of things linq to sql can't do. Any ideas? 回答1: There's nothing in LINQ to SQL that allows you to do that without writing T-SQL, no. Similarly, you can't do straight updates or deletes without selecting the data you want to alter first and manipulating the objects. You'd have to write stored procedures

ALTER TABLE with programmatically determined constant DEFAULT value

牧云@^-^@ 提交于 2019-12-23 09:32:39
问题 I am trying to add a column (MSSQL 2005) to a table (Employee) with a default constraint of a primary key of another table (Department). Then I am going to make this column a FK to that table. Essentially this will assign new employees to a base department based off the department name if no DepartmentID is provided. This does not work: DECLARE @ErrorVar INT DECLARE @DepartmentID INT SELECT @DepartmentID = DepartmentID FROM Department WHERE RealName = 'RocketScience' ALTER TABLE [Employee]

SQL Server Alter Computed Column

旧巷老猫 提交于 2019-12-23 08:49:47
问题 Does anyone know of a way to alter a computed column without dropping the column in SQL Server. I want to stop using the column as a computed column and start storing data directly in the column, but would like to retain the current values. Is this even possible? 回答1: Not that I know of but here is something you can do add another column to the table update that column with the values of the computed column then drop the computed column 回答2: Ok, so let me see if I got this straight. You want