Er “stig” dálkurinn örugglega skilgreindur sem int ?
mysql> create table stig(
-> `user_id` int(1) NOT NULL AUTO_INCREMENT,
-> `username` varchar(12) NOT NULL,
-> `stig` varchar(10) NOT NULL,
-> unique(`user_id`)
-> );
Query OK, 0 rows affected (0.07 sec)
mysql> insert into stig (username, stig) VALUES ('haddi', '10');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stig (username, stig) VALUES ('kulp', '1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stig (username, stig) VALUES ('rusty', '5');
Query OK, 1 row affected (0.00 sec)
mysql> insert into stig (username, stig) VALUES ('kulpp', '8');
mysql> desc stig;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| user_id | int(1) | | PRI | NULL | auto_increment |
| username | varchar(12) | | | | |
| stig | varchar(10) | | | | |
+----------+-------------+------+-----+---------+----------------+
mysql> select * from stig order by stig asc limit 5;
+---------+----------+------+
| user_id | username | stig |
+---------+----------+------+
| 2 | kulp | 1 |
| 1 | haddi | 10 |
| 3 | rusty | 5 |
| 4 | kulpp | 8 |
+---------+----------+------+
mysql> select * from stig order by stig desc limit 5;
+---------+----------+------+
| user_id | username | stig |
+---------+----------+------+
| 4 | kulpp | 8 |
| 3 | rusty | 5 |
| 1 | haddi | 10 |
| 2 | kulp | 1 |
+---------+----------+------+
4 rows in set (0.00 sec)
prófum að breyta stig í int, athugum hvort það breyti einhverju :)
mysql> alter table stig change stig stig integer;
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> desc stig;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| user_id | int(1) | | PRI | NULL | auto_increment |
| username | varchar(12) | | | | |
| stig | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
Snilld, að vísu fullmikið í int() en jæja :)
mysql> select * from stig order by stig asc limit 5;
+---------+----------+------+
| user_id | username | stig |
+---------+----------+------+
| 2 | kulp | 1 |
| 3 | rusty | 5 |
| 4 | kulpp | 8 |
| 1 | haddi | 10 |
+---------+----------+------+
4 rows in set (0.00 sec)
mysql> select * from stig order by stig desc limit 5;
+---------+----------+------+
| user_id | username | stig |
+---------+----------+------+
| 1 | haddi | 10 |
| 4 | kulpp | 8 |
| 3 | rusty | 5 |
| 2 | kulp | 1 |
+---------+----------+------+
4 rows in set (0.00 sec)
Leyfi þér að túlka niðurstöðurnar =)