Query to toggle a Boolean value in MySQL [duplicate] - mysql

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a way in MySQL to reverse a Boolean field with one query?
To update a (Boolean) value, normally we would check if it's set to false or true, and update it. Is there a query that would toggle a Boolean value?

UPDATE mytbl
SET field = !field
WHERE id = 42
Where 42 is the id of the record, field is the name of the Boolean field and mytbl is the table name.

You can use a Boolean operator for this. Here delete is your Boolean field.
update tab set `delete`=NOT `delete`

Related

Column 'name' cannot be null [duplicate]

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.

How to add new value to existing table column value [duplicate]

This question already has answers here:
Concatenate string with field value in MySQL [duplicate]
(4 answers)
Closed 6 years ago.
I know this can update a table column to a new value from the previous value UPDATE table SET column=column+2 but this only happen to an integer value, I want to update a text value like this
// if the table column value is = "james,john,peter"
$new = ",andrew";
mysqli_query($con, "UPDATE table SET column=column+'".$new."');
so the new table column value will now be james,john,peter,andrew. I have searched even stackoverflow but all answers I got is for integer value. Please anyone with an idea?
Use the concat() function:
UPDATE table set column=concat(column, ',andrew');

Find next value of AUTO_INCREMENT column in MySQL [duplicate]

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.

Get column values of the updated row [duplicate]

This question already has answers here:
Get Updated Value in MySQL instead of affected rows
(5 answers)
Closed 9 years ago.
$query = "UPDATE transaction SET c_status = :status WHERE c_name = :name AND c_id = :id";
$stmt = $this->handle->prepare($query);
$stmt->bindParam(':c_status',$status,PDO::PARAM_STR);
$stmt->bindParam(':c_name',$name,PDO::PARAM_STR);
$stmt->bindParam(':c_id',$id,PDO::PARAM_STR);
return $stmt->execute();
Using the above syntax, I am able to update a record in the transaction table. However, what I only get with the return is a boolean. I want to know if there is a way I can get the transaction_id (the AUTO_INCREMENT field in the transaction table, c_id and c_name where just a column of that) and the rest of its columns?
This question is not related to PDO but to mysql in general.
UPDATE queries are not intended to return anything. To get a row from database you have to use SELECT query.

MySQL Error in Data definition language [duplicate]

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