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.
Related
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:
Query with multiple values in a column
(4 answers)
Closed 6 years ago.
I am having a problem with my sql command.
So I have a column of type SET let's call it test_set, and I have multiple values that a row can have, let's say test1, test2
And let's say I have one row that has test1,
and another that has test1 and test2,
How could I select all rows that have test1, (should return both rows)
What about all rows that have test2 (Should return the second row)
As of right now, I know you can do SELECT * WHERE test_set='test1'
But this only returns rows that only have test1, not the ones that have test1 and test2.
Thanks!
If I am understanding you correctly you have a VARCHAR column containing comma delimited values.
In that case a LIKE will work for you.
SELECT * WHERE test_set LIKE '%test1%'
You might want to consider changing the database schema if you can though - For example have a separate "SETS" table that references your original table.
Ex.
CREATE TABLE MY_DATA (ID INT NOT NULL, NAME VARCHAR(255) NULL)
CREATE TABLE SETS (ID INT NOT NULL, MY_DATA_ID INT NOT NULL, SET_ITEM VARCHAR(50) NOT NULL)
SELECT *
FROM MY_DATA D
JOIN SETS S
ON S.MY_DATA_ID = D.ID
WHERE S.SET_ITEM = 'test1'
I am using MySQL, I have a table called student as given below,
Name Idnumber
'tony' 1
'jimmy' 2
I am going to add one more column using the below query
alter table student add location varchar(20) not null;
Now, if I use the query,
update student set location=null where name='tony'
the value of null is being inserted into the table. Below was the table I got.
Name IdNumber location
'tony' 1 null
'jimmy' 2
Since I had set location as not null while altering the table, why is it still accepting null? Also, if I tried to insert null using,
insert into student(Name,IdNumber) values('robert',3);
the query is not demanding a not null value for location. 'Not Null' constraing is only working when I use the query,
insert into student (Name,IdNumber,location) values('john',8,null);
Please tell my why 'not null' constraint is not working in the other cases described above.
Thanks,
Vivek G
This question already has answers here:
How to get the next auto-increment id in mysql
(21 answers)
Closed 9 years ago.
I am using MySQL.
I want to retrieve the next value that the AUTO_INCREMENT column will take without entering a new record.
create table ABC(id int(10) NOT NULL AUTO_INCREMENT,name char(10));
In oracle I would have used sequencename.nextval(); But what to I use in MySQL?
Here is why I did not use
select max(id) from ABC;
Suppose I have an entry with id=2. Now column id will take the next value as 3.
Before I create a record with id=3, If I delete the record with id=2.
The answer for query I mentioned will be 2. But I want the actual value 3, which the auto_increment column will anyway take.
Query table status like this:
SHOW TABLE STATUS WHERE `Name` = 'table_name'
Now in result you will get a column named Auto_increment. This is the value You were asking for.
In JAVA:
conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
stmt = conn.createStatement();
rs = stmt.executeQuery("SHOW TABLE STATUS WHERE `Name` = 'table_name'");
rs.next();
String nextid = rs.getString("Auto_increment");
Full example here: http://www.avajava.com/tutorials/lessons/how-do-i-use-jdbc-to-query-a-mysql-database.html
If I understand correctly,you could use the number of rows as indicator:
SELECT TABLE_ROWS+1
FROM information_schema.tables
WHERE table_name='tableName'
AND table_schema = DATABASE();
There is no way to guarantee what value you are going to get before inserting the row. This is mostly because you will have to lock the entire table to guarantee no other thread will do an insert with "your" next value.
You can reserve a value by starting a transaction, inserting a row, getting the value and then doing a rollback. Then you can safely use that value.
It will be much simpler to just insert the row, so maybe I'm not understanding the purpose of what you are doing.
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