A
A
Artqookie2011-01-30 15:09:50
MySQL
Artqookie, 2011-01-30 15:09:50

Field types in MySQL?

What types would be ideologically correct for each of the fields and why? I just started to study databases, so the question is lamer, but necessary in the future. Fields:

  • user_id
  • username
  • user_pass
  • user_invite
  • user_reg_flag
  • user_mail
  • user_course

Answer the question

In order to leave comments, you need to log in

8 answer(s)
L
L0NGMAN, 2011-01-30
@Artqookie

CREATE TABLE `users` (
​​`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_name` char(50) NOT NULL DEFAULT '',
`user_pass` char(32) NOT NULL DEFAULT '',
`user_mail` char(50 ) NOT NULL DEFAULT '',
`user_invite` tinyint(1) NOT NULL DEFAULT 0,
`user_created` TIMESTAMP DEFAULT '0000-00-00 00:00:00' NOT NULL,
`user_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`user_id`),
UNIQUE KEY (`user_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

V
Vladimir Chernyshev, 2011-01-30
@VolCh

Under name, using 25 in the case of utf-8 is not the best idea, even if you limit yourself to Russian, 50 may also not be enough for soap in perverted cases, but the corresponding rfc (domains.rf for example :)). And in general, there is no overhead between varchar(1) and varchar(256), I don’t see any point in saving, if there are restrictions in the requirements, then check them in the application and precisely for the number of characters, not bytes (for php - mb_strlen(), not strlen() ).

N
niko83, 2011-01-30
@niko83

CREATE TABLE `users` (
​​`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL DEFAULT '',
`pass` varchar(32) NOT NULL DEFAULT '',
`mail` varchar(50 ) NOT NULL DEFAULT '',
`invite` tinyint(1) NOT NULL DEFAULT 0,
`created` TIMESTAMP DEFAULT '0000-00-00 00:00:00' NOT NULL,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

C
Chvanikoff, 2011-01-30
@Chvanikoff

In general, if you immediately think about good architecture, then this should not be one table.
Table1: id, login, password
Table2: user
data I didn’t understand the meaning of the invite field, but if you mean “referral program”, or its likeness, it’s also a separate table with the data “user id”, “referrer”
and instead of user_reg_flag do a table of user roles in the system (you still, most likely, will have an administrator?) - to begin with, “login” and “admin” and link them through a linking table of users with roles so that everyone can have several. Then, when confirming registration, for example, we add the “login” role to the user, if we want to ban (prohibit entry), we remove it.
If interested, I can write the structure of these tables.
PS: as for the subject itself, I don’t see the point in optimizing the fields without optimizing the database structure.

S
SwampRunner, 2011-01-30
@SwampRunner

id - int 10 unsigned
name - varchar 25
pass - varchar 32 (after md5)
email - varchar 50
if you need a bool, then tinyint 1

D
DeusModus, 2011-01-30
@DeusModus

sqlyog has a great table parser. You create a table mindlessly, fill it with data, then analyze it and sqlyog will suggest the optimal field types for you. It will not relieve you of the need to deal with the DBMS, but it can solve your problem in the short term.

C
charon, 2011-02-02
@charon

in general: use a fixed format (char instead of varchar) of the minimum required length. You can also take into account the features of the architecture (32-bit server: id is an unsigned integer with a size of 32 bits).

M
mitnlag, 2011-02-02
@mitnlag

I would say that habr is not a place for solving household problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question