79519308

Date: 2025-03-19 06:32:36
Score: 0.5
Natty:
Report link

Just create a Table with constraints: PRIMARY KEY, DEFAULT, CHECK



mysql> CREATE TABLE redis_connection(id INT PRIMARY KEY DEFAULT 1 CHECK (id = 1), connection BOOL);
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO redis_connection(connection) VALUES(1);
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM  redis_connection;
+----+------------+
| id | connection |
+----+------------+
|  1 |          1 |
+----+------------+
1 row in set (0.00 sec)

mysql> #Let's try insert a row again
mysql> INSERT INTO redis_connection(connection) VALUES(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'redis_connection.PRIMARY'
mysql> #You can just update the value of connection to either 0 or 1
mysql> UPDATE redis_connection SET connection = 0 WHERE id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM  redis_connection;
+----+------------+
| id | connection |
+----+------------+
|  1 |          0 |
+----+------------+
1 row in set (0.00 sec)

mysql> #Happy Learning!!

If this solution solved your problem, do give a upvote. Thanks!!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (0.5): upvote
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Prakash J