CodeIgniter Weird Sessions Table
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text DEFAULT '' NOT NULL,
PRIMARY KEY (session_id)
);
Now, I have a few questions:
Each session_id has a length of 32 characters but in the table we have varchar(开发者_JS百科40). Why varchar(40) and not char(32)?
An ip_address like 182.111.112.113 has a length of 15 characters but in the table we have varchar(16). Why varchar(16) and not varchar(15)?
Each user_agent has a length of 50 characters but in the table we have varchar(50). Why varchar(50) and not char(50)?
Can someone answer me at this questions? Please!
NOTE: My default storage engine is InnoDB.
You don't need to worry about that at all. Take a look through the MySQL documentation for data types:
http://dev.mysql.com/doc/refman/5.0/en/char.html
To sum it up, varchar expands to what is needed up to the specified limit. So if you only use 50 chars then it will only take 50 chars in the database not 60. So it's probably just the developer wanting pretty even numbers.
They probably chose to use 40 characters instead of 32, just to have their backs covered in case in the future some standards changed or they modified the way they coded id_session. Varchar instead of char... Well, char adds blank spaces at the end (it's a fixed length type, http://dev.mysql.com/doc/refman/5.0/en/char.html) to exactly fit 40 bytes. When you retrieve it, it comes back to 32 bytes, but I suppose they use Varchar to save storage space.
I have no idea. Sorry!
Maybe to use varchar as a standard? I don't know, but in my case, I tend to methodically use varchar instead of char.
Bear in mind all of my answers are just suppositions and I'm not clearly sure of any of them (especially the second one xD).
Best regards
you can change it to varchar(32)
for IPV6
user_agent is a var so change it to varchar(255)
精彩评论