简体   繁体   English

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

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

I have a table called person in my database.我的数据库中有一个名为person的表。 I want to add another column to the same table and it's a Boolean datatype column .我想在同一个表中添加另一列,它是一个 Boolean 数据类型的列 I have tried following queries but it says syntax error near 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.所以请帮助我。

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 SET key word too.我也尝试过不使用SET关键字。

In SQL SERVER it is BIT , though it allows NULL to 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 column keyword in alter statement当您更改表以添加列时,无需在alter语句中提及column关键字

  2. For adding default constraint no need to use SET keyword添加默认约束不需要使用SET关键字

  3. Default value for a BIT column can be ('TRUE' or '1') / ('FALSE' or 0) . BIT列的默认值可以是('TRUE' or '1') / ('FALSE' or 0) TRUE or FALSE needs to mentioned as string not as Identifier TRUEFALSE需要作为string而不是作为标识符提及

The answer given by Pரதீப் creates a nullable bool, not a bool, which may be fine for you. Pரதீப்给出的答案创建了一个可为空的布尔值,而不是一个布尔值,这对您来说可能没问题。 For example in C# it would create: bool? AdminApproved例如在 C# 中它会创建: bool? AdminApproved bool? AdminApproved not bool AdminApproved . bool? AdminApproved不是bool AdminApproved

If you need to create a bool (defaulting to false):如果您需要创建一个 bool(默认为 false):

    ALTER TABLE person
    ADD AdminApproved BIT
    DEFAULT 0 NOT NULL;

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;

下面的查询对我有用,默认值为 false;

ALTER TABLE cti_contract_account ADD ready_to_audit BIT DEFAULT 0 NOT NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM