Why laravel timestamp fields are nullables? - mysql

this is not a technical question.
I'm using laravel for several projects and today I had a doubt: why Laravel timestamp fields are can be null? Is there a portability reason behind this choice or it is only useful?
What is the principle that they applied on this choice?

This is because of MySQL. If they weren't set nullable in some MySQL versions MySQL would put in there own values and when they are marked as nullable, MySQL won't put there own values and values from framework will be used.

Related

SQLAlchemy, MySQL, and python - how should I handle boolean?

I have a Pyramid application that I am using with SQLAlchemy and MySQL. For database fields that I wanted to treat as boolean, I've been using a "BIT" data type on the SQLAlchemy side, and BIT(1) on the MySQL side.
This had all been working fine, but I was checking some newly updated code on my webhost and their version of phpMyAdmin is newer than the one I'm using locally; I was browsing a table that has a BIT field and on the newer phpMyAdmin none of the data appears - it's just blank. On my local instance BIT fields display as 0 or 1. If I tried to inline edit the hosted phpMyAdmin it wouldn't take any values I tried. I did try my application code and it appears to be able to toggle the true/false values just fine.
The got me wondering - with this setup should I be approaching it differently? SQLAlchemy does support Boolean, which seems like it would be more intuitive and appropriate, should I use that and set the MySQL fields to TINYINT instead?
What is the conventionally accepted way to handle booleans between SQLAlchemy and MySQL?
MySQL has a BOOL type (which is what SQLAlchemy uses) so I'm not sure why you don't just use that? Apparently it is an alias for TINYINT.
from sqlalchemy import Boolean and you should be good to go.

Insert datetime with min mysql value in table with Entity Framework

i have a question about the default min value of a date in mysql and entity framework.
I have a a web application that uses entity framework 4.1 and the mysql connector 6.3.6 and in my mysql table i have columns that the need to initialize in the min datettime value of mysql that is '0000-00-00', but from the .NET side, i cant insert that value because the min value of .NET is '0001-01-01' and that gives me many problems. I cant change that the mysql column allow the '0001-01-01' because the mysql table is used in another projects that use that date as a reference, so if i change this for my project, then all the projects will need to update their code and thats is a thing that i dont want to.
So, do i have a solution for this? How can i insert a min mysql value ('0000-00-00') from .NET using entity framework?
Thanks!
You can set default value in MYSQL with your desired format, and pass null value with EF.
Hope isn't mandatory fields ;-)

What is the cost of using NULL in database columns?

I use MySQL and SQLite often and plan on bringing more PostgreSQL into my workflow soon. With that in mind, what are the costs of using NULL in each database? I heard that MySQL adds an extra bit to each NULL column value to mark it as nullable.
This question was answered separately for PostgreSQL:
How much disk-space is needed to store a NULL value using postgresql DB?
and for MySQL:
NULL in MySQL (Performance & Storage)
But to recap they both use bitmask fields to mark nulls.

Fix length number in mysql

I want to have a field in a Mysql table, which should accept inputs having a fixed size - no more, no less. The input data is a number, but solutions for strings can also be considered, as I have no problem storing this data as varchar like stuff.
To be exact, I want a datatype which will NOT allow me to store a number which is having less than 7 or greater than 7 digits. I dont want to use triggers/stored procedures.
This may be possible with a stored procedure, but I wouldn't do this on database level. Validation like this belongs in your application.
I don't believe there is any way to achieve this in MySQL at present without using triggers or stored procedures. If MySQL supported check constraints then you could do it, but it doesn't, so you can't.
The possible solutions are:
TRIGGER on update/insert.
CHECK constraint, but MySQL parses and promptly discards check constraints.
Application-level validation.
Foreign key to a lookup table containing the 900,000 integers of 7 digits.
The only other suggestion is to migrate to a SQL database that supports CHECK constraints.
Open-source databases that support CHECK constraints include:
PostgreSQL
SQLite
Firebird
Apache Derby
HyperSQL
Every commercial database also supports CHECK constraints.
Basically, MySQL is the only SQL database on the market that doesn't support CHECK constraints!

for the database question using the mysql and the .net 2.0

I have my data in the mysql database but i want to update my database tables without affecting the old data.
and i need the functionality that i can fetch the old data as well as new updated data as and when required in the software according to the date
Here's one way to do what you describe:
Add a datetime column—let's say INSERTED_DT—to your database.
Populate INSERTED_DT with the current datetime on insert and/or on update. Most DBMSs have some facility to make it easy to do this kind of thing.
Write database queries with parameters to extract relevant sections of your database based on INSERTED_DT.