Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本MySQL查询_Mysql - Fatal编程技术网

基本MySQL查询

基本MySQL查询,mysql,Mysql,在MySQL中,我需要创建一个名为users的新表,其中包含以下字段: id – integer type (primary key) first_name – varchar type username – varchar type password – char type with length 40 (this is the length of a sha1 hash) type – varchar type (‘admin’ or ‘author’) 除此之外,一切都向前看 type

在MySQL中,我需要创建一个名为users的新表,其中包含以下字段:

id – integer type (primary key)
first_name – varchar type
username – varchar type
password – char type with length 40 (this is the length of a sha1 hash)
type – varchar type (‘admin’ or ‘author’) 
除此之外,一切都向前看

type – varchar type (‘admin’ or ‘author’) .
如何执行此操作?

您想使用MySQL的数据类型:

ENUM
是一个字符串对象,其值从表创建时列规范中显式枚举的允许值列表中选择

因此,在你的情况下:

`Type` ENUM('admin', 'author')

您应该使用枚举数据类型


如果您确定用户类型不会是除
admin
author
之外的任何其他类型,则可以使用
ENUM

`type` ENUM('admin', 'author') NOT NULL DEFAULT 'author'
但是,如果可能的用户类型需要扩展,或者需要在表单中列出不同的用户类型,则这可能会导致反模式。如果是这种情况,您可以添加另一个表:

user_types (type)
其中,
type
是一个
VARCHAR(20)
(应该足够长)。在
用户
表中,您创建了相同类型的字段,并向
用户类型
表中添加了外键



顺便说一句,不要存储密码的简单
SHA1()
,而是使用:)

枚举是列表的集合 比如数字,天数

ENUM('admin','author');

或对表用户类型的引用。
ENUM('admin','author');