teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

sample

2019/12/05 00:32

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -1,3 +1,23 @@
1
1
  - 適当なカラムをつくり(その時点ではユニーク属性はつけない)
2
2
  - set @num=0;update set 該当カラム=(@num:=@num+1) order by なんちゃら、としてユニークにし
3
- - alter tableで当該カラムにuniqu属性をつける
3
+ - alter tableで当該カラムにuniqu属性をつける
4
+
5
+ # sample
6
+
7
+ - 仮データ作成
8
+ ```SQL
9
+ create table tbl(val double);
10
+ insert into tbl values(rand()),(rand()),(rand()),(rand()),(rand());
11
+ ```
12
+ - カラム追加
13
+ ```SQL
14
+ alter table tbl add id int not null first;
15
+ ```
16
+ - uniqueなidを投入
17
+ ```SQL
18
+ update tbl set id=(select @a:=@a+1 from (select @a:=0) as dummy) order by val;
19
+ ````
20
+ - unique属性追加
21
+ ```SQL
22
+ alter table tbl add unique(id);
23
+ ```