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?
1 Answer 1
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')
Explore related questions
See similar questions with these tags.