1

Say I have a table with "column1" (Primary Key, Auto Increment) and "column2".

How can insert into the table with column2 will have the same values as column1 which is auto increment column?

Marcello Miorelli
17.3k53 gold badges182 silver badges324 bronze badges
asked Jul 15, 2019 at 4:39

1 Answer 1

0

You can create a table with a Calculated column that it's value is equal to column1. Each time you insert a row into your table column1 will be inserted as identity and column2 will be euqal to that.

MySql :

CREATE TABLE myTable 
 (column1 int NOT NULL AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(50) NOT NULL,
 column2 int);
INSERT INTO myTable (name) VALUES('John Doe')
UPDATE mytable SET column2 = column1;

Best ways for MySql : Calculated column or trigger that I refer you to these answers : Calculated Column MySql , Insert, then update, Create before insert trigger

You can add other columns like name.

Sql Server :

CREATE TABLE myTable 
 (column1 int identity (1,1) NOT NULL,
 name nvarchar(100),
 column2 as column1);
INSERT INTO myTable (name) VALUES('John Doe')
answered Jul 15, 2019 at 4:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.