set default values in mysql Id column? - mysql

I want to start my mysql table id as todaysdate+000001 like 20120602000001 OR 20120602/000001 and that when next entry made then ID should be 20120602000002 OR 20120602/000002 (means it should get auto_incremented also).
HOw can we do this?
my table structure is
table abc(id int auto_increment,name varchar(50),company varchar(50),contact varchar(10),persontomeet varchar(30),intime TIMESTAMP CURRENT_TIMESTAMP,primary key(id));
but id starts with 1 obiviously..
I want to start it as 20120602000001 and when next time INSERT triger will trigger then it should get set to 20120602000002
please help me...

Use:
ALTER TABLE tableName AUTO_INCREMENT = 20120602000001

Related

Two autoincrements columns or autoincrement and same value in other column

I need two columns in table that would have same value on insert. Is there any way to do it from database side?
So you want to let one column use the auto_increment feature, but make another column in the same table also have the same value?
I can't think of a reason you would need this feature. Perhaps you could explain what you're trying to accomplish, and I can suggest a different solution?
A trigger won't work for this. It's a chicken-and-egg problem:
You can't change any column's value in an AFTER trigger.
But the auto-increment value isn't set yet when a BEFORE trigger executes.
It also won't work to use a MySQL 5.7 GENERATED column:
CREATE TABLE MyTable (
id INT AUTO_INCREMENT PRIMARY KEY,
why_would_you_want_this INT GENERATED ALWAYS AS (id)
);
ERROR 3109 (HY000): Generated column 'why_would_you_want_this'
cannot refer to auto-increment column.
You can't do it in a single SQL statement. You have to INSERT the row, and then immediately do an UPDATE to set your second column to the same value.
CREATE TABLE MyTable (
id INT AUTO_INCREMENT PRIMARY KEY,
why_would_you_want_this INT
);
INSERT INTO MyTable () VALUES ();
UPDATE MyTable SET why_would_you_want_this = LAST_INSERT_ID()
WHERE id = LAST_INSERT_ID();
You could alternatively generate the ID value using some other mechanism besides AUTO_INCREMENT (for example a Memcached incrementing key). Then you could insert the new value in both columns:
INSERT INTO MyTable (id, why_would_you_want_this) VALUES ($gen_id, $gen_id);
Define a before or after insert trigger and assign the value of the 2nd field in the trigger.
If the 1st field is an auto increment column, then you need to use an after insert trigger. If your application assigns value to the 1st field, then you can use a before insert trigger.
However, I would no necessarily duplicate the value on insert. You can leave the 2nd field as null on insert, which would mean that its value is the same as the 1st field's. The only drawback of this approach is that it may be more difficult to create joins on the 2nd field.
You can do this in one query by using the primary key (assumed to be id) and setting your column (assumed to be columnName):
"INSERT INTO tableName SET `columnName` = (SELECT MAX(x.id) FROM tableName x)+1"
This will not work if you have deleted the most recent primary key row however. To get past this, you can insert into the id as well:
"INSERT INTO tableName SET `columnName` = (SELECT MAX(x.id) FROM tableName x)+1, `id`= (SELECT MAX(x.id) FROM tableName x)+1"
However, this solution has the downside (or upside depending on the case) of reusing primary key values that have already been deleted.
suggested way:
To use the actual auto_increment value, you can do this:
"INSERT INTO tableName SET `columnName` = (SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table_name')"
Sources that helped me solve this: Prashant Pimpale's answer

How can I reinsert every row in a table in SQL Server 2008?

I added a new identity column, and I need to update it so that the id column goes into the identity column, then delete the old id column and rename the identity column, because you cannot change a column to an identity.
I don't know how to do this, I guess I want to loop through each row, insert it into the same table, setting the identity field to match the id field (using IDENTITY_INSERT) but I am not familiar with loops in SQL. Any ideas?
You could do it with temp table something like this:
Sample table and data
create table xx
(
OldID int
)
insert into xx values(10),(20),(30)
Add NewID as identity and get the value from OldID.
create table xxTmp
(
OldID int,
[NewID] int identity
)
set identity_insert xxTmp on
insert into xxTmp(OldID, [NewID])
select OldID, OldID
from xx
set identity_insert xxTmp off
drop table xx
exec sp_rename 'xxTmp', 'xx'

How to update existing AUTO NUMBER Values in MYSQL

I have created a MYSQL table with autoincrement id, and inserted appx 10K values, so now id starts from 1 onwards.
Now I would like to change my existing ID from 10001 onwards.. i.e. my existing id 1 should starts from 10001..next 10002...
So how it possible without affect other data's
Thanks,
Laxmilal Menaria
Try this:
UPDATE your_table
SET id = id + 10000
To "correct" old values do this:
UPDATE TABLE `tbl` SET `id` = `id` + 10000;
If you want all new autoincrementing values to start at 10001, do this (it will automatically skip numbers that are already set; so, if you already have 10001, it will start with 10002):
ALTER TABLE `tbl` AUTO_INCREMENT = 10000;

How to delete rows in MySQL, but force the numbering of all next rows to the number of my last id?

I have a table with 1000 rows.
I need to delete rows from 5 to 1000, so the first 4 remain intact and eveythin after 4 is deleted.
However if I write something like:
DELETE FROM table_name WHERE id > 4
the numbering of the next record after INSERT is 1001, which I do not want.
How to delete rows, but force the numbering of all next rows to the number of my last id?
P.S. I can not use ALTER TABLE to drop all the table.
Thanks in advance.
Can you do this?
ALTER TABLE theTableInQuestion AUTO_INCREMENT=5
If your id - auto_increment field you need to use ALTER TABLE to change it's value...
But if it's impossible you can try not to delete rows but to set there value to NULL or "" or 0 and then just to update there value
not INSERT but UPDATE ... WHERE id = 5
With MySQL? You don't. AUTO_INCREMENT never... decrements unless you call ALTER TABLE. Have you considered use of a trigger DELETE, however?
(You may need to debug this)
delimiter |
CREATE TRIGGER testref AFTER DELETE ON theTableInQuestion
FOR EACH ROW BEGIN
IF SELECT COUNT(*) FROM test1 < 5 THEN
ALTER TABLE theTableInQuestion AUTO_INCREMENT=5
END IF;
END;
|
DBCC CHECKIDENT ( 'table_name', RESEED, new_reseed_value ) ==> Works with MS-SQL
whereas
ALTER TABLE table_name AUTO_INCREMENT = 5 ==> Works with MySQL
Think the table is in auto_increment . If you cant ALTER TABLE you need to manualy give the id in each query, something like
INSERT INTO table (id,value) VALUES ((SELECT MAX(id)+1 FROM table), value);
(see comment vis-a-vis lock table)

Reset auto increment column value in script mysql

I have two mysql tables, one needs to start its auto-increment column id with the last value of the last inserted row in the other table (plus 1).
According to mysql manual you can restart the value of an auto increment column like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;
However, this is not possible:
mysql> ALTER TABLE tb2 AUTO_INCREMENT = (SELECT MAX(id) FROM tbl1);
I need to perform something like this because I'm filling the tables using a script. Is there another way to achieve it?
I think you are trying to use the auto-increment column for something it is not well suited to. If you care about the exact values that get inserted then it shouldn't be auto-increment.
IT IS USING THIS SCRIPT
ALTER TABLE `people` AUTO_INCREMENT =1
where people is my table, or you can do this through GUI (operation tab of phpmyadmin with current table selected)
What about create a dummy record and increment?
-- insert dummy forcing id
INSERT INTO 'tb2' (ID, NAME, ...)
VALUES (SELECT MAX(id) FROM tb1, "dummy", ...);
-- automagically increment to last id + 1
ALTER TABLE `tb2' AUTO_INCREMENT = 1; -- only myISAM
-- delete dummy
DELETE FROM 'tb2' WHERE NAME="dummy";