How would you add a column that only has a set choice of values? - sql

If I wanted to add a column that could only be two values, for example, a column called 'Gender' with the two possible options of 'Male' or 'Female'

use CHECK
Gender VARCHAR(6) NOT NULL CHECK (Gender IN ('Male', 'Female'))
SQLFiddle Demo

I would probably want to use a bit field type, and not allow nulls, so that value can only be 0 or 1 (true/false).

create table abc (
Gender Varchar(10) check (gender in ('Female','Male'))

Related

How to allow a gender field to accept only two values?

The question: Add a gender field. Ensure the field will only
accept ‘M’ or ‘F’ and the default value should be
‘F’
PostgresSQL code:
alter table Patient
add Gender varchar(1) default 'F' ,Check (Gender = 'M' or Gender = 'F');
ERROR: syntax error at or near "Check" LINE 2: add Gender
varchar(1) default 'F' ,Check (Gender = 'M' or G...
How do i fix it?
Try Below
CREATE TYPE gender AS ENUM ('F', 'M');
CREATE TABLE t (
g gender default 'F' -- <==== default value
);
Your approach is good, there is only a small syntax error:
alter table Patient
add Gender varchar(1) default 'F',
add Check (Gender = 'M' or Gender = 'F');
The second add was missing. Using IN would be typographically shorter.

How to make the default selection of a value in sql out of multiple choices?

I have a column in my SQL table i.e. Gender and there can be one of two possible values for it, 'M' and 'F'.
For those values, I am able to pass two values by using a check constraint as option when creating the table:
Gender varchar(6) CHECK (Gender IN ('M', 'F'))
Also, one of those value is defined as the default:
Gender varchar(6) DEFAULT 'M'
But here, if I am trying to merge those two queries while table creation, I am not getting the output. I want to pass two choices for column value and default as 'M'.
Both can be used as part of the create table syntax:
create table t(gender char(1) default('M') check(gender in ('M','F')));
Demo Fiddle
You can easily use both in the CREATE TABLE statement - and preferably, define explicit names for your constraints!
CREATE TABLE Person
(
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Gender CHAR(1) NOT NULL
CONSTRAINT CHK_Person_Gender CHECK (Gender IN ('M', 'F'))
CONSTRAINT DF_Person_Gender DEFAULT ('M')
)

SQL Multiple constraints on a single column

I am working with sql server and the table of workers.
I need to add a column "Gender" and apply two constraints : check and default.
So i need to check that they gender is male/female and the default value should be "To be updated"
My query looks like that by it doesn't work:
ALTER TABLE Workers
ADD Gender VARCHAR(6) CHECK (Gender IN ('Male', 'Female'))
DEFAULT 'To be updated'
You don't say what "doesn't work" means, however varchar(6) doesn't accomodate the 13 characters of 'To be updated', and you need to include your default value as an allowable value. It's also a good idea to specifically name the constraints which enables you to easily reference them in future.
alter table Workers add
Gender varchar(13)
constraint GenderCheck check (Gender in ('Male', 'Female','To be updated') )
constraint GenderDefault default ('To be updated');
Working Demo
Also note - specifying not null when adding the column will apply the default to existing rows.

SQL parameters for upper case single letters

Title seems confusing but the question is simple:
I made a column in TblPersonel called sex, this column must only allow one letter: M, W, or null (man, woman or null) but it can only be uppercase and only allowed to be that or null.
What I have is this:
ALTER TABLE TblPersonel
ADD Sex varchar(1);
ALTER TABLE TblPersonel
ADD CONSTRAINT ch CHECK(Sex COLLATE Latin1_General_BIN2 IN ('M', 'W') )
You can try this following:
ALTER TABLE TblPersonel
ADD CONSTRAINT CK_TblPersonel_Sex CHECK(Sex IN ('M', 'W'))

SQL SVR 08 R2 Limit a field to specific values

For example, say I have field 'Gender' and I want to allow only the values Male, Female or Unisex. Is this possible in the SQL server management studio?
Update: So far the solution is pointing towards the 'Check constraints' function which can be found by right-clicking on the desired column.
Still, I'm getting syntax errors. Any thoughts on how I should type up the constraint?
Answer: Turns out I just needed to type it likes this
Gender = 'Male' OR Gender = 'Female' OR Gender = 'Unisex'
Unlike MySQL, MSSQL doesn't have ENUM data type. The only want you can do this is to trap the value in the application level. Or by using CHECK constraint
ColumnName varchar(10)
CHECK(ColumnName IN ('Male', 'Female', 'Unisex'))
DEFAULT 'Unisex'
You could do the following:
- Create another table and create a one to many relationship from the primary table to the Gender.
- Create an constraint like the following one:
CREATE TABLE Person
(
Id int NOT NULL,
Gender varchar(200)
CONSTRAINT chk_Gender CHECK (Gender IN ('Male', 'Unisex', 'Female'))
)