alter

mysql: how to truncate the length of a field

房东的猫 提交于 2019-11-28 07:21:26
问题 Alter table merry_parents change mobile mobile char(10). When I do the above I'm getting error: #1265 - Data truncated for column 'mobile' at row 2 How can I truncate my mobile field to char(10)? Currently it is char(12). 回答1: The error is telling you that there is data 12 characters long in row 2 (and probably others) so it's stopped the alter command to avoid losing data. Try updating your table using SUBSTRING() to shorten the column. It's unclear why you want to do this as you'll lose

How to change all the tables in my database to UTF8 character set?

青春壹個敷衍的年華 提交于 2019-11-28 04:56:27
My database is not in UTF8, and I'd like to convert all the tables to UTF8, how can I do this? Tomasz Zieliński For single table you can do something like this: ALTER TABLE tab CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; For the whole database I don't know other method than similar to this: http://www.commandlinefu.com/commands/view/1575/convert-all-mysql-tables-and-fields-to-utf8 mysqldump --user=username --password=password --default-character-set=latin1 --skip-set-charset dbname > dump.sql sed -r 's/latin1/utf8/g' dump.sql > dump_utf.sql mysql --user=username --password=password

Optimize mySql for faster alter table add column

ⅰ亾dé卋堺 提交于 2019-11-28 04:04:55
I have a table that has 170,002,225 rows with about 35 columns and two indexes. I want to add a column. The alter table command took about 10 hours. Neither the processor seemed busy during that time nor were there excessive IO waits. This is on a 4 way high performance box with tons of memory. Is this the best I can do? Is there something I can look at to optimize the add column in tuning of the db? RRUZ I faced a very similar situation in the past and i improve the performance of the operation in this way : Create a new table (using the structure of the current table) with the new column(s)

How to add new column in existing View in SQL-Server 2014 using Alter

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:37:56
问题 I have created a view that is based on another view and a table. I want to add new column of type varchar. I did like below, But getting syntax error? I am new to SQL, So,could not understand ALTER VIEW [dbo].[MyView] ADD New_Col varchar(10) null GO 回答1: you have to write the entire view again and just add or omit what you want to change for example your view is now : create view myView as select field1 from table1 and now you want to add a field called New_Col than you write this : alter

How can I alter this computed column in SQL Server 2008?

耗尽温柔 提交于 2019-11-28 01:47:04
I have a computed column created with the following line: alter table tbPedidos add restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 then 1 else 0 end as bit)) But, now I need to change this column for something like: alter table tbPedidos alter column restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 or restricaoValor = 1 then 1 else 0 end as bit)) But it's not working. I'm trying to input another condition to the case statement, but it's not working. Thanks a lot! If you're trying to change an existing column, you can't use ADD. Instead, try this:

How to change the primary key to be non-clustered?

别来无恙 提交于 2019-11-27 17:10:21
问题 Part-time reluctant DBA here. I want to change an existing primary key index from clustered to non-clustered. And the syntax is escaping me. This is how it's scripted out right now. ALTER TABLE [dbo].[Config] WITH NOCHECK ADD CONSTRAINT [PK_Config] PRIMARY KEY CLUSTERED ( [ConfigID] ) ON [PRIMARY] I am not seeing an ALTER CONSTRAINT statement in the online docs. 回答1: Drop the clustered index, then recreate the primary key as non-clustered: ALTER TABLE dbo.Config DROP CONSTRAINT PK_Config go

How to remove constraints from my MySQL table?

心已入冬 提交于 2019-11-27 17:02:03
I want to remove constraints from my table. My query is: ALTER TABLE `tbl_magazine_issue` DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users` But I got an error: #1064 - 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 'constraint FK_tbl_magazine_issue_mst_users ' at line 1 Mysql has a special syntax for dropping foreign key constraints: ALTER TABLE tbl_magazine_issue DROP FOREIGN KEY FK_tbl_magazine_issue_mst_users I had the same problem and I got to solve with this code: ALTER TABLE `table_name` DROP FOREIGN

How do I alter a mysql table column defaults?

让人想犯罪 __ 提交于 2019-11-27 13:50:57
I have a table with a column of type timestamp which defaults current_timestamp and updates to current_timestamp on every update. I want to remove the "on update" feature on this column. How do I write the alter statement? I tried the following: ALTER TABLE mytable alter column time set DEFAULT now(); but this didn't work. Pete was almost correct but used the wrong syntax for 'change': ALTER TABLE mytable CHANGE `time` `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP Notice that you must repeat the column name. Also, make sure you are using backticks instead of single quotes to escape the

How to change column datatype in SQL database without losing data

淺唱寂寞╮ 提交于 2019-11-27 10:34:46
I have SQL Server database and I just realized that I can change the type of one of the columns from int to bool . How can I do that without losing the data that is already entered into that table? You can easily do this using the following command. Any value of 0 will be turned into a 0 (BIT = false), anything else will be turned into 1 (BIT = true). ALTER TABLE dbo.YourTable ALTER COLUMN YourColumnName BIT The other option would be to create a new column of type BIT , fill it from the old column, and once you're done, drop the old column and rename the new one to the old name. That way, if

ALTER TABLE ADD COLUMN takes a long time

左心房为你撑大大i 提交于 2019-11-27 10:17:47
I was just trying to add a column called "location" to a table (main_table) in a database. The command I run was ALTER TABLE main_table ADD COLUMN location varchar (256); The main_table contains > 2,000,000 rows. It keeps running for more than 2 hours and still not completed. I tried to use mytop to monitor the activity of this database to make sure that the query is not locked by other querying process, but it seems not. Is it supposed to take that long time? Actually, I just rebooted the machine before running this command. Now this command is still running. I am not sure what to do. Romain