MySQL UNIQUE声明以避免插入重复值?
以下是MySQL中UNIQUE子句的声明-
create table yourTableName ( yourColumnName1 dataType, yourColumnName2 dataType, UNIQUE(yourColumnName1), UNIQUE(yourColumnName1) );
让我们首先创建一个表-
create table DemoTable ( Value int, Value2 int, UNIQUE(Value), UNIQUE(Value2) );
使用insert命令在表中插入一些记录。在这里,不会插入重复的记录,因为我们在上面使用了UNIQUE-
insert into DemoTable values(10,20) ; insert into DemoTable values(10,30); ERROR 1062 (23000): Duplicate entry '10' for key 'Value' insert into DemoTable values(40,20); ERROR 1062 (23000): Duplicate entry '20' for key 'Value2' insert into DemoTable values(60,70);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+-------+--------+ | Value | Value2 | +-------+--------+ | 10 | 20 | | 60 | 70 | +-------+--------+ 2 rows in set (0.00 sec)
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练