I've got a problem with my sql code (working with oracle).
I'm triyng to make a constraint with body height, which should be between 1,0 and 2,4. The data type is a float. The decimal place is divided with a comma.
This ist the code:
alter table tableName
add constraint check_height
check (columnName between 1,0 and 2,4);
I tried to divide the decimal places in the code (I can't change the decimal place holder in the data list) with comma and points, I also tried to change the range and to show the range with < >. Nothing worked so far. Has anyone an idea what I am missing?
The error message is: 'check constraint violated'.
cheers
If you already have data in your table with values that would violate the constraint, then you can't create it with the default validate clause:
create table tableName(columnName number);
insert into tablename (columnName) values(2.5);
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4);
SQL Error: ORA-02293: cannot validate (STACKOVERFLOW.CHECK_HEIGHT) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious
(My territory has nls_numeric_characters='.,', so I've used . rather than ,).
You can either correct or remove the invalid values, or allow the bad values to remain while only validating new data by specifying the novalidate clause:
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4)
novalidate;
table TABLENAME altered.
New inserts or updates will still be validated, this only affects existing data.
The decimal place should be separated with a dot . from the fractional part, not a comma, so:
alter table tableName
add constraint check_height
check (columnName between 1.0 and 2.4);
Edit
Regarding comments below - you cannot write a literal number in SQL and PL/SQL using a different decimal point separator than a dot . (documentation). You can alter the session to set the NLS_NUMERIC_CHARACTERS parameter to change the decimal point separator, but it will only be taken into account when converting value of a different type to a number, for example, in the TO_CHAR function.
If you change the NLS_NUMERIC_CHARACTERS, you can use a different separator than a dot . if you enclose the number in quotes (and that will cause an implicit conversion to a number, using the characters set in NLS_NUMERIC_CHARACTERS to determine what is a decimal point, and what is a group separator). So, this will work:
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ", ";
ALTER TABLE tableName
ADD CONSTRAINT check_height
CHECK (columnName BETWEEN '1,0' AND '2,4');
Related
Everytime I try to add a column using ALTER and ADD COLUMN I am getting a syntax error.
ALTER TABLE EMP_1
ADD COLUMN EMP_PCT NUMBER(4,2),
ADD COLUMN PROJ_NUM CHAR(3);
I am not familiar with a number data type. Perhaps you mean decimal:
ALTER TABLE EMP_1 ADD COLUMN EMP_PCT DECIMAL(5, 2);
You may also need two ALTER TABLE statements. I am just not sure if MS Access allows two changes in one statement.
For a numeric type you should use NUMERIC, not NUMBER, and for a text type you should use VARCHAR.
For a test tomorrow we're told to name our constraints
I know it's possible to create a constraint when you use ALTER TABLE
but can you add a name to a not null constraint when you CREATE TABLE?
f.e.
CREATE TABLE test (
test1 VARCHAR
CONSTRAINT nn_test1 NOT NULL (test1)
)
I get an error when trying to run this query. Am I writing it wrong?
The error I get is
ERROR: syntax error at or near "NOT"
LINE 3: CONSTRAINT nn_test1 NOT NULL (test1))
^
SQL state: 42601
Character: 56
You have two options to define a named not null constraint:
Inline with the column:
CREATE TABLE test
(
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL,
test2 integer --<< no comma because it's the last column
);
Or at the end of columns as an out-of-line constraint. But then you need a check constraint:
CREATE TABLE test
(
test1 VARCHAR,
test2 integer, --<< comma required after the last column
constraint nn_test1 check (test1 is not null)
);
This has become irrelevant, since you're not using SQL Server
First of all, you should always specify a length for a VARCHAR. Not doing so (in SQL Server variables, or parameters) may result in a string of just exactly ONE character in length - typically NOT what you want.
Then, you need to just specify the NOT NULL - there's no need to repeat the column name (actually this is the error) - if you're specifying the CONSTRAINT "inline" with the column definition (which is a perfectly legal and in my opinion the preferred way of doing this).
Try this code:
CREATE TABLE test
(
test1 VARCHAR(50)
CONSTRAINT nn_test1 NOT NULL
)
At least this is the CREATE TABLE statement that works in T-SQL / SQL Server - not sure about PostgreSQL (don't know it well enough, don't have it at hand to test right now).
I, a_horse_with_no_name, the two syntax:
constraint nn_test1 check (test1 is not null)
and
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL
are equivalent ? performance correctly ecc.
Because in first case the SQL server exception return the name nn_test so the system know exactly error.
I'm a beginner on oracle apex and im trying to implement a constraint that only allows variables in a column that start with a certain letter ('P') and are followed by numeric digits. Any help?
You could use the regexp_like operator:
ALTER TABLE mytable
ADD CONSTRAINT mytable_field_check
CHECK (REGEXP_LIKE (myfiled, 'P[0-9]*'))
How can you alter the default value set to a column in a table in SQL.
I got an error from:
ALTER TABLE tablename.tab ALTER COLUMN mess1 DEFAULT ('hi')
What was the correct query?
I would name your constraints. To change an existing one...
ALTER TABLE tablename.tab
DROP CONSTRAINT .... --you have a system generated name. Well done.
ALTER TABLE tablename.tab
ADD CONSTRAINT DF_tablename_mess1 DEFAULT 'hi' FOR mess1
Normally, the syntax is a variant of:
ALTER TABLE jankhana.jankh MODIFY (mess1 CHAR(10) NOT NULL DEFAULT 'hi');
Technically, the parentheses around the column specification are optional when there's just one column; if there are several, they are mandatory.
The details could vary by DBMS - DDL statements tend to be the most variable.
I created a SQL table an enforced check constraints on it, but now when I try to insert data I get an error message.
create table BranchTel
(
BrRegNo varchar(10) REFERENCES Branch(BrRegNo),
TelNo char(12)
PRIMARY KEY(BrRegNo)
)
ALTER TABLE BranchTel Add Constraint BranchTelTelNo
Check(TelNo LIKE '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
Insert statement
insert into BranchTel values('BG-205','940112571963')
Error message
The INSERT statement conflicted with the CHECK constraint "BranchTelTelNo". The conflict occurred in database "StudentDetails", table "dbo.BranchTel", column 'TelNo'.
The statement has been terminated.
Insert statement
insert into BranchTel values('BG-205','94-011-2571963')
Error message
String or binary data would be truncated.
The statement has been terminated.
Please help me
Your check constraint is 14 characters long (you need to count the - as well), while the field size is 12.
Additionally, 940112571963 does not conform to the pattern xx-xxx-xxxxxxx you have defined in your check constraint.
You need to change the field size to 14 and when inserting make sure the dashes are in the right place:
insert into BranchTel values('BG-205','94-011-2571963')
Insert statement insert into BranchTel values('BG-205','94-011-2571963') Error message String or binary data would be truncated. The statement has been terminated.
Here the value 94-011-2571963 length is greater than 12 which obviously violates the check constraint.