首页 > 数据库 >mysql主键不能有null而唯一键可以有重复的null值并且都不能有重复的空值

mysql主键不能有null而唯一键可以有重复的null值并且都不能有重复的空值

时间:2024-12-25 17:34:42浏览次数:2  
标签:重复 db tbl 3306 hlf null root 主键 localhost

mysql主键不能有null而唯一键可以有重复的null值并且都不能重复的空值


root@e9be1791c2c1:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 33893
Server version: 5.7.32-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost: 08:48 [3306][(none)]>


root@localhost: 08:52 [3306][db_hlf]>help create ;
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   CREATE DATABASE
   CREATE EVENT
   CREATE FUNCTION
   CREATE FUNCTION UDF
   CREATE INDEX
   CREATE LOGFILE GROUP
   CREATE PROCEDURE
   CREATE SCHEMA
   CREATE SERVER
   CREATE TABLE
   CREATE TABLESPACE
   CREATE TRIGGER
   CREATE USER
   CREATE VIEW
   SHOW
   SHOW CREATE DATABASE
   SHOW CREATE EVENT
   SHOW CREATE FUNCTION
   SHOW CREATE PROCEDURE
   SHOW CREATE SCHEMA
   SHOW CREATE TABLE
   SHOW CREATE USER
   SPATIAL INDEXES

root@localhost: 08:52 [3306][db_hlf]>help create index;
Name: 'CREATE INDEX'
Description:
Syntax:
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (key_part,...)
    [index_option]
    [algorithm_option | lock_option] ...

key_part:
    col_name [(length)] [ASC | DESC]

index_option: {
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER parser_name
  | COMMENT 'string'
}

index_type:
    USING {BTREE | HASH}

algorithm_option:
    ALGORITHM [=] {DEFAULT | INPLACE | COPY}

lock_option:
    LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}

Normally, you create all indexes on a table at the time the table
itself is created with CREATE TABLE. See [HELP CREATE TABLE]. This
guideline is especially important for InnoDB tables, where the primary
key determines the physical layout of rows in the data file. CREATE
INDEX enables you to add indexes to existing tables.

CREATE INDEX is mapped to an ALTER TABLE statement to create indexes.
See [HELP ALTER TABLE]. CREATE INDEX cannot be used to create a PRIMARY
KEY; use ALTER TABLE instead. For more information about indexes, see
https://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html.

URL: https://dev.mysql.com/doc/refman/5.7/en/create-index.html


root@localhost: 08:53 [3306][db_hlf]>

 


root@localhost: 08:50 [3306][(none)]>create database db_hlf;
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:50 [3306][(none)]>use db_hlf;
Database changed
root@localhost: 08:51 [3306][db_hlf]>

root@localhost: 09:02 [3306][db_hlf]>create table tbl_pri(name varchar(30) primary key, address varchar(50));
Query OK, 0 rows affected (0.02 sec)

root@localhost: 09:02 [3306][db_hlf]>show create table tbl_pri\G
*************************** 1. row ***************************
       Table: tbl_pri
