S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

MySQL - Create table Column with pre-defined insertacle value

I want to create a table which would have a column named Role where only 3 value can be inserted, "Admin","User","Customer".

So, what I have done is this-

CREATE TABLE Person
(
    PersonID int,
    Name varchar(255),
    Role varchar(10) IN ("Admin","User","Customer"),
    Address varchar(255),
    City varchar(255)
)

But it is giving me a error like it-

enter image description here

Can anyone please help?

Thanks in advance for helpipng :)

Upvotes: 1

Views: 917

Answers (2)

eros harianto nugroho
eros harianto nugroho

Reputation: 62

you must insert separately

CREATE TABLE Person
(
PersonID int,
Name varchar(255),
Role varchar(10),
Address varchar(255),
City varchar(255)
)

SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.

The first form does not specify the column names where the data will be inserted, only their values:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Upvotes: 0

Luiz Paulo Camargo
Luiz Paulo Camargo

Reputation: 194

You can use enum datatype, as following: Check this url: enum mysql

CREATE TABLE Person
    (
        PersonID int,
        Name varchar(255),
        Role enum("Admin","User","Customer"),
        Address varchar(255),
        City varchar(255)
    )

Upvotes: 1

Related Questions