【问题标题】:How to create an int column without primary key?如何创建没有主键的 int 列?
【发布时间】:2014-07-09 01:00:13
【问题描述】:

我在 phpmyadmin 工作,我有一个包含以下列的 sql 表:

image_id, int(11), AUTO_INCREMENT, Primary key, Unique
image_name, text
photographer, text
collection, text

当我尝试将新的int 列添加到我想调用image_order 的表中时,我总是最终得到属性:primary key and unique

因为我得到了不需要的主键,我做错了什么?我可以以某种方式删除主键以便输入重复值吗?

【问题讨论】:

    标签: mysql phpmyadmin primary-key


    【解决方案1】:

    如果你的桌子是

    create table images
    (
      image_id int(11) AUTO_INCREMENT Primary key,
      image_name text,
      photographer text,
      collection text
    );
    

    然后使用它来添加您的列

    alter table images
    add column image_order int
    

    SQLFiddle demo

    【讨论】:

      【解决方案2】:

      我没有看到这种行为。您可能在创建时没有从“索引”下拉列表中选择任何内容?你有什么 phpMyAdmin 版本?

      我从您描述的表格开始,然后转到“结构”选项卡和“在表格末尾添加 1 列”并按开始。

      在那个页面上,我输入“名称”'image_order',我不需要更改“类型”,因为默认值为 INT。无需更改任何其他内容:

      这给出了预期的结果:

      CREATE TABLE IF NOT EXISTS `stackdemo` (
      `image_id` int(11) NOT NULL,
        `image_name` text NOT NULL,
        `photographer` text NOT NULL,
        `collection` text NOT NULL,
        `image_order` int(11) NOT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
      
      ALTER TABLE `stackdemo`
       ADD PRIMARY KEY (`image_id`), ADD UNIQUE KEY `image_id` (`image_id`);
      
      ALTER TABLE `stackdemo`
      MODIFY `image_id` int(11) NOT NULL AUTO_INCREMENT;
      

      向我们展示您是如何创建附加列的,以防万一出现奇怪的情况,但它似乎对我来说很好用。

      另外,您可能不需要定义 UNIQUE PRIMARY 键; PRIMARY 可能是您在这里所需要的。有关更多详细信息,请参见例如 difference between primary key and unique key

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-23
        • 2021-01-15
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多