Changing collation on a column with multipage rows - sql-server-2008

We're in the process of changing the collation of our database.
We've run into a problem, when I try to alter one of the columns (with the datatype varchar(max)) I get the following error:
Cannot create a row of size 8083 which is greater than the allowable maximum row size of 8060.
If I check the size of the biggest post.
select top 1 LEN(Document) as l1,* from GroupDocument where LEN(document) > 8000 order by LEN(document) desc
I get the size 39431 which would be approx 10 pages.
I assume that this is the problem why I cant change the collation. I havent run into this problem earlier with the other columns. Any help would be appreciated.
I guess one solution would be to copy all the content of the table to another table, change collation and then move it back again. But I'd rather not do that if it's possbile.
EDIT:
Tried the following:
create table temptable (id int, document nvarchar(max))
insert into temptable (id, document) select GroupDocumentID, Document from GroupDocument
alter table GroupDocument drop column Document
alter table temptable alter column document nvarchar(max)
ALTER TABLE [GroupDocument] add [Document] ntext COLLATE Finnish_Swedish_CI_AS NULL
update GroupDocument set Document = (select temptable.document from temptable where temptable.id = GroupDocument.GroupDocumentID)
Still the same problem.
The row that is causing the problem has a varchar that is 7996 bytes, that + some ints makes it a boundary case I guess.

I did a combination of forcing larger values to be out of row.
EXEC sp_tableoption 'dbo.GroupDocument',
'large value types out of row', 1
Cleaning the table.
dbcc cleantable('ExamDoc', 'groupdocument', 0)
and finally dropping and rebuilding the indexes for the table.
Solved the problem! :D

Related

Equal to operation not working on a field with updated collation

I was trying to convert an existing varchar column with a unique index on it to a case sensitive column. So to do this, I updated the collation of the particular column.
Previous value: utf8mb4_unicode_ci
Current value: utf8mb4_bin
Now I have a row in my table TEST_TABLE with test_column value is abcd.
When I try to run a simple query like SELECT * FROM TEST_TABLE WHERE test_column = 'abcd'; it returns no result.
However when I try SELECT * FROM TEST_TABLE WHERE test_column LIKE 'abcd'; it returns the data correctly.
Also when I try SELECT * FROM TEST_TABLE WHERE BINARY test_column = 'abcd'; it returns the data correctly.
One more thing I tried was creating a duplicate of the table with column collation set as utf8mb4_bin while creating itself and then copy all data from original table. Then the query SELECT * FROM TEST_TABLE WHERE test_column = 'abcd'; is working alright.
So this seems to be a problem with BINARY conversion. Is there any solution to this or Am I doing something wrong ?
This seems to be an issue with MySQL. The steps I followed to resolve this is as follows:
dropped the unique index on the column
change the collation of the column
created the unique index again
Now it is working as expected. It seems MySQL didn't rebuild unique index when collation was changed. However the above steps solved my issue.
How did you change the collation? There are about 4 ways that you might think to do it. Most do something different.
Probably ALTER TABLE ... CONVERT TO COLLATION utf8mb4_bin was what you needed.
Why "bin"? You want to match case and accents? That is "abcd" != "Abcd"?

How do I change the Auto Increment counter in MySQL?

I have an ID field that is my primary key and is just an int field.
I have less than 300 rows but now every time someone signs up that ID auto inc is inputted really high like 11800089, 11800090, etc.... Is there a way to get that to come back down so it can follow the order (310,311,312).
Thanks!
ALTER TABLE table_name AUTO_INCREMENT=310;
Beware though, you don't want to repeat an ID. If the numbers are that high, they got that way somehow. Be very sure you don't have associated data with the lower ID numbers.
https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
There may be a quicker way, but this is how I would do it to be sure I am recreating the IDs;
If you are using MySQL or some other SQL server, you will need to:
Backup your database
Drop the id column
Export the data
TRUNCATE or 'Empty' the table
Recreate the id column as auto_increment
Reimport the data
This will destroy the IDs of the existing rows, so if these are important, it is not a viable option.
The auto increment counter for a table can be (re)set two ways:
By executing a query, like others already explained:
ALTER TABLE <table_name> AUTO_INCREMENT=<table_id>;
Using Workbench or other visual database design tool. I am gonna show in Workbench how it is done - but it shouldn't be much different in other tool as well. By right click over the desired table and choosing Alter table from the context menu. On the bottom you can see all the available options for altering a table. Choose Options and you will get this form:
Then just set the desired value in the field Auto increment as shown in the image.
This will basically execute the query shown in the first option.
Guessing that you are using mysql because you are using PHP. You can reset the auto_increment with a statement like
alter table mytable autoincrement=301;
Be careful though - because things will break when the auto inc value overlaps
I believe that mysql does a select max on the id and puts the next. Try updating the ids of your table to the desired sequence. The problem you will have is if they're linked you should put a Cascade on the update on the fk.
A query that comes to my mind is:
UPDATE Table SET id=(SELECT max(id)+1 FROM TAble WHERE id<700)
700 something less than the 11800090 you have and near to the 300 WHERE id>0;
I believe that mysql complaints if you don't put a where
I was playing around on a similar problem and found this solution:
SET #newID=0;
UPDATE `test` SET ID=(#newID:=#newID+1) ORDER BY ID;
SET #c = (SELECT COUNT(ID) FROM `test`);
SET #s = CONCAT("ALTER TABLE `test` AUTO_INCREMENT = ",#c);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I hope that helps someone in a similar situation!

Merging two columns of fields in MySQL [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Merge date from one datetime and time from another datetime
How can I physically merge two MySQL column/fields into one column/field without losing any data on it
Specifically I will be combining a DATE and TIME column/field
thanks
Add your timestamp field, initialize it with the timestamp function, and then once you feel safe, drop the old columns. Something like this:
alter table your_table add timestamp_column timestamp;
update your_table set timestamp_column = timestamp(date_column, time_column);
-- Pause and make sure everything is okay.
-- Take your time, the database will wait.
alter table your_table drop column date_column;
alter table your_table drop column time_column;
alter table your_table modify timestamp_column timestamp not null;
By "merge", do you mean "add"? It sounds like you've got the date and time in separate fields, and so putting them together is the same thing as adding them.
If that's the case, you can use the MySQL's ADDTIME() function:
mysql> SELECT ADDTIME(aDate, aTime) FROM aTable;
If you mean to concatenate them, then MySQL has a CONCAT() function... But it doesn't seem suited for your purpose.
--edited--
If it's another column you're after, it's a two step process:
-- Add the column
mysql> ALTER TABLE aTable ADD COLUMN aDateTime DateTime DEFAULT NULL;
-- Insert the data
mysql> UPDATE aTable SET aDateTime = ADDTIME(aDate, aTime);
But after this point, you'll have to maintain it in all three columns. That is, aDateTime won't automatically update when you add/change aDate or aTime.
i dont wanna if you want to create a new field but just in case you wanna do it
alter table t add yournewfield datetime not null;
UPDATE table t SET younerwfield = addtime(t.date,t.time);

Column calculated from another column?

Given the following table:
id | value
--------------
1 6
2 70
Is there a way to add a column that is automatically calculated based on another column in the same table? Like a VIEW, but part of the same table. As an example, calculated would be half of value. Calculated should be automatically updated when value changes, just like a VIEW would be.
The result would be:
id | value | calculated
-----------------------
1 6 3
2 70 35
Generated Column is one of the good approach for MySql version which is 5.7.6 and above.
There are two kinds of Generated Columns:
Virtual (default) - column will be calculated on the fly when a
record is read from a table
Stored - column will be calculated when a
new record is written/updated in the table
Both types can have NOT NULL restrictions, but only a stored Generated Column can be a part of an index.
For current case, we are going to use stored generated column. To implement I have considered that both of the values required for calculation are present in table
CREATE TABLE order_details (price DOUBLE, quantity INT, amount DOUBLE AS (price * quantity));
INSERT INTO order_details (price, quantity) VALUES(100,1),(300,4),(60,8);
amount will automatically pop up in table and you can access it directly, also please note that whenever you will update any of the columns, amount will also get updated.
If it is a selection, you can do it as:
SELECT id, value, (value/2) AS calculated FROM mytable
Else, you can also first alter the table to add the missing column and then do an UPDATE query to compute the values for the new column as:
UPDATE mytable SET calculated = value/2;
If it must be automatic, and your MySQL version allows it, you can try with triggers
MySQL 5.7 supports computed columns. They call it "Generated Columns" and the syntax is a little weird, but it supports the same options I see in other databases.
https://dev.mysql.com/doc/refman/5.7/en/create-table.html#create-table-generated-columns
#krtek's answer is in the right direction, but has a couple of issues.
The bad news is that using UPDATE in a trigger on the same table won't work. The good news is that it's not necessary; there is a NEW object that you can operate on before the table is even touched.
The trigger becomes:
CREATE TRIGGER halfcolumn_update BEFORE UPDATE ON my_table
FOR EACH ROW BEGIN
SET NEW.calculated = NEW.value/2;
END;
Note also that the BEGIN...END; syntax has to be parsed with a different delimiter in effect. The whole shebang becomes:
DELIMITER |
CREATE TRIGGER halfcolumn_insert BEFORE INSERT ON my_table
FOR EACH ROW BEGIN
SET NEW.calculated = NEW.value/2;
END;
|
CREATE TRIGGER halfcolumn_update BEFORE UPDATE ON my_table
FOR EACH ROW BEGIN
SET NEW.calculated = NEW.value/2;
END;
|
DELIMITER ;
You can use generated columns from MYSQL 5.7.
Example Usage:
ALTER TABLE tbl_test
ADD COLUMN calc_val INT
GENERATED ALWAYS AS (((`column1` - 1) * 16) + `column2`) STORED;
VIRTUAL / STORED
Virtual: calculated on the fly when a record is read from a table (default)
Stored: calculated when a new record is inserted/updated within the
table
If you want to add a column to your table which is automatically updated to half of some other column, you can do that with a trigger.
But I think the already proposed answer are a better way to do this.
Dry coded trigger :
CREATE TRIGGER halfcolumn_insert AFTER INSERT ON table
FOR EACH ROW BEGIN
UPDATE table SET calculated = value / 2 WHERE id = NEW.id;
END;
CREATE TRIGGER halfcolumn_update AFTER UPDATE ON table
FOR EACH ROW BEGIN
UPDATE table SET calculated = value / 2 WHERE id = NEW.id;
END;
I don't think you can make only one trigger, since the event we must respond to are different.
I hope this still helps someone as many people might get to this article. If you need a computed column, why not just expose your desired columns in a view ? Don't just save data or overload the performance with triggers... simply expose the data you need already formatted/calculated in a view.
Hope this helps...

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

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 thing: a MySQL row can be only 65535 bytes (not counting BLOB/TEXT columns). If you try to change a column to be too large, making the total size of the row 65536 or greater, you may get an error. If you try to declare a column of VARCHAR(65536) then it's too large even if it's the only column in that table, so MySQL automatically converts it to a MEDIUMTEXT data type.
mysql> create table foo (str varchar(300));
mysql> alter table foo modify str varchar(65536);
mysql> show create table foo;
CREATE TABLE `foo` (
`str` mediumtext
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
I misread your original question, you want VARCHAR(65353), which MySQL can do, as long as that column size summed with the other columns in the table doesn't exceed 65535.
mysql> create table foo (str1 varchar(300), str2 varchar(300));
mysql> alter table foo modify str2 varchar(65353);
ERROR 1118 (42000): Row size too large.
The maximum row size for the used table type, not counting BLOBs, is 65535.
You have to change some columns to TEXT or BLOBs
ALTER TABLE {table_name} MODIFY [COLUMN] {column_name} {column_type} {defaults and/or not-null};
(Including COLUMN is optional.)
Note: if your column was created with NOT NULL etc. you may need to specify those in the MODIFY statement to avoid losing them.