alter-table

MySQL very slow for alter table query

三世轮回 提交于 2019-11-27 05:09:20
问题 Why is it taking more than an hour to simply update this table to add a column? This table has 15M rows. It has 2 indexes and a single key primary key. The ALTER TABLE query has been in "copy to tmp table" state for 1 hour 15 minutes now. ALTER TABLE `frugg`.`item_catalog_map` ADD COLUMN `conversion_url` TEXT NULL DEFAULT NULL Table: mysql> describe item_catalog_map; +------------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----

Modify column Vs change column

我怕爱的太早我们不能终老 提交于 2019-11-27 03:49:51
I know, we can not rename a column using modify column syntax ,but can change column syntax . My question is: what is the main usage of modify syntax ? For example, alter table tablename change col1 col1 int(10) not null instead of alter table tablename modify col1 int(10) not null Edited Question replaced What is the main usage of modify syntax ? Above question was replaced by below Why we have to use change column instead of modify column? Parvathy CHANGE COLUMN If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't

Alter table if exists or create if doesn't

我怕爱的太早我们不能终老 提交于 2019-11-27 03:09:07
问题 I need to run an installer which can also be an updater. The installer needs to be able to end up having a certain scheme/structure of the mysql database, regardless if some of the tables existed, missed a few columns, or need not to be changed because their structure is up to date. How can I make an elegant combination of ALTER and CREATE ? I was thinking there must be something like "ADD... IF... Duplicate" Say I have table A. In one client the table has one column -A1, and another client

check if column exists before ALTER TABLE — mysql

痴心易碎 提交于 2019-11-27 03:08:16
问题 Is there a way to check if a column exists in a mySQL DB prior to (or as) the ALTER TABLE ADD coumn_name statement runs? Sort of an IF column DOES NOT EXIST ALTER TABLE thing. I've tried ALTER IGNORE TABLE my_table ADD my_column but this still throws the error if the column I'm adding already exists. EDIT: use case is to upgrade a table in an already installed web app-- so to keep things simple, I want to make sure the columns I need exist, and if they don't, add them using ALTER TABLE 回答1:

Alter column, add default constraint

徘徊边缘 提交于 2019-11-27 00:07:23
问题 I have a table and one of the columns is "Date" of type datetime. We decided to add a default constraint to that column Alter table TableName alter column dbo.TableName.Date default getutcdate() but this gives me error: Incorrect syntax near '.' Does anyone see anything obviously wrong here, which I am missing (other than having a better name for the column) 回答1: Try this alter table TableName add constraint df_ConstraintNAme default getutcdate() for [Date] example create table bla (id int)

How do I add more members to my ENUM-type column in MySQL?

笑着哭i 提交于 2019-11-26 23:59:11
问题 The MySQL reference manual does not provide a clearcut example on how to do this. I have an ENUM-type column of country names that I need to add more countries to. What is the correct MySQL syntax to achieve this? Here's my attempt: ALTER TABLE carmake CHANGE country country ENUM('Sweden','Malaysia'); The error I get is: ERROR 1265 (01000): Data truncated for column 'country' at row 1. The country column is the ENUM-type column in the above-statement. SHOW CREATE TABLE OUTPUT: mysql> SHOW

How can I add a column to a Postgresql database that doesn't allow nulls?

谁都会走 提交于 2019-11-26 23:53:57
问题 I'm adding a new, "NOT NULL" column to my Postgresql database using the following query (sanitized for the Internet): ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL; Each time I run this query, I receive the following error message: ERROR: column "mycolumn" contains null values I'm stumped. Where am I going wrong? NOTE: I'm using pgAdmin III (1.8.4) primarily, but I received the same error when I ran the SQL from within Terminal. 回答1: You have to set a default value.

Create an index on a huge MySQL production table without table locking

筅森魡賤 提交于 2019-11-26 22:30:27
问题 I need to create an index on a ~5M rows MySQL table. It is a production table, and I fear a complete block of everything if I run a CREATE INDEX statement... Is there a way to create that index without blocking inserts and selects? Just wondering I have not to stop, create index and restart my system! 回答1: [2017] Update: MySQL 5.6 has support for online index updates https://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html In MySQL 5.6 and higher, the table remains available

ALTER TABLE to add a composite primary key

こ雲淡風輕ζ 提交于 2019-11-26 21:23:28
I have a table called provider . I have three columns called person , place , thing . There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination. How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns? ALTER TABLE provider ADD PRIMARY KEY(person,place,thing); If a primary key already exists then you want to do this ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing); @Adrian Cornish's answer is correct. However, there is another caveat to

How can I modify the size of column in a mysql table?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 19:27:53
I have created a table and accidentally put varchar length as 300 instead of 65353 . How can I fix that? An example would be appreciated. Have you tried this? ALTER TABLE <table_name> MODIFY <col_name> VARCHAR(65353); This will change the col_name 's type to VARCHAR(65353) ALTER TABLE <tablename> CHANGE COLUMN <colname> <colname> VARCHAR(65536); You have to list the column name twice, even if you aren't changing its name. Note that after you make this change, the data type of the column will be MEDIUMTEXT . Miky D is correct, the MODIFY command can do this more concisely. Re the MEDIUMTEXT