如何将布尔数据类型列添加到 sql 中的现有表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40244152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:58:04  来源:igfitidea点击:

How to add a boolean datatype column to an existing table in sql?

sqlsql-serveralter-table

提问by Mike

I have a table called personin my database. I want to add another column to the same table and it's a Boolean datatype column. I have tried following queries but it says syntax errornear default. I know this is a common and there are lot of answers. I have tried many of them and couldn't figure out to make it work. So please help me.

我的数据库中有一个名为person的表。我想在同一个表中添加另一列,它是一个 Boolean 数据类型 column。我试过下面的查询,但它说接近默认的语法错误。我知道这是一个常见问题,并且有很多答案。我已经尝试了很多,但无法弄清楚如何让它工作。所以请帮助我。

queries I have tried

我试过的查询

ALTER TABLE person add column "AdminApproved" BOOLEAN SET default FALSE;
ALTER TABLE person alter column "AdminApproved" BOOLEAN SET default FALSE;         

I have tried without SETkey word too.

我也试过没有SET关键字。

回答by P?????

In SQL SERVER it is BIT, though it allows NULLto be stored

在 SQL SERVER 中它是BIT,虽然它允许NULL存储

ALTER TABLE person add  [AdminApproved] BIT default 'FALSE';

Also there are other mistakes in your query

您的查询中还有其他错误

  1. When you alter a table to add column no need to mention columnkeyword in alterstatement

  2. For adding default constraint no need to use SETkeyword

  3. Default value for a BITcolumn can be ('TRUE' or '1')/ ('FALSE' or 0). TRUEor FALSEneeds to mentioned as stringnot as Identifier

  1. 当您更改表以添加列时,无需columnalter语句中提及关键字

  2. 添加默认约束无需使用SET关键字

  3. BIT列的默认值可以是('TRUE' or '1')/ ('FALSE' or 0)TRUEFALSE需要提及string不作为标识符

回答by MEC

The answer given by P?????creates a nullable bool, not a bool, which may be fine for you. For example in C# it would create: bool? AdminApprovednot bool AdminApproved.

P给出的答案????创建一个可为空的 bool,而不是 bool,这对您来说可能没问题。例如在 C# 中它会创建:bool? AdminApprovednot bool AdminApproved.

If you need to create a bool (defaulting to false):

如果您需要创建一个 bool(默认为 false):

    ALTER TABLE person
    ADD AdminApproved BIT
    DEFAULT 0 NOT NULL;

回答by Zafar Faheem

In phpmyadmin, If you need to add a boolean datatype column to an existing table with default value true:

在 phpmyadmin 中,如果您需要将布尔数据类型列添加到默认值为 true 的现有表中:

ALTER TABLE books
   isAvailable boolean default true;