Using foreign key as an enum type : ENUM « Data Type « SQL / MySQL






Using foreign key as an enum type

   
mysql>
mysql> CREATE TABLE SEXES (SEX CHAR(1) NOT NULL PRIMARY KEY)
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO SEXES VALUES ('M'),('F')
    -> ;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql>
mysql> CREATE TABLE EmployeeS_SMALL2
    ->       (EmployeeNO     INTEGER NOT NULL PRIMARY KEY,
    ->        NAME         CHAR(15) NOT NULL,
    ->        INITIALS     CHAR(3) NOT NULL,
    ->        BIRTH_DATE   DATE,
    ->        SEX          CHAR(1),
    ->        FOREIGN KEY (SEX) REFERENCES SEXES (SEX));
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> drop table sexes;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql> drop table Employees_small2;
Query OK, 0 rows affected (0.00 sec)

   
    
    
  








Related examples in the same category

1.How to use enum
2.Use two ENUM data type in a table
3. Storage Comparisons of ENUM and SET
4.Insert String value as ENUM
5.Define and use ENUM data type
6.Define 'Male' and 'Female' as Enum
7.ENUM: Enumeration of up to 65,535 strings
8.With ENUM you can manage a list of up to 65,535 character strings.
9.Sorting ENUM Values
10.MySQL uses the internal numeric values for sorting for enum value
11.Sort ENUM values in lexical order
12.Consider changing the column type to ENUM, if you always sort a non-enumeration column in a specific non-lexic
13.Use ALTER TABLE to convert the name column to an ENUM that lists the colors in the desired sort order:
14.Add to mytbl an ENUM column e that has several members
15.Enumerating a Many-to-Many Relationship
16.Configure an ENUM column and a SET column
17.If you add an invalid value to ENUM columns, an empty string ("") is inserted instead
18.Perform queries on enumerated fields based on their indexes
19.Sets work in a similar way to enumerated fields
20.The ENUM and SET Column Types
21.Enumeration values aren't limited to being single letters or uppercase.
22.An ENUM column definition may list up to 65,535 members.
23.Insert value to enum type column
24.The SET datatype, like ENUM, is declared using a comma-separated list of quoted strings that define its valid
25.enum value index
26.Enum for month names
27.Enum type column
28.Order by enum value
29.Use operators to specify a range of values.