notnull

Sql Conditional Not Null Constraint

 ̄綄美尐妖づ 提交于 2019-12-02 20:18:14
I am curious to know is it possible to create a conditional not null constraint in sql? In otherwords is it possible to create a constraint such that a column B can be null as long column A contains lets say 'NEW' but if the contents of column A changes to something else then column B is no longer allowed to be null? And to extend on that, it is then possible to make it so that column B must be null or empty as long as column A says 'NEW'? Thanks All :D This is perfectly fine for CONSTRAINT CHECK. Just do this: Requirement: is it possible to create a constraint such that a column B can be null

Ruby on Rails: How do I add a not null constraint to an existing column using a migration?

為{幸葍}努か 提交于 2019-12-02 16:53:59
In my Rails (3.2) app, I have a bunch of tables in my database but I forgot to add a few not null constraints. I've googled around but I can't find how to write a migration which adds not null to an existing column. TIA. For Rails 4+, nates' answer (using change_column_null ) is better. Pre-Rails 4, try change_column . You can also use change_column_null : change_column_null :table_name, :column_name, false 1) FIRST: Add column with default value 2) THEN: Remove default value add_column :orders, :items, :integer, null: false, default: 0 change_column :orders, :items, :integer, default: nil If

MySQL - Foreign key on delete set null in not null field

五迷三道 提交于 2019-12-02 02:22:43
This is probably a trivial question, but I'm still a little clumsy when it comes to foreign key constraints so I wanted to make sure. Let's say I have a table countries with the fields country_id (PK) and name , and a table cities with the fields city_id (PK), name and country_id (FK). The foreign key cities.country_id has the constraint ON DELETE SET NULL . As I understand it, this means that if a record from countries is deleted, any records in cities that reference that deleted record's country_id will have its country_id field set to NULL. What if, however, cities.country_id has the

IntelliJ IDEA complains about null check for @NotNull parameter

孤街浪徒 提交于 2019-12-01 04:18:58
I want to use Jetbrains @Nullable/@NotNull Annotations in my project. I have a class with a @NotNull field. The constructor naturally does not accept null but throws an exception instead. Of course the parameter of this constructor is also be annotated with @NotNull. Why does IntelliJ IDEA complain about the null-check? The documentation states: An element annotated with NotNull claims null value is forbidden to return (for methods), pass to (parameters) and hold (local variables and fields). But I still have to check for null-values at runtime in case the build system does not understand the

IntelliJ IDEA complains about null check for @NotNull parameter

懵懂的女人 提交于 2019-12-01 01:34:15
问题 I want to use Jetbrains @Nullable/@NotNull Annotations in my project. I have a class with a @NotNull field. The constructor naturally does not accept null but throws an exception instead. Of course the parameter of this constructor is also be annotated with @NotNull. Why does IntelliJ IDEA complain about the null-check? The documentation states: An element annotated with NotNull claims null value is forbidden to return (for methods), pass to (parameters) and hold (local variables and fields).

Check a record IS NOT NULL in plsql

丶灬走出姿态 提交于 2019-11-30 11:00:48
I have a function which would return a record with type my_table%ROWTYPE , and in the caller, I could check if the returned record is null, but PL/SQL complains the if-statement that PLS-00306: wrong number or types of arguments in call to 'IS NOT NULL' Here is my code: v_record my_table%ROWTYPE; v_row_id my_table.row_id%TYPE := 123456; begin v_record := myfunction(v_row_id) if (v_record is not null) then -- do something end if; end; function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is v_record_out my_table%ROWTYPE := null; begin select * into v_record_out from my

insert a NOT NULL column to an existing table

≡放荡痞女 提交于 2019-11-29 20:54:49
I have tried: ALTER TABLE MY_TABLE ADD STAGE INT NOT NULL; But it gives this error message: ALTER TABLE only allows columns to be added that can contain nulls or have a DEFAULT definition specified As an option you can initially create Null-able column, then update your table column with valid not null values and finally ALTER column to set NOT NULL constraint: ALTER TABLE MY_TABLE ADD STAGE INT NULL GO UPDATE MY_TABLE SET <a valid not null values for your column> GO ALTER TABLE MY_TABLE ALTER COLUMN STAGE INT NOT NULL GO Another option is to specify correct default value for your column:

Check a record IS NOT NULL in plsql

左心房为你撑大大i 提交于 2019-11-29 16:29:12
问题 I have a function which would return a record with type my_table%ROWTYPE , and in the caller, I could check if the returned record is null, but PL/SQL complains the if-statement that PLS-00306: wrong number or types of arguments in call to 'IS NOT NULL' Here is my code: v_record my_table%ROWTYPE; v_row_id my_table.row_id%TYPE := 123456; begin v_record := myfunction(v_row_id) if (v_record is not null) then -- do something end if; end; function myfunction(p_row_id in my_table.row_id%TYPE)

SQL Column definition : default value and not null redundant?

佐手、 提交于 2019-11-28 16:47:43
I've seen many times the following syntax which defines a column in a create/alter DDL statement: ALTER TABLE tbl ADD COLUMN col VARCHAR(20) NOT NULL DEFAULT "MyDefault" The question is: since a default value is specified, is it necessary to also specify that the column should not accept NULLs ? In other words, doesn't DEFAULT render NOT NULL redundant ? DEFAULT is the value that will be inserted in the absence of an explicit value in an insert / update statement. Lets assume, your DDL did not have the NOT NULL constraint: ALTER TABLE tbl ADD COLUMN col VARCHAR(20) DEFAULT "MyDefault" Then you

How do I check if a SQL Server text column is empty?

跟風遠走 提交于 2019-11-28 03:13:13
I am using SQL Server 2005. I have a table with a text column and I have many rows in the table where the value of this column is not null, but it is empty. Trying to compare against '' yields this response: The data types text and varchar are incompatible in the not equal to operator. Is there a special function to determine whether the value of a text column is not null but empty? Eric Z Beard where datalength(mytextfield)=0 Eric ISNULL( case textcolum1 WHEN '' THEN NULL ELSE textcolum1 END ,textcolum2) textcolum1 Actually, you just have to use the LIKE operator. SELECT * FROM mytable WHERE