MySQL Create Table That Accept Check values That Starts With

51 views Asked by At

I created a table for my cellphone contacts and I want to sort the cellphone numbers. Example if the phone number starts with 1 to 3 it belongs to the first telecommunications company and 4 to 6 belongs to other.

What I want to do is if the user enters their phone number the number will be inserted to the table of the telecom company but each telecom company is dependent on the ContactId which is the primary key in cellphoneContacts table.

I already tried using cellnum1 but nothing works

cellnum1 ENUM('1%','2%','3%') ............. 
cellnum1 VARCHAR(11) CHECK (cellnum1 IN ('1%','2%','3%')) ......... 
cellnum1 VARCHAR(11) CHECK (cellnum1 LIKE('1%','2%','3%'))
2

There are 2 answers

3
Andreea Dumitru On
SELECT * FROM tbl_item WHERE SUBSTR([<Column name>], 1, X) = '<Your ID prefix>' ORDER BY [<Column name>]  

Where X from SUBSTR is howmany characters do you want. And please, be more specific with what you want in the future. :)

3
Kobi On

The MySQL Reference Manual says:

The CHECK clause is parsed but ignored by all storage engines.

Try a trigger...

If you are checking first character value , you can use substr on the NEW table rows to filter data :

substr(NEW.cellnum1 ,1,1) in ( 1,2,3)