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
Related
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}$'))
);
I have a table with a column nif which has to be exactly 9 digits long, after I tried to do:
CREATE TABLE fornecedor(
nif numeric(9) PRIMARY KEY,
nome varchar(64) NOT NULL,
CONSTRAINT nif_tamanho CHECK (length(to_char(nif, 'FM999MI')) = 9));
INSERT INTO fornecedor(nif, nome) VALUES (123456789, 'Pmsmm');
It returns an error saying:
ERROR: new row for relation "fornecedor" violates check constraint "nif_tamanho"
How do I make it so that the inserted number is verified having 9 digits exactly ?
Thanks in advance
As you mention in your comment, you need a string data type if you don't want to lose leading zeroes.
But I think that the correct solution depends on how you want to use the column. It is always good to choose the data type according to the column's semantics.
If you need to do arithmetic or numeric comparisons with the data, choose integer by all means (you can add a check constraint on nif < 100000000). You can always format the value with leading zeroes when you convert it to a string for output.
If you need to do string operations on the value (e.g. substrings), storing the data as a sting type is preferable.
I want to put some special constraints over my values in the next table:
CREATE TABLE MyTable
{
id varchar(100) NOT NULL PRIMARY KEY,
first_special varchar(8), <- I want it to have max of 8 symbols with no spaces in it
second_special float <- I want it to have precision of 2 decimal points after the '.'
}
EDIT:
The platform is Microsoft SQL Server.
In Oracle (and I think in all dbs) the second one is easy.
second_special NUMBER(5,2) -- The max number would be 999.99
Hi I have the below table created
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar,
lastname nvarchar,
city nvarchar,
zip int
);
I have written the insert statement as
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city','123456')
but i am getting the following error when i try to insert values into the table
string or binary data would be truncated
The statement has been terminated
Please help to overcome my problem , i am not sure where i did i went wrong.
Please help,
Thanks In Advance.
in your script there are 2 issues:
You did not specify the length of the varchar columns.
Take a look here.
Do not define columns, variables and parameters using VARCHAR, and
NVARCHAR data types without specifying length attribute. This will not
produce a dynamic length string data, but will make SQL Server choose
default length of 1 (NOTE: In some scenarios it the length can be 30).
Replace it with:
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar(50),
lastname nvarchar(50),
city nvarchar(50),
zip int
);
2..You are attempting to insert a varchar zip rather as int (which is the columns type).
Replace the insert with:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
Whenever you see this error
string or binary data would be truncated its a column length` issue.
Check the length of the column in the sql field definition
You are trying to insert data of more length than the field length
So either you limit the data to the length in database or increase the column length.
You are inserting a string for an int (zip), try this:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
EDIT:
you should also declare your nvarchar length as it only creates it as a nvarchar(1) if you don't , so change all of your nvarchar's to nvarchar(50) or whatever length you want for max
Per TSQL Technet Article on NVARCHAR:
nvarchar[(n)]: When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
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.