MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

MySQLで負の数を防ぎますか?


MySQLで負の数を防ぐには、INTUNSIGNEDを使用する必要があります。たとえば、ここにUserGameScoresなどのintとして列を持つテーブルを作成したとします

mysql> create table preventNegativeNumberDemo
   - > ( 
   - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   - > UserName varchar(20),
   - > UserGameScores int
   - > );
Query OK, 0 rows affected (1.74 sec)
>

ここで、負の数を防ぐ必要がある場合は、同じ列をINT UNSIGNED

で変更します。
mysql> alter table preventNegativeNumberDemo modify column UserGameScores INT UNSIGNED NOT NULL;
Query OK, 0 rows affected (3.32 sec)
Records: 0 Duplicates: 0 Warnings: 0

次に、テーブルの説明をもう一度確認します。

クエリは次のとおりです

mysql> desc preventNegativeNumberDemo;

以下は出力です

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| UserId         | int(11)          | NO   | PRI | NULL    | auto_increment |
| UserName       | varchar(20)      | YES  |     | NULL    |                |
| UserGameScores | int(10) unsigned | NO   |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

ここで、UserGameScores列に負の数を挿入しようとすると、INT UNSIGNEDとして設定されているため、MySQLはエラーを返します。負の数を含むいくつかの値を挿入しましょう

mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Larry',0);
Query OK, 1 row affected (1.20 sec)
mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Mike',-1);
ERROR 1264 (22003): Out of range value for column 'UserGameScores' at row 1
mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Sam',-100);
ERROR 1264 (22003): Out of range value for column 'UserGameScores' at row 1
mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('John',100);
Query OK, 1 row affected (0.84 sec)
mysql> insert into preventNegativeNumberDemo(UserName,UserGameScores) values('Bob',200);
Query OK, 1 row affected (0.48 sec)

負の値を挿入しようとすると、上記のエラーを確認してください。

ここで、selectステートメントを使用してテーブルのすべてのレコードを表示します。正の数のみが挿入されます

mysql> select *from preventNegativeNumberDemo;

以下は出力です

+--------+----------+----------------+
| UserId | UserName | UserGameScores |
+--------+----------+----------------+
|      1 | Larry    |              0 |
|      2 | John     |            100 |
|      3 | Bob      |            200 |
+--------+----------+----------------+
3 rows in set (0.00 sec)

  1. MySQLの英数字文字列から数字のみを並べ替えますか?

    英数字の文字列から数字のみを並べ替えるには、ORDER BY RIGHT()を使用します。まずテーブルを作成しましょう- mysql> create table DemoTable1948    (    StudentCode varchar(20)    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1948 values('121John_567

  2. Cの負の数のモジュラス

    ここでは、モジュラスを取得するために負の数を使用した場合の結果を確認します。アイデアを得るために、次のプログラムとその出力を見てみましょう。 例 #include<stdio.h> int main() {    int a = 7, b = -10, c = 2;    printf("Result: %d", a % b / c); } 出力 Result: 3 ここで、%と/の優先順位は同じです。したがって、%は最初は機能しているので、%bは7を生成し、cで除算した後、3を生成します。ここで%bの場合、左オペランド