SQL SVR 08 R2 Limit a field to specific values - sql

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'))
)

Related

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.

Swap data in two columns using single sql query

User has entered data in wrong columns.
For example, I have a table with two columns applicant name and father name. Data operator has entered father name in applicant name column and applicant name in father name column. Please suggest a way to swap the data in both columns i.e data in applicant name column should move to father name column and data in father name column should move to applicant name column. Using single sql query
It may sounds funny, But you can easily alter the table and change the column name with correct labeling.
CREATE TABLE `swap_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`x` varchar(255) DEFAULT NULL,
`y` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `swap_test` VALUES ('1', 'a', '10');
INSERT INTO `swap_test` VALUES ('2', NULL, '20');
INSERT INTO `swap_test` VALUES ('3', 'c', NULL);
Solution would be :
UPDATE swap_test SET x=(#temp:=x), x = y, y = #temp;
More info can be found here.
You can simply assign the names
update the_table
set applicant_name = father_name,
father_name = applicant_name
where ...; -- make sure to only do that for the rows that need it
The SQL standard requires that the values used on the right side are evaluated before the assignment.
This works with every modern DBMS, but not with MySQL. See dexter's answer if you need a workaround for MySQL.
Online example: https://rextester.com/RIK34525

SQL true/false or either

I have a table in SQL server that accepts either a true or false value. Is there anyway to say that it can be true or false and not a fixed value. My table is for a container that can hold hot food or cold food. I want some of the containers to be able to carry hot or cold food depending on an order.
SQL Server has Bit type. Try Something like this:
CREATE TABLE yourtablename
(
ID int PRIMARY KEY,
IsHot bit not null default 1
)
Did you try check constraint?
Example code:
CREATE TABLE dbo.Vendors
(VendorID int PRIMARY KEY, VendorName nvarchar (50),
CreditRating tinyint)
GO
ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)
This this example we're forcing column CreditRating to store values from 1 to 5. Hope this helps.

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

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'))