문제

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?

도움이 되었습니까?

해결책

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top