What is the meaning of nullable() in Laraval migration?
For example, this is in our migration:
$table->string('middle_name')->nullable();
It means the middle_name field can also store null values: as in inserting a value is not required.
Imagine a registration form for example. Not everybody has a middle name. So in that case they would leave the middle_name field empty and in the database it'll be null.
This is equivalent in MySQL statement "DEFAULT NULL", when we declare a column of a MySQL Table.
`imageURL` varchar(255) DEFAULT NULL,
is equal to Laravel's
$table->string('imageURL',255)->nullable();
Then if it's default value is NULL, when we perform an INSERT statement without mentioning a value for that column, the value would be inserted as NULL.
It will make the column nullable in the database which means you can store null values to that column or also can be said that it is not a mandatory field in database
Related
My migration is like so:
$table->bigIncrements('id');
$table->timestamp('from_date');
$table->timestamp('to_date');
$table->timestamps();
The problem is that when I migrate it, the second one which is from_date automatically gets on update CURRENT_TIMESTAMP attribute so it means when I update other columns this column will be updated too. That's what I don't want. How can I prevent it?
You can add nullable to the column in order to remove the constraint.
$table->timestamp('from_date')->nullable();
Unfortunately I think that this is the only solution. Then you can add a form validation in order to prevent setting null value for the field.
-- EDIT
$table->timestamp('from_date')->default(DB::raw('CURRENT_TIMESTAMP'));
// or
$table->timestamp('from_date')->useCurrent();
Try this as well, I believe this is what the created_at has.
You need to make the DateTime column nullable, then MySQL won't add that. By default, MySQL adds that to the first timestamp in the table, unless explicitly told not to (via allowing a null value for the field). This is a MySQL thing, not a Laravel thing.
$table->timestamp('colName')->nullable();
Read :
Automatic Initialization and Updating for TIMESTAMP and DATETIME - MYSQL DOCS
Laravel & MySQL auto-adding “on update current_timestamp()” to timestamp fields
This behaviour is native to MySQL. You can read about it on this documentation page:
TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.
The docs go on to specify that there are two strategies for avoiding this behaviour:
Enable the explicit_defaults_for_timestamp variable in your database configuration.
When defining your timestamp column, specify a DEFAULT or make the column nullable.
I'm trying to understand Room database library. I am struck with a scenario where two tables are linked via #ForeignKey constraint. What I need is, when the parent row is deleted, all the child columns in the child table should be set to NULL or some default value. But when I tried to use
onDelete=SET_NULL or SET_DEFAULT with #ForeignKey
I get the following error:
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint
failed: Log.tagId
From the error I can see that the child column has been set as NOT NULL during the table definition, can someone say how to change it to be NULLABLE since room creates the tables for us? Also, it is ok if we can set a standard default value on the columns. If so, how to set the default value of columns? I think there should be some way else the constants SET_NULL or SET_DEFAULT has no meaning and purpose.
Thanks in advance!
I'd like to add a NOT NULL column to a table called server. Problem is, the table already contains records. When I invoke ALTER TABLE server ADD COLUMN full_discovery_duration BIGINT NOT NULL H2 complains that full_discovery_duration may not be null. I can work around the problem by specifying DEFAULT 0 but I don't want a default valuefor future inserts. What am I supposed to do?
Should I add the column with a default and then remove DEFAULT 0 from the column definition in a subsequent statement? Is there a better way?
You can first add the column with a default value, and then set the default to null. Getting rid of the default definition is not possible however as far as I know.
An alternative is to first allow null, then set the values, and later not allow nulls.
drop table server;
create table server(id int);
insert into server values(1);
alter table server
add column
full_discovery_duration bigint;
update server set full_discovery_duration = 0;
alter table server
alter column
full_discovery_duration set not null;
While adding columns to existing tables, it should either be a nullable column or a default value must be specified. And what do you mean by removing the default? how can you remove value from a not null column?
How can I create unique constraint when I need to treat null values as equals.
For
alter table T1 add constraint T1_UN
unique (nvl(C1, ' '))
i get
ORA-00904: : invalid identifier
points on nvl
Thanks!
NOT NULL seems like a better idea, but you could make a function-based index unique:
create unique index idx_t1 on t1 (nvl(C1, ' '));
Just make the column NOT NULL.
The nature of NULL is that it is never equal to anything. Hence every NULL value in your column is already UNIQUE in a way. It doesn't make sense to include them in a UNIQUE key like you want to. The reason why you want to do it is probably an existing data integrity problem. So to make the column NOT NULL, you might have to migrate NULL values to something else, first...
Note: You can make the column just simply UNIQUE. Then you can have several NULL values as described above...
Using Oracle, if a column value can be 'YES' or 'NO' is it possible to constrain a table so that only one row can have a 'YES' value?
I would rather redesign the table structure but this is not possible.
[UDPATE] Sadly, null values are not allowed in this table.
Use a function-based index:
create unique index only_one_yes on mytable
(case when col='YES' then 'YES' end);
Oracle only indexes keys that are not completely null, and the CASE expression here ensures that all the 'NO' values are changed to nulls and so not indexed.
This is a kludgy hack, but if the column allows NULLs, then you could use NULL in place of "NO" and use "YES" just as before. Apply a unique key constraint to that column, and you'll never get two "YES" values, but still have many NOs.
Update: #Nick Pierpoint: suggested adding a check constraint so that the column values are restricted to just "YES" and NULL. The syntax is all worked out in his answer.
You will want to check a Tom Kyte article with exactly this question being asked and his answer:
http://tkyte.blogspot.com/2008/05/another-of-day.html
Summary: don't use triggers, don't use autonomous transactions, use two tables.
If you use an Oracle database, then you MUST get to know AskTom and get his books.
It doesn't work on the table definition.
However, if you update the table using a trigger calling a stored procedure, you could make sure that only one row contains "YES".
Set all rows to "NO"
Set the row you want to YES
Following on from my comment to a previous answer by yukondude, I'd add a unique index and a check constraint:
create table mytest (
yesorno varchar2(3 char)
);
create unique index uk_mytest_yesorno on mytest(yesorno);
alter table mytest add constraint ck_mytest_yesorno check (yesorno is null or yesorno = 'YES');
Does Oracle support something like filtered indices (last week I heard that e.g. MSSQL2008 does)? Maybe you can define a unique key which applies only to rows with the value "Yes" in your column.
I guess I'd use a second table to point to the appropriate row in your current table. That other table could be used to store values of other variables too too.