I am using mysql for a project. When I did 'desc table_name', I see that the tabular output shows the Null of the field is set to No, but then 'Default' is set to NULL. How can a field not have a Null but then default is Null itself?
mysql> desc users;
+-----------------+------------------+------+-----+----------------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+----------------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| email | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| profile_pic | varchar(255) | YES | | img/profile/profilepic.jpg | |
| activation_code | varchar(255) | YES | UNI | NULL | |
| status_id | int(10) unsigned | NO | MUL | 1 | |
| profile_id | int(10) unsigned | YES | MUL | NULL | |
+-----------------+------------------+------+-----+----------------------------+----------------+
10 rows in set (0.00 sec)
This is a perfectly legal combination. It means that when you try to insert a new row and do not explicitly specify this column's value, it would default to null and thus error out. This combination is often used as a way to force you to explicitly assign a value to the column instead of just relying on defaults.
we should know this:
Null(NO): means this column couldn't accept a "NULL" value
Null(YES): means this column could accept a "NULL" value
Default: means this column's default value :)
we should know: in MySQL, blank is not equal to NULL, if we insert nothing into a column, it will stay nothing(it will not insert a "NULL" into this column)
so, when Null(NO) comes with Default(NULL), if we insert nothing to this value, it'll have a default value "NULL" and it 'll conflict with the constraint, then it'll throw a exception
Related
I not sure if this is related to Laravel or not but I created the table with Laravel. I've got a table called programmers
DESC programmers;
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| framework_id | int(10) unsigned | NO | | NULL | |
| test | tinyint(1) | NO | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
as you can see there's a column called test that's not nullable and has a default value of null. When I to run the following command from the database I expected an error
INSERT INTO programmers (name, age, framework_id) VALUES ('Melly2', 19, 2)
it actually worked fine and here's the data
SELECT * FROM programmers;
+----+--------+-----+---------------------+---------------------+--------------+------+
| id | name | age | created_at | updated_at | framework_id | test |
+----+--------+-----+---------------------+---------------------+--------------+------+
| 1 | melly | 20 | 2022-05-03 16:36:12 | 2022-05-03 16:36:12 | 1 | 0 |
| 2 | Melly2 | 19 | NULL | NULL | 2 | 0 |
+----+--------+-----+---------------------+---------------------+--------------+------+
the test column actually defaulted to 0 not null, and if I were to run the following command it tells me I can't have null as a value as expected
INSERT INTO programmers (name, age, framework_id, test) VALUES ('Melly2', 19, 3, null);
ERROR 1048 (23000): Column 'test' cannot be null
question: can someone briefly explain why test column didn't default to null?
In this scenario, the default value is null only if you don't provide a value. But when you provide some value, it should be compatible with the datatype you set for the column.
Here the datatype is tinyint. So, you should provide the values from true/false which infact will be converted into 1/0; else you should insert integers example:0,1,2,... etc.
Here's my table.
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Postcode | varchar(255) | YES | | NULL | |
| Town | varchar(255) | YES | | NULL | |
| Region | varchar(255) | YES | | NULL | |
| Company Name | varchar(255) | YES | | NULL | |
| Fee | double | YES | | NULL | |
| Company Benefits | varchar(255) | YES | | NULL | |
| Date Updated | date | YES | | NULL | |
| Website | mediumtext | YES | | NULL | |
| Updated By | varchar(255) | YES | | NULL | |
| Notes | varchar(255) | YES | | NULL | |
| LNG | varchar(255) | YES | | NULL | |
| LAT | varchar(255) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
You can see we have an "Updated by" column.
How can I make it so that, when a user updates the row, the "Updated By" column automatically updates (or inserts if it's a new row they're adding) with the currently logged-in users name?
Many Thanks
You will have to explicitly make sure about that and whenever an UPDATE is happening then you need to update that column as well saying below. Best way to assure it, have your application logic fill in the column whenever an UPDATE to the record is happening from current logged-in user principle or claim
update tbl1
set ...,
Updated By = <logged in user name>
where Id = <some val>
You can use USER() or CURRENT_USER() in Update or Insert statements to achieve needed effect.
From my side - the only one secure way is to create stored procedures, providing inserts or updates to desired table.
Indeed, this problem was discussed here:
mysql Set default value of a column as the current logged in user
Something like this !
CREATE TRIGGER `updater`.`tableName_BEFORE_INSERT` BEFORE INSERT ON `tableName`
FOR EACH ROW
BEGIN
Set New.Updated_By = current_user();
END
I am getting this error
javax.servlet.ServletException: com.mysql.jdbc.NotUpdatable: Result
Set not updatable.
I know this error is regarding the primary key but for all my tables I initially insert a primary key.So for this table also I have a primary key.I am posting part of my code.
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("Select * from test3 order by rand() limit 5");
List arrlist = new ArrayList();
while(rs.next()){
String xa =rs.getString("display");
if(xa.equals("1")){
arrlist.add(rs.getString("question_text"));
}
rs.updateString("display", "0");
rs.updateRow();
Just tell me if something is going wrong in this code.please help.
This is my database
+----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| index_question | varchar(45) | YES | | NULL | |
| question_no | varchar(10) | YES | | NULL | |
| question_text | varchar(1000) | YES | | NULL | |
| file_name | varchar(128) | YES | | NULL | |
| attachment | mediumblob | YES | | NULL | |
| display | varchar(10) | YES | | NULL | |
+----------------+---------------+------+-----+---------+----------------+
You have to update the row immediately after you have fetched it (FOR UPDATE and rs.updateRow(),
OR
you have to write an UPDATE tablename set = where statement to update a row at any time
The query can not use functions. Try removing the "rand()" from the SQL query string.
See the JDBC 2.1 API Specification, section 5.6 for more details.
I'm testing a link-shortener Sinatra application that uses a mysql database. When I enter a link to be shortened, I'm getting this mysql error
(mysql_errno=1364, sql_state=HY000) Field 'link_identifier' doesn't have a default value Query: INSERT INTO `urls` (`original`) VALUES ('http://pryrepl.org/screencasts.html')
I'm assuming that means field link_identifier should have a default value
urls and visits tables both have link_identifier field, but the links table doesn't. However, in the code that creates the tables, copied below, it's the Link class that has the `property :identifier.' Does anyone know how I would fix this problem?
mysql> show columns from urls;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| original | varchar(255) | YES | | NULL | |
| link_identifier | varchar(50) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
3 rows in set (0.12 sec)
mysql> show columns from visits
-> ;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| created_at | datetime | YES | | NULL | |
| ip | varchar(39) | YES | | NULL | |
| country | varchar(50) | YES | | NULL | |
| link_identifier | varchar(50) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
5 rows in set (0.08 sec)
mysql> show columns from links
-> ;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| identifier | varchar(50) | NO | PRI | NULL | |
| created_at | datetime | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
Code
class Url
include DataMapper::Resource
property :id, Serial
property :original, String, :length => 255
belongs_to :link
end
....
class Link
include DataMapper::Resource
property :identifier, String, :key => true
property :created_at, DateTime
has 1, :url
has n, :visits
...
class Visit
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :ip, IPAddress
property :country, String
belongs_to :link
after :create, :set_country
....
Update - the source code already seems to have a way to create a link identifier to satisfy the requirements of mysql, but it doesn't seem to be working
def self.create_link(original)
url = Url.create(:original => original)
if Link.first(:identifier => url.id.to_s(36)).nil? or !DIRTY_WORDS.include? url.id.to_s(36)
link = Link.new(:identifier => url.id.to_s(36))
link.url = url
link.save
return link
else
create_link(original)
end
end
end
The error means you are not setting a value for link_identifier in your insert statement. And link_identifier cannot be null by table definition.
You need to specify a value for link_identifier. Example
INSERT INTO `urls` (`original`, `link_identifier`)
VALUES ('http://pryrepl.org/screencasts.html', 'value_for_link_identifier')
When sending the query
INSERT INTO `urls` (`original`) VALUES ('http://pryrepl.org/screencasts.html')
the value for link_identifier isn't set.
You defined the urls table as
mysql> show columns from urls;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| original | varchar(255) | YES | | NULL | |
| link_identifier | varchar(50) | NO | MUL | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
3 rows in set (0.12 sec)
The default value for link_identifier is NULL (or not set), but since the value cannot be NULL, mysql doesn't know what to set as value.
Change the mysql query into
INSERT INTO `urls` (`original`, `link_identifier`) VALUES ('http://pryrepl.org/screencasts.html', '<your identifier here>')
I have following a structure of MySQL table:
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| reg | int(11) | NO | | NULL | |
| descr | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
And with this way I am saving data to my database:
#saving = Table.new(:reg => 1234, :descr => params[:descr]).save
And my problem is - I don't know how it is possible, but this query does not save the number to column "reg". I am getting this error
Mysql2::Error: Column 'reg' cannot be null: INSERT INTOname_of_table(updated_at,descr,reg,created_at) VALUES ('2011-06-06 20:40:43', 'description of man', NULL, '2011-06-06 20:40:43')
I worked a lot with PHP and MySQL database, but this error I never got...
Where's the problem?
if you look at your schema, it says the default for :reg is NULL, and that it can not be NULL -- that might cause the problem you see
can you change the schema to allow :reg to be NULL and try again?