How to add auto_increment field to a table with huge data? - mysql

Say there's a table table_test, and there're about 10M rows data in the table. And I want to add a auto_increment field like key to do some pagination work. What is the best way to make it work?
the table stucture as below:
# table_test
+---------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------------+--------------+------+-----+---------+-------+
| ad_id | int(64) | NO | PRI | NULL | |
| account_id | varchar(64) | YES | MUL | NULL | |
| country | varchar(64) | NO | PRI | NULL | |
| image_hash | varchar(64) | YES | | NULL | |
+---------------------------------------+--------------+------+-----+---------+-------+
| table_test | CREATE TABLE `table_test` (
`ad_id` int(11) NOT NULL,
`account_id` varchar(64) DEFAULT NULL,
`country` varchar(64) NOT NULL,
`image_hash` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`,`country`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
update:
change id to ad_id, and ad_id here is unique key togather with field country, so ad_id itself may be duplicated.

I'd go:
-- drop PK as AUTO increment demands being the PK
ALTER TABLE table_test DROP PRIMARY KEY;
-- add Id as PK and the first column
ALTER TABLE table_test ADD Id INT AUTO_INCREMENT PRIMARY KEY FIRST;
-- make sure the old PK is unique
ALTER TABLE table_test ADD CONSTRAINT table_test_uk UNIQUE (ad_id, country);
or perhaps:
CREATE TABLE table_test2 LIKE table_test;
ALTER TABLE table_test2 DROP PRIMARY KEY;
-- add Id as PK and the first column
ALTER TABLE table_test2 ADD Id INT AUTO_INCREMENT PRIMARY KEY FIRST;
-- make sure the old PK is unique
ALTER TABLE table_test2 ADD CONSTRAINT table_test2_uk UNIQUE (ad_id, country);
INSERT INTO table_test2(ad_id, account_id, country, image_hash)
SELECT ad_id, account_id, country, image_hash FROM table_test;
DROP TABLE table_test;
RENAME TABLE table_test2 TO table_test;

Just Alter your table
ALTER TABLE table_test CHANGE id id INT(11) NOT NULL AUTO_INCREMENT;

You can already use the 'id' column as an auto increment (unless you are using UUID or some other strategy).
You can check the following answer on how to alter the table to add auto increment to a table:
ALTER table - adding AUTOINCREMENT in MySQL
Edit:
If you wish to add a new column that will be auto incremented , you can use the following:
ALTER TABLE table_test ADD id INT AUTO_INCREMENT;

Related

Adding a FOREIGN KEY but says the column does not exists when it does?

I have created two tables
EMPLOYEE
CREATE TABLE employee (
emp_id INT AUTO_INCREMENT NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
PRIMARY KEY (emp_id)
);
TEAM
CREATE TABLE team (
team_id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(20),
manager_id INT (20),
PRIMARY KEY (team_id)
);
I am trying to add a add a foreign key:
ALTER TABLE employee ADD FOREIGN KEY (manager_id) REFERENCES team(manager_id);
It is giving me an error telling me that the column does not exist
ERROR 1072 (42000): Key column 'manager_id' doesn't exist in table
But it does show when I Describe team
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| team_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| manager_id | int(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
Column manager_id does not exists in table employee.
Something like this would be more meaningful:
ALTER TABLE team ADD FOREIGN KEY (manager_id) REFERENCES employee(emp_id);
Possibly, you also build a table to represent the relationship between employees and teams. As of now, nothing in your schema can be used to relate an employee to one or several teams.

Can't create table with error no : 150

I am using the below create table to create a posts table in mysql :
mysql> create table posts(
-> post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> author_id INT NOT NULL,
-> title varchar(50),
-> post TEXT,
-> data DATE,
-> FOREIGN KEY (author_id)
-> REFERENCES authors(author_id)
-> ON DELETE RESTRICT)ENGINE=INNODB;
But i always get this error:
ERROR 1005 (HY000): Can't create table 'myFirstBlog.posts' (errno: 150)
The describe of my authors table is as follows:
mysql> desc authors;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| author_id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(16) | YES | | NULL | |
| password | varchar(40) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
Can someone please help me with where the error is .
Thanks in advance.
create table posts(
post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
title varchar(50),
post TEXT,
data DATE,
FOREIGN KEY (author_id)
REFERENCES authors(author_id)
ON DELETE RESTRICT
)ENGINE=INNODB;
Kindly make datatype of both table's column same.
authors(author_id) and post(author_id)
Or
Make authors table first and then execute above query.
I had tried below query it's worked for me.
create table posts(
post_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
title varchar(50),
post TEXT,
data DATE,
FOREIGN KEY (author_id)
REFERENCES labels(id)
ON DELETE RESTRICT
)ENGINE=INNODB;
Hope this will helps.

Altering existing unique constraint

I have a table that was defined like this:
CREATE TABLE `Message` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`user_id` integer NOT NULL,
`user_to` integer NOT NULL,
`top_num` integer NOT NULL,
`priority` smallint NOT NULL,
`error` varchar(120) NOT NULL,
UNIQUE (`user_id`, `user_to`, `top_num`)
);
Later, I added another column to it, msg_type, like this:
ALTER TABLE Message ADD COLUMN msg_type SMALLINT(6) NOT NULL DEFAULT 0;
However, I have come to realize that I need to change my original UNIQUE constraint to include msg_type. I tried running
ALTER TABLE Message
ADD UNIQUE INDEX (`user_id`, `user_to`, `top_num`, `msg_type`);
but INSERTs into my table still fail, and the error message indicates that that is because the old uniqueness constraint fails.
When I call describe Messages in mysql I see the following:
+-----------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| user_to | int(11) | NO | MUL | NULL | |
| top_num | int(11) | NO | MUL | NULL | |
| priority | smallint(6) | NO | | NULL | |
| error | varchar(120) | NO | | NULL | |
| msg_type | smallint(6) | NO | | 0 | |
+-----------------+----------------------+------+-----+---------+----------------+
which makes it seem like msg_type really isn't part of the constraint... How can I alter the constraint that the table was defined with, short of recreating the table?
As in previous answer to change foreign key constraint use steps:
Step 1: Drop old constraint:
ALTER TABLE `Message` DROP INDEX `user_id`;
Step 2: Add new:
ALTER TABLE `Message` ADD UNIQUE INDEX (
`user_id`,
`user_to`,
`top_num`,
`msg_type`);
Use SHOW CREATE TABLE to know name of constraint:
mysql> SHOW CREATE TABLE `Message` ;
| Message | CREATE TABLE `Message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_to` int(11) NOT NULL,
`top_num` int(11) NOT NULL,
`priority` smallint(6) NOT NULL,
`error` varchar(120) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`user_to`,`top_num`)
-- ^^^^^^^^^ name
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
If you checks:
mysql> SHOW INDEX FROM `Message`;
Key_name is user_id that is first argument in UNIQUE (user_id ....
Suppose if you write:
ALTER TABLE `Message` ADD UNIQUE INDEX (
`user_to`,
`user_id`,
`top_num`,
`msg_type`);
Then you have to drop using user_to as:
ALTER TABLE `Message` DROP INDEX `user_to`;
This is because you are adding unique index. Please first drop unique index and then add unique constraint.
-- you have to drop each index one by one.
ALTER TABLE Message DROP UNIQUE INDEX user_id;
and now add unique constraint.
ALTER TABLE Message ADD CONSTRAINT uc_message UNIQUE (`user_id`, `user_to`, `top_num`, `msg_type`);
This is because you haven't deleted the first unique constraint you have created. Right now, you have two unique constraints on your table.
To delete a unique constraint, have a look at this post
Dropping Unique constraint from MySQL table
This is because you are adding unique index. Please first drop unique index and then add unique constraint.
DROP INDEX index_name ON table_name
and now add unique constraint.
ALTER TABLE Message
ADD CONSTRAINT uc_message UNIQUE ((`user_id`, `user_to`, `top_num`, `msg_type`);)

how to set a column to be a primary key?

I have this table in mysql database
number | username | friendname | location
------------------------------------------------------------
1 | Nifa salem |jack | 47.117828 -88.545625
2 | Flora | fred | 38.898556 -77.037852
3 | Flora | Nifa salem | 32.9697 -96.80322
4 | Flora | Anne | 29.46786, -98.53506
but it says this " This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available."
Now i need to set the number column to be the priamry key column! how can o do it! as i need to edit the data in this table.
You can do this when creating the table ...
CREATE TABLE `admin` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL DEFAULT '',
`friendname` varchar(64) NOT NULL DEFAULT '',
`locatino` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Is this not as simple as
ALTER TABLE yourTableName
ADD PRIMARY KEY (number);
ALTER TABLE TableName ADD PRIMARY KEY(Number);

Unable to create Foreign Key (ERROR 1072)

I have a table which looks like this:
mysql> SHOW COLUMNS FROM Users;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| user_id | int(10) | NO | PRI | NULL | auto_increment |
| username | varchar(50) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| phone | varchar(255) | YES | | NULL | |
I am trying to create a new table like this:
create table jobs (id int, FOREIGN KEY (user_id) REFERENCES Users(user_id)) ENGINE=INNODB;
But I am getting this error:
ERROR 1072 (42000): Key column 'user_id' doesn't exist in table
I am sure I am missing something very basic.
Try this:
create table jobs (
id int,
user_id int,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
The first user_id in foreign key constraint refers to the table where the contraint is defined and the second refers to the table where it is pointing to.
So you need a field user_id in your jobs table, too.
This is the script you need:
CREATE TABLE jobs
(
id int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES Users(user_id)
)
Here's a good reference to learn the basics about setting up relationships: SQL FOREIGN KEY Constraint
You're trying to define as FOREIGN KEY, a column which is not present on your query.
That's the reason why you are receiving Key column 'user_id' doesn't exist in table
Observe your query:
create table jobs (
id int,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
As the other answers have demonstrated:
you have to define the creation of the column of your new table, which will be a FK for a PK from another table
create table jobs (
id int,
user_id int NOT NULL
FOREIGN KEY (user_id) REFERENCES Users(user_id)
) ENGINE=INNODB;
Create table attendance:
CREATE TABLE tbl_attendance
(
attendence_id INT(100) NOT NULL,
presence varchar(100) NOT NULL,
reason_id varchar(100) NULL,
PRIMARY KEY (attendance_id)
);