简体   繁体   中英

SQL insert field, with value of other auto increment field + value

It might sound complicated, but it's not. I have a table called "orders", with fields:

id INT(11) auto_increment,
realid INT(14)

Now, I want at every insert into this table, do something like:

INSERT INTO orders VALUES (null, id+1000);

However I'll be doing it on shop which is currently online, and I want to change everything in 5 minutes. Will something like this work? If not, how do I do that?

I would think a simpler solution would be a calculated column presuming your DBMS supports it (This is using SQL Server syntax, although the MySql syntax should be nearly identical except that you would use AUTO_INCREMENT instead of Identity(1,1) ):

Create Table Foo    (
                    Id int not null identity(1,1)
                    , Name varchar(50)
                    , Bar As Id + 1000 
                    )

Of course, you could also just do it in the presentation or business tier instead of the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM