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.
Related
ALTER TABLE table_name ALTER COLUMN column_name [int] (4) NULL;
unable to execute the script, please assist how can I add maximum length to already existed column for an int data type.
If I understand correctly, you want to add a constraint to the column so that it cannot contain a value larger than 9999:
ALTER TABLE table_name
ADD CONSTRAINT CK_column_name_RANGE CHECK (column_name >= 0 AND column_name <= 9999)
SQL Server's integer data types use binary integers. The INT data type is a 32-bit signed number, which allows values from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). You cannot specify the width of such integers, except by choosing TINYINT or SMALLINT.
If you must somehow prevent numbers outside the range that fits in four digits from getting into your table, you can use the DECIMAL data type; it allows you to specify the digit count.
column_name DECIMAL(4,0)
I need to be able to write a script to create a table in Microsoft Access, and I am running into a problem. I need a long integer field (longer than LONG), so I have to use DECIMAL. The problem I am running into is it is defaulting the Format to scientific notation. I need it to stay as a normal integer value.
Here is my script:
CREATE TABLE MY_TABLE
(
MY_GUID GUID CONSTRAINT MY_CONSTRAINT PRIMARY KEY,
MY_NAME VARCHAR,
CREATOR VARCHAR,
MILLI_CREATED DECIMAL (13, 0)
);
The MILLI_CREATED field defaults to General Number Format, which then displays the value in scientific notation, like this: 1.444255960009E+12
How can I set the Format of the table during CREATE or with an ALTER or even at INSERT if that is what I have to do?
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}')
I have just started with SQL and want to clear the basic keywords of SQL.
What is the difference between
"number" and "numeric" & "number & integer"?
While creating a table
Create table myTable
(
my_Id int(6) primary key
...
Above query Gives me an error suggesting to put null or not null before "primary key".
Do I always need to put either null or not null for the keyword integer?
If I replace int(6) with number(6), that statement works.
1."number" and "numeric" & "number & integer"?
An integer cannot take inputs such as 1.1 and the likes since float or decimal datatype handles this, while a number can take this both. I believe the reason why INT does not display it with a decimal its because its being rounded off try to input a 1.5 on an int column and you'll get a 2 instead
2.While creating a table
Create table myTable (
my_Id int(6) primary key, <--- Gives me an error suggesting to put
null or not null before "primary key". Do I always need to put either
null or not null for the keyword integer?
you need to either put a null or not null before a primary key unless I believe its been set into an Auto Increment
BTW my answer was based on MYSQL since that's what I used.. although I'm not sure if your using it since you didn't add any tags :)
for more info for this topic I think this could add a little more light to your inquiry
reference link
In MYSQL a primary key has to be a non-null value ie you will have to indicate by typing in NOT NULL You can re-write the code as follows:
my_id INT([optional]) PRIMARY KEY NOT NULL
When you want to make a Primary Key field it shouldn't be Null.
And
When you use int data type it don't have any (<value>), But number has.
SO
my_Id int not null primary key
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.