How to check the length of a number in oracle sql? - sql

I'm begginer on Sql.
I created a table with a field of phone:
CREATE TABLE contact (
phone NUMBER(9),
)
Now I have to set a CHECK to delimite the lenght of digits to the number var, to allow you to only enter a 9-digit number and no more or less.
I try:
ALTER TABLE contact ADD CONSTRAINT CK_phone_right check (length(phone) < 9)
But it doesn't work, because it converts number format to string.
How to do it to keep number type and check it to 9 digits?

Don't store a phone as a number. Use a string instead. If it has to be exactly 9 digits, then:
CREATE TABLE contact (
phone CHAR(9),
constraint chk_contact_phone check ( regexp_like(phone, '^[0-9]{9}$') )
)
A phone number is not really a number. A number is something you can do arithmetic on. Also, it is possible for a phone number to start with 0.

You can put phone number as varchar2 or char type and check the phone number by regular expression:
CREATE TABLE contact (
phone CHAR(9),
constraint valid_phone_number
CHECK (REGEXP_LIKE(phone, 'd{9}$'))
);

Related

Using like for numeric in a constraint

I am creating a table which is like this:
CREATE TABLE Peeps
(
Name VARCHAR(255) NOT NULL,
PhoneNum BIGINT NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
)
Every phone number has to start with 08. However when I tried insert there's an error because LIKE can't be used for numeric (or that's what my friend said). The alternative would be using VARCHAR for PhoneNum, but this is an assignment and we have to use numeric for the phone number.
If a phone number can start with a 0 then you need to use a string:
CREATE TABLE Peeps (
Name VARCHAR(255) NOT NULL,
PhoneNum VARCHAR(255) NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
);
Although you can use LIKE on a number, it is highly not recommended. What happens is that the number is converted to a string. However, that string will never start with a 0 -- well, at least never when the value is greater than 1.

SQL - Column string characters has to be greater than 25

When creating a table, can you put a constraint check on a column to check that the string value has to be greater than 25 characters? I know it can be done on numeric, but having difficulty to do it for a string.
Here is my code:
CREATE TABLE TITLE
(Title_ID VARCHAR(8),
Title_Name VARCHAR(MAX) CHECK (Title_Name > 25));
I realize this only checks to see if the numeric value is greater than 25, but how can make it that it checks that the string value is greater than 25 characters
In Oracle, the maximum length of a string in a table is 4000 8-bit characters (as explained here). And you generally use varchar2() for variable length strings. So you can do:
CREATE TABLE TITLE (
Title_ID VARCHAR2(8),
Title_Name VARCHAR2(4000) CHECK (LENGTH(Title_Name) > 25)
);
If you want an unlimited length string, you can use a CLOB, but those are generally a bit more finicky to work with.
If you happen to be using SQL Server (which your syntax suggests), then this would be:
CREATE TABLE TITLE (
Title_ID VARCHAR(8),
Title_Name VARCHAR(MAX) CHECK (LEN(Title_Name) > 25)
);
You should use LENGTH function to check title length:
CREATE TABLE TITLE(
Title_ID VARCHAR2(8),
Title_Name VARCHAR2(400) CHECK (LENGTH(Title_Name) > 25)
);
db<>fiddle demo

SQLite - Check before insert all characters in a varchar

I have a table that looks like this:
CREATE TABLE customer (
customer_id integer primary key,
email varchar not null,
...
);
On insert, I want to check wether the email has this specific format:
'a#b.c'
Additionally I want to check that a, b and c contain only latin letters (ASCII 65-90) and a, b can also contain numbers (ASCII 48-57).
I thought of creating a trigger and going through all characters of the varchar. Is there a possibility to do that?

How to limit data type to 6 digit numbers in SQL?

I have a table in sql for a bank account
CREATE TABLE (
name VARCHAR(100),
bsb INTEGER,
account_num INTEGER,
PRIMARY KEY (bsb, account_num)
);
How do I put a constraint that will limit BSB to only integers that have 6 digits including 000,000 all the way to 999,999? Do I need a constraint or to use a different data type?
I know I can do this check restraint to limit the size of an int to be 6 digits CONSTRAINT Bank_BSB_CHK CHECK (BSB < 999999) but a BSB can start with 0's but just has to be 6 digits long.
An integer has no fixed format, so 000001 and 1 are the same value. So if you really want an integer, then the correct constraint is CHECK (BSB >= 0 AND BSB <= 999999) (note that you had a fence-post error in your question, using < not <=).
If your data is actually a sequence of 6 digits, with no mathematical meaning, like a phone number, you're probably better off using a string data type, and constraining it by pattern. That way, you won't have to worry about reformatting it when displaying the data.
A reasonably portable constraint would be to use the SIMILAR TO operator (I don't know how widely implemented it is, but it is apparently in the SQL standard), which uses a regular expression:
CHECK ( BSB SIMILAR TO '[0-9]{6}' )
Since the data is a sequence of 6 digits with no mathematical meaning it is better to use a CHAR(6) data type and put a constraint on the pattern the bsb can take. This would be the correct constraint to limit the pattern to 6 digits.
CONSTRAINT Bank_Account_BSB_CHK CHECK (bsb SIMILAR TO '[[:digit:]]{6}')

PL/SQL REGEXP_LIKE with multiple [0-9][0-9]

I am having a hard time making my PL/SQL code work. I have a constraint that makes sure 5 digits have been input. The constraint works for any number that does not use a 0 first. Example 0, 1, 2, 33, 401 work but 01, 02, 004 do not work. How can I make it so that I can input numbers such as 00009?
CREATE TABLE table1
(
id NUMBER(5)
CONSTRAINT idconstraint
CHECK (REGEXP_LIKE(id, '[0-9][0-9][0-9][0-9][0-9]'))
);
INSERT INTO table1
VALUES ('00009');
You seem to have the wrong constraint or the wrong data type. If you want leading zeros, use varchar2() or char(). Because you seem to want a fixed length string, try this:
CREATE TABLE table1 (
id CHAR(5)
CONSTRAINT idconstraint
CHECK (REGEXP_LIKE(id, '[0-9][0-9][0-9][0-9][0-9]'))
);
Your problem is that the field is declared as a number. The input string is converted to a number, then back to a string. It loses the leading 0's in the process.