Adding a column with unique values in AWS Athena - mysql

So, I am looking for a way to sequence the rows of my athena table. I have already tried:
ROW_NUMBER() OVER ()
But then this leads to Query exhausted resources at this scale factor error. It has to be a unique value for every row, so yes it doesn't need to be an integer. The method should be performance effective if it can be.

Please provide SHOW CREATE TABLE for the table in question.
If you don't already have an AUTO_INCREMENT column on the table, consider the following.
ALTER TABLE t
ADD COLUMN id INT UNSIGNED AUTO_INCREMENT NOT NULL,
ADD INDEX(id);
If you don't already have a PRIMARY KEY on the table, do this instead:
ALTER TABLE t
ADD COLUMN id INT UNSIGNED AUTO_INCREMENT NOT NULL,
ADD PRIMARY KEY(id);

You can give each row a unique id, uuid, using the uuid() function. Here are the docs:
https://trino.io/docs/current/functions/uuid.html

Related

How to create sequence for a number that starts at 100 and increment by 5 in MySQL?

I need that the values for a ColumnID, which is a Primary Key, to start at 100 and increment by 5. This condition is asked to be included as a constraint before populating the tables. I already created the tables, I just need to add that constraint. I know I can't use AUTO_INCREMENT because the increase is only by 1. Is there a way to do it in MySQL?
MySQL does not provide any built-in function to create a sequence for a table's rows or columns. But we can generate it via SQL query.
Example:
Let us understand it with the help of the following example. First, we need to create a new table and make sure that there is one column with the AUTO_INCREMENT attribute and that too, as PRIMARY KEY.
Execute the below query to create a table:
CREATE TABLE Insects (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
Origin VARCHAR(30) NOT NULL
);
Then you can alter your column to start from another value:
ALTER TABLE Insects AUTO_INCREMENT=100;
You can check it here.

external GUID to satisfy unique constraint in mysql

I have a MYSQL 8.x table, each row is unique since the PK is auto incremented. A column transactionID is used to store an external transactionID that needs to be coupled with the row/record.
However a new requirement came and we want to create a new row with the same transactionID. I was thinking to add a new column that holds the GUID of the transaction.
Is this good idea? Is this going to be slow? Are there any second thougths regarding uuid as strings? MySQL 8.0 added UUID_TO_BIN and BIN_TO_UUID function that could store UUID as number.
CREATE TABLE `testme`.`new_table` (
`ID` INT NOT NULL AUTO_INCREMENT,
`transactionID` INT NOT NULL,
`maybe_uuid` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ID`));
ALTER TABLE `testme`.`new_table`
ADD UNIQUE INDEX `index2` (`transactionID` ASC, `maybe_uuid` ASC) VISIBLE;
;
GUIDs are not great for use as a primary key for many reasons that i will not detail here and it would faster to use you auto_increment key that is already in place. But if you also need to store the GUID it is perfectly reasonable to just add another column for that. And you can use the two functions you mentioned.

MySQL auto increment based on field?

Is it possible to create auto-increment based on a specific field? For example i have UserId and Status fields, so for each row with same UserId i need to auto-increment its Status, not global.
There is three thing that come to mind when I read your question. One was an auto incrementing field which acts as your ID number. Updating a table with data that has no unique ID number. Searching for fields with the same Userid to Status
Mt First example is of a creating a table and your AUTO_INCREMENTing number ID:
CREATE TABLE tableNameHere
(
UniqueID int NOT NULL AUTO_INCREMENT,
FirstName varchar(255) NOT NULL,
StatusOrYourColumn int(100) NOT NULL,
PRIMARY KEY (UniqueUD)
)
More on auto incrementation.
You may have already built your table and now want to 'add' additional and or modify your fields using ALTER:
ALTER TABLE tableNameHere StatusOrYourColumn INT AUTO_INCREMENT PRIMARY KEY
But be careful, you don't want to overwrite your settings that you have already set.
Another Thing that came to my mind when reading was where you said Status and Userid where the same. You can find these using the WHERE clause like so:
SELECT * FROM tableName WHERE tableName.Userid = anotherTableOrTableName.Status
Using these queries you can update, remake, alter and query your database table.

MySQL populate new column with incrementing values without auto_increment

I see lots of almost similar questions with obvious answers but I'm fairly sure this question isn't already on here.
I need to add an auto-incrementing id column to an existing table and set it as the primary key. I can't lose any of the existing data.
I can successfully make the change to the table structure but I get an error about truncated data in the new column. When I view the data every value in the new auto-incrementing column is null (and therefore not unique).
How can I back-fill these values to ensure uniqueness in my primary key?
***I would prefer to avoid dumping the existing data to a temporary table and re-inserting if there is a simpler solution.
Current script:
alter table the_table add new_field int first;
alter table the_table drop primary key, add primary key (new_field);
alter table the_table change new_field new_field int unsigned not null auto_increment;
I run the script in this order as I can't have an auto-incrementing column that isn't the primary key.
(MySQL 5.3)
Try creating the column, setting it as primary key and auto increment in one go
ALTER TABLE `the_table` ADD `new_field` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
Just add an auto-increment field, without being a primary key:
ALTER TABLE `the_table` ADD `new_field` INT NOT NULL AUTO_INCREMENT

MySQL - How to create an auto increement field in the DB?

I am entering records in the MySQL DB. Now I want to have a "Serial_Number" field that increements automatically whenever a record is entered into the DB.
I don't want this "Serial_Number" field to be the primary key of the DB.
How can I create this field (with the attributes needed to be set).
I am using "SQL YOG" to access MySQL. If you are aware of the SQL YOG then tell me how to do that through SQL YOG.
The AUTO_INCREMENT column has to have a UNIQUE KEY constraint associated to it.
For instance, this will work just fine:
CREATE TABLE AutoNotId
(
Id INTEGER PRIMARY KEY,
Auto INTEGER AUTO_INCREMENT,
UNIQUE (Auto)
);
Edit:
The ALTER statement would look somewhat like this:
ALTER TABLE AutoNotId
MODIFY COLUMN Auto INTEGER AUTO_INCREMENT,
ADD UNIQUE (Auto);
I recommended, however the use of the long-hand syntax to specify the name of the UNIQUE constraint; But you can always refer to MySQL's Reference Manual for the exact specifications.
In MySQL tables can only have one auto increment field and they must be indexed.
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
Is there a reason you don't want it to be the primary key?
If you want an incrementing value, you could fudge it by running updates after each insert:
SELECT MAX(serial) + 1 FROM myTable;
UPDATE myTable SET serial = <that number> WHERE id = ...
I don't think you can have an auto increment field:
CREATE TABLE `t` (`dd` int(11) NOT NULL)
ALTER TABLE `t` CHANGE `dd` `dd` INT( 11 ) NOT NULL AUTO_INCREMENT
MySQL said: Documentation
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
You cannot do this in MySQL. From the doc:
There can be only one AUTO_INCREMENT
column per table, it must be indexed,
and it cannot have a DEFAULT value. An
AUTO_INCREMENT column works properly
only if it contains only positive
values. Inserting a negative number is
regarded as inserting a very large
positive number. This is done to avoid
precision problems when numbers “wrap”
over from positive to negative and
also to ensure that you do not
accidentally get an AUTO_INCREMENT
column that contains 0.
For MyISAM and BDB tables, you can
specify an AUTO_INCREMENT secondary
column in a multiple-column key. See
Section 3.6.9, “Using AUTO_INCREMENT”.
create table mytable (
ID INT IDENTITY(1,1) PRIMARY KEY,
SN INT IDENTITY(1,1)
)