Mysql 无效地使用空值

Mysql 无效地使用空值,mysql,sql,mysql-error-1138,Mysql,Sql,Mysql Error 1138,我在使用mySQL时遇到问题 弹出此错误: 无效使用空值 我试图使表中的两个属性成为主键;这是我的密码: alter table contact_info add primary key(phone_number, contactID); 以下是我在联系人信息表中输入的alter语句: alter table contact_info add contactID varchar(10); alter table contact_info add suffixID varchar(8); a

我在使用mySQL时遇到问题

弹出此错误:

无效使用空值

我试图使表中的两个属性成为主键;这是我的密码:

alter table contact_info
add primary key(phone_number, contactID);
以下是我在联系人信息表中输入的alter语句:

alter table contact_info
add contactID varchar(10);

alter table contact_info
add suffixID varchar(8);

alter table contact_info
add titleID varchar(12);

alter table contact_info
add companyID varchar(12);

alter table contact_info
add phonetypeID char(2);

有人知道怎么了吗?提前感谢。

查找电话号码或联系人ID中有空值的联系人信息。不能添加表中现有空值的主键

select *
from contact_info
where (phone_number is null or contactID is null)
运行该SQL以查找值为null的所有记录。更新记录,然后再次尝试应用主键

我不知道你在做什么,但你可能想先备份你的数据在运行任何更新之前。以下是您可以用来设置contactID的更新:

update contact_info
set contactID = (select max(contactID) from contact_info) + 1
where contactID is null

update contact_info
set phone_number = '555-1212'
where phone_number is null
如果数据中存在重复项,则需要找到它们并更新它们。以下是如何找到重复项:

-- Assuming the phone_number is duplicate (2 people living in the same house with the same phone number)
select a.phone_number, count(1)
from contact_info a
group by a.phone_number
having count(1) > 1

运行此查询将告诉您有问题的列在哪里。不能将
NULL
值设置为主键

SELECT *
FROM contact_info
WHERE phone_number IS NULL
OR contactID IS NULL

我现在为键“primary”获取重复的条目“”。这意味着什么?主键,无论是一列还是两列,都不能有重复项。我将帮助您搜索重复项。我现在获得键“primary”的重复项“”。这是什么意思?