I am learning about sql databases. I am using mysql.
我正在学习sql数据库。我正在使用mysql。
I have designed the tables, and queries. I am now working on the code to put stuff into the database.
我设计了表格和查询。我现在正在研究将内容放入数据库的代码。
I can not work out how to ensure that a record is unique when I have a text field. I tried to mark the part of the record that was not the pk (primary key) as unique, but when it is text it complains that it is not fixed length. I then played with the idea of conditionals in a stored procedure, but could not get it to work.
当我有一个文本字段时,我无法确定如何确保记录是唯一的。我试图将不是pk(主键)的记录部分标记为唯一,但是当它是文本时,它会抱怨它不是固定长度。然后,我在存储过程中使用条件的概念,但无法使其工作。
DELIMITER $$
DROP PROCEDURE IF EXISTS `experiment1`.`add_zzzz`$$
CREATE PROCEDURE `experiment1`.`add_zzzz` (IN v INT, IN n TEXT)
BEGIN
IF EXISTS (
SELECT value, name
FROM zzzz
WHERE value=v AND name=n
)
THEN
ELSE
INSERT INTO zzzz(value,name)
VALUES v,n;
END IF;
END$$
DELIMITER ;
So anyone know what I am doing wrong?
所以任何人都知道我做错了什么?
1 个解决方案
#1
0
VARCHAR is not fixed and you can use unique index with it
VARCHAR未修复,您可以使用唯一索引
more on http://www.w3schools.com/sql/sql_unique.asp
更多信息,请访问http://www.w3schools.com/sql/sql_unique.asp
you can also use INSERT ignore
你也可以使用INSERT ignore
#1
0
VARCHAR is not fixed and you can use unique index with it
VARCHAR未修复,您可以使用唯一索引
more on http://www.w3schools.com/sql/sql_unique.asp
更多信息,请访问http://www.w3schools.com/sql/sql_unique.asp
you can also use INSERT ignore
你也可以使用INSERT ignore