Create Table: CREATE TABLE `tbl_pri` (
  `name` varchar(30) NOT NULL,
  `address` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

root@localhost: 09:02 [3306][db_hlf]>

root@localhost: 09:05 [3306][db_hlf]>insert into tbl_pri(address) values('chengdu');
ERROR 1364 (HY000): Field 'name' doesn't have a default value
root@localhost: 09:05 [3306][db_hlf]>insert into tbl_pri(name,address) values('xiaohong','chengdu');
Query OK, 1 row affected (0.01 sec)

root@localhost: 09:06 [3306][db_hlf]>insert into tbl_pri(name,address) values('xiaohong','beijing');
ERROR 1062 (23000): Duplicate entry 'xiaohong' for key 'PRIMARY'
root@localhost: 09:06 [3306][db_hlf]>insert into tbl_pri(name,address) values('','guiyang');
Query OK, 1 row affected (0.01 sec)

root@localhost: 09:06 [3306][db_hlf]>insert into tbl_pri(name,address) values('','guiyang');
ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'
root@localhost: 09:06 [3306][db_hlf]>select * from tbl_pri;
+----------+---------+
| name     | address |
+----------+---------+
|          | guiyang |
| xiaohong | chengdu |
+----------+---------+
2 rows in set (0.00 sec)

root@localhost: 09:06 [3306][db_hlf]>

 

root@localhost: 08:50 [3306][db_hlf]>create table tbl_unq(id int(11) primary key,name varchar(30),address varchar(50));
Query OK, 0 rows affected (0.02 sec)

root@localhost: 08:53 [3306][db_hlf]>create unique index unq_name on tbl_unq(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

root@localhost: 08:54 [3306][db_hlf]>alter table tbl_unq add index idx_address(address);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

root@localhost: 08:54 [3306][db_hlf]>show create table tbl_unq\G
*************************** 1. row ***************************
       Table: tbl_unq
Create Table: CREATE TABLE `tbl_unq` (
  `id` int(11) NOT NULL,
  `name` varchar(30) DEFAULT NULL,
  `address` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unq_name` (`name`),
  KEY `idx_address` (`address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

root@localhost: 08:54 [3306][db_hlf]>insert into tbl_unq values(1);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
root@localhost: 08:55 [3306][db_hlf]>insert into tbl_unq(id) values(1);
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:55 [3306][db_hlf]>insert into tbl_unq(id) values(2);
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(3,'zhangsan');
Query OK, 1 row affected (0.00 sec)

root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(3,'');
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(4,'');
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(4,'');
ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'
root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(5,'');
ERROR 1062 (23000): Duplicate entry '' for key 'unq_name'
root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(5,'mazi');
Query OK, 1 row affected (0.00 sec)

root@localhost: 08:56 [3306][db_hlf]>insert into tbl_unq(id,name) values(6,'lisi','chengdu');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
root@localhost: 08:57 [3306][db_hlf]>insert into tbl_unq(id,name,address) values(6,'lisi','chengdu');
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:57 [3306][db_hlf]>insert into tbl_unq values(7,'','jiangyang');
ERROR 1062 (23000): Duplicate entry '' for key 'unq_name'
root@localhost: 08:57 [3306][db_hlf]>insert into tbl_unq values(7,'wanger','');
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:57 [3306][db_hlf]>insert into tbl_unq values(8,'hlf','');
Query OK, 1 row affected (0.01 sec)

root@localhost: 08:58 [3306][db_hlf]>select * from tbl_unq;
+----+----------+---------+
| id | name     | address |
+----+----------+---------+
|  1 | NULL     | NULL    |
|  2 | NULL     | NULL    |
|  3 | zhangsan | NULL    |
|  4 |          | NULL    |
|  5 | mazi     | NULL    |
|  6 | lisi     | chengdu |
|  7 | wanger   |         |
|  8 | hlf      |         |
+----+----------+---------+
8 rows in set (0.00 sec)

root@localhost: 08:58 [3306][db_hlf]>

标签:重复,db,tbl,3306,hlf,null,root,主键,localhost
From: https://www.cnblogs.com/hsjz-xinyuan/p/18631001

相关文章

  • 写一个方法去除对象中值为空('',' ',null,undefined)的属性
    在前端开发中,你可以使用JavaScript来编写一个方法,该方法会遍历对象的属性并删除值为空(''、''、null、undefined)的属性。以下是一个示例实现:functionremoveEmptyProperties(obj){//创建一个新的对象,以避免直接修改原始对象constcleanedObj={...obj};//遍历对......
  • 处理MYSQL 插入数据时主键相同的场景:新增?替换?忽略?
    在MySQL中,如果你尝试插入一条记录,其主键(或唯一约束字段)与已有记录的主键相同,会根据不同的操作方式产生不同的结果。以下是常见情况及其处理方式:1.使用INSERT语句如果你使用标准的INSERT语句并尝试插入一条记录,但其主键与已存在的记录冲突,会出现错误,通常是类似以下的错误......
  • ORACLE sql查询用户/表/数据/外键/主键/索引
    查询用户数量--查询用户数量SELECTCOUNT(*)ASuser_countFROMDBA_USERS;查询表数量--查询表数量SELECTOWNER,COUNT(*)AStable_countFROMDBA_TABLESWHEREOWNERIN('RBCC_ICO_ADMIN','DPDB_CONFIDENTIAL','DPDB_INTERNAL')GROUPBYOWNER;......
  • SM5604勾选的主键和实际主键可能不一致
    这是一个bug,看到勾选中是第一行,实际对应的主键可能是第2行。所见不是所得。勾选的主键与实际的主键不一样。如果不一样,就要把实际的主键设置成一样。$('#btnSCHC,#btnMXCF').click(function(){var$checked=$("td[colname='MC']input[type='checkbox']:checked")......
  • Java:为什么容器接口中定义的clear()方法具体实现要遍历每个元素并将其设置为null,而不
    以ArrayList为例,其clear()的具体实现为遍历每一个元素,并将其设置为null。publicvoidclear(){modCount++;finalObject[]es=elementData;for(intto=size,i=size=0;i<to;i++)es[i]=null;}笔者作为初学者,很难不产生疑惑,为什么不将s......
  • 写一个方法统计给定数组中重复元素的次数
    在前端开发中,你可以使用JavaScript来编写一个方法,用于统计给定数组中重复元素的次数。以下是一个简单的示例:functioncountDuplicates(arr){//创建一个空对象,用于存储元素及其出现的次数constelementCount={};//遍历数组,统计每个元素出现的次数arr.forEach((e......
  • Unity中检查重复的资源
    目的:检查Unity中资源是否重复,例如有两张贴图,明明是一张,却被复制为两份放在工程中,名字或者所在目录位置不同,这对于资源管理来说是很浪费的。因此需要写一个检查工具来检查项目中是否存在重复的资源。思路:Unity引擎内部对资源的管理是否有一个唯一标识,通过这个唯一标识来检查是......
  • 只谈C++11新特性 - nullptr
    nullptr在C++11标准中,引入了nullptr关键字,用来表示空指针。这是对原有NULL的重要改进,提供了更强的类型安全性,并避免了一些常见的编程错误。1.nullptr的特点nullptr是一种新类型std::nullptr_t的常量,用于表示空指针。与NULL不同,nullptr具有明确的指针类型......
  • 【实用技巧】MD5 的重复请求应用以及性能测试
    1 前言大家可能都遇到幂等或者娇艳重复提交的问题,比如重复的JSON请求、文件上传相同的文件重复上传了两次等,当然了确实存在某种情况比如人家用户一会上传了一个文件隔了一会儿又上传一个同样的文件是合理的,我们要做的就是短时间内的重复提交。这个时候我们可以通过计算请求......
  • 459_重复的子字符串
    Leetcode459重复的子字符串问题描述给定一个非空的字符串s,检查是否可以通过由它的一个子串重复多次构成。1.示例1:-输入:s="abab"-输出:true-解释:可由子串"ab"重复两次构成。2.示例2:-输入:s="aba"-输出:false3.示例3:-输入:......