column x (Not NULL) | column z
1............................| P
0............................| T
I got a check constraint : column x in (1, 0), so the column only accept value 1 and 0
what I want which is really weird (I don't want a procedure or a trigger). I want a way like constraints to change the nullable of the column z by the value that the column x have
example:
if column x has value of 1 column z should be nullable,
if column x have value of 0 column z should be Not null with default N
Why do I want that? I am using oracle and sybase, the problem in sybase if I add column with out specifiying if null or not , automatically it will be not null(add test varchar(12) <-- this will be Null in sybase by default and null by oracle , so by that column I want to specify if its syabse or oracle)
Maybe a procedure will be better , but I am searching if there is another way for my problem.
I would start with a check constraint for integrity:
ALTER TABLE <table> ADD CONSTRAINT chk_x_z CHECK (X = 1 OR Z IS NOT NULL)
This will make sure that your business rule is always enforced.
You will need either a procedure or a trigger to implement your custom default N.
Related
I am looking for a query which I can use in pymysql to UPDATE the rows in one column without the WHERE constraint.
This is the query I used but it replaces all the NULL into 1s:
UPDATE simplefocusstatistic_studentanswers
SET Player_id = null WHERE simplefocusstatistic_id = 1
I am trying to alter a table where the original datatype of this column is Number to a generated column, but I get an error of "ORA-00905 Missing Keyword"
Alter Table MyTable
Modify Column FlagColumn NUMERIC (38,0) GENERATED ALWAYS AS (CASE WHEN ValueColumn IS NULL THEN 1 ELSE 0 END) VIRTUAL;
Is my syntax correct?
Do I have any other options besides dropping and recreating the table?
The Oracle documentation pretty clearly doesn't support the syntax you're attempting. The obvious solution is to drop the column, then replace it:
ALTER TABLE mytable
DROP COLUMN flagcolumn;
ALTER TABLE mytable
ADD numeric GENERATED ALWAYS AS (CASE WHEN valuecolumn IS NULL THEN 1 ELSE 0 END) VIRTUAL;
There's really no reason not to do this, since you're getting rid of the column's original data in any case.
I have to update a 'varchar2' column in a table with random values, but the catch is that the column is defined with 'unique' constraint, due to which I am getting the error saying that "Unique constraint violated".
Can somebody help please.
Thanks
You haven't given any constraints over what the values might be, so why not just set it to a set of known unique values, e.g. integers:
UPDATE table_name SET unique_col = TO_CHAR(ROWNUM);
you can use the DBMS_RANDOM package to create the random values and insert the same in table like below
INSERT INTO table_name(unique_col) values (DBMS_RANDOM.string('a',n);
here n can be max value of table. For more information refer here http://oracle-base.com/articles/misc/dbms_random.php
The value generated will be unique depends on the length of the value
why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.
I have that query :
INSERT INTO GOST (ASSORTMENTID, ROZMIAR, GOST)
VALUES ( 54,'S','MjgwMzktODkgMTc0LTk2')
I want insert new row in table GOST, but I don't want to specify column with primary key - GOSTID. I want that database set next id value.
When I run this code I have that error:
validation error for column GOSTID, value "* null *"
I understand that I should set GOSTID column in INSERT query, yes ?
It is possible to run this without this parameter ?
I think a sample script worths more than 1000 words:
Go to a shell interface in the firebird server machine, cd to a folder where you have read/write permissions, start isql or isql-fb (depends on your system and firebird version) and run this script:
create database 'netmajor.fdb' user 'sysdba' password 'masterkey';
set autoddl off;
create table netmajor_example (
netmajor_id integer not null
, str_data varchar(200)
, int_data integer
, constraint pk_netmajor_example
primary key (netmajor_id)
);
create generator netmajor_gen;
set term ^;
create trigger netmajor_pkassign
for netmajor_example
active before insert position 1
AS
begin
if (new.netmajor_id is null) then
new.netmajor_id = gen_id(netmajor_gen, 1);
end
^
commit work^
set term ; ^
insert into netmajor_example (str_data, int_data) values ('one', 1);
insert into netmajor_example (str_data, int_data) values ('twenty', 20);
commit work;
select * from netmajor_example;
Take a look at the results, which in my machine are:
; NETMAJOR_ID STR_DATA INT_DATA
;============ ============================ ============
; 1 one 1
; 2 twenty 20
IF you have questions, don't hesitate to contact. Best regards.
Obviously, your primary key is a NOT NULL column, which means, it's always required. You cannot insert a row without giving a value for the primary key (unless it were an "auto-number" column which gets automatically set by the database system).
Use "before insert" trigger to set value for primary key. Firebird doesn't have "auto-increment" field type, so you need take care of it by yourself.
See http://www.firebirdfaq.org/faq29/ for tutorial how to do this. Some DB applications (eg Database Workbench) can create the trigger and generator automatically.