This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL ignores the NOT NULL constraint
I set a column to be not null
but when inserting new row and put this field value with null
mysql inserts the row
how i prevent that?
did you insert the value on database field like
insert into table
values('');
or
insert into table
values(null);
well both will insert a row in database but the field will be with null value. NULL is a keyword which indicates a field value that is null. if you want the field value will be empty then thats not null actually. to do so you have to do
insert into table
values(' '); // a space bar in between ' ' so thats not null
Related
This question already has answers here:
mysql automatically store record creation timestamp
(6 answers)
Closed 3 years ago.
I want to define a new MySQL table, let's say table-1, with a timestamp column that will be initialized only on INSERT and not on UPDATE. How to do it?
you could use the deful value CURRENT_TIMESTAMP
create table table1 (
your_col your_data_type ,
......
my_insert_date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
and let the col my_insert_date_time null in insert
This question already has answers here:
MySQL LAST_INSERT_ID() used with multiple records INSERT statement
(3 answers)
Closed 3 years ago.
Have:
1. create DB
2. create Table
3. insert 3 rows
4. select LAST_INSERT_ID()
Here test code:
DROP DATABASE IF EXISTS TEST;
CREATE DATABASE TEST;
USE TEST;
CREATE TABLE test (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
age INT
);
INSERT INTO test (age) VALUES (1), (2), (3);
SELECT LAST_INSERT_ID();
Why LAST_INSERT_ID() return 1 ?
Excepted: 3
How to get valid LAST_INSERT_ID() ?
The MySQL documentation clearly explains this behavior:
With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first (emphasis mine) automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.
The first value generated by the auto increment sequence in the insert was 1, not 2 or 3, so the value 1 gets returned.
I think the confusion you have is around the name LAST_INSERT_ID. The "last" part refers to the most recent insert statement, not the most recent id value within that insert.
This question already has answers here:
Mysql Table Column Cannot Be Null
(1 answer)
mysql - "column cannot be null"
(2 answers)
Closed 4 years ago.
I have read a lot of different posts trying to figure out why my code isn't working but the goal here is to set the value of the column 'name' to null when column 'mark' is below 69. My code is:
CREATE PROCEDURE gradesReport()
BEGIN
SELECT name FROM students WHERE mark > 69;
UPDATE students SET name = NULL WHERE mark < 69;
END
The first statement works fine but then I get the error that column name cannot be null.
It could be that the column name is declared as not null
You should firstly changed it into a NOT NULL Column
ALTER TABLE students
CHANGE `name` varchar(255) NULL;
Here is another way of making a column nullable in MySQL:
ALTER TABLE students MODIFY name VARCHAR(255);
Columns are nullable by default in MySQL, so we don't actually need to specify NULL in the alter statement.
This question already has answers here:
How to make MySQL table primary key auto increment with some prefix
(4 answers)
Autoincrement ID with a prefix
(3 answers)
Closed 5 years ago.
ID column set to primary key, auto increment.
I want to have a second column Project number, is it possible to set something like this in SQL ? Or how should I do this ?
ID: 1 projectnumber:Project001
ID: 2 projectnumber:Project002
.
.
.
ID: n projectnumber: Project00n
you could do that in 2 ways:
Either using a trigger or do that when retrieving the record from the database:
trigger: after insert
CREATE TRIGGER `yourtable_after_insert` AFTER INSERT ON `yourtable` FOR EACH ROW BEGIN
UPDATE yourtable
SET projectnumber = CONCAT('project', NEW.id)
WHERE id = NEW.id;
END;
or
just do that CONCAT thing in your select query or even better in the logic of php. Consider the possibility you want to translate your application. You would store duplicate information as well...
as pointed out below: NEW.id will not work.
So use LAST_INSERT_ID() instead:
CREATE TRIGGER `yourtable_after_insert` AFTER INSERT ON `yourtable`
FOR EACH ROW BEGIN
UPDATE yourtable
SET projectnumber = CONCAT('project', LAST_INSERT_ID())
WHERE id = LAST_INSERT_ID();
END;
but still: it would be duplicating content
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
INSERT INTO 'rozliczenia' ('userid', 'data', 'stawka') VALUES ('1', '2015-11-01', NULL) ON DUPLICATE KEY UPDATE stawka = COALESCE(VALUES('stawka'), 'stawka');
It should check if value is NULL. If it is then don't update premia column(I don't have any null values in table), but it updates it to 0. Why? Everything worked fine yesterday.
Before update:
Premia 200
After
Premia 0, though it should be 200
Indexes in the table:
Primary - Id
Unique - data
Unique - userid
IT IS NOT ABOUT QUOTES, WHEN CHANGE THEM IT BEHAVES THE SAME WAY
You have single quotes around table and column names, so your query shouldn't work.
INSERT INTO rozliczenia(userid, data, stawka)
VALUES ('1', '2015-11-01', NULL)
ON DUPLICATE KEY UPDATE stawka = COALESCE(VALUES(stawka), stawka);
The reason why coalesce() returns 0 is because you are passing in a string and then using it in a numeric context. A string that doesn't start with a digit ends up being 0 in such a context.
Repeat until you understand it thoroughly: "I will only use single quotes for string and date constants."