简体   繁体   English

mysql表默认公式

[英]mysql table default formula

In mysql (Ubuntu 13.10, MySql 5.5) I'm trying to create a table that will automatically create a random alphanumeric ID with this code: 在mysql(Ubuntu 13.10,MySql 5.5)中,我正在尝试创建一个表,该表将使用以下代码自动创建随机字母数字ID:

create table YGraph (
    YGraphEdgeId CHAR(8) NOT NULL PRIMARY KEY DEFAULT SUBSTRING(MD5(RAND()) FROM 1 FOR 8),
    YGraphStartVertex CHAR(6) NOT NULL,
    YGraphEndVertex CHAR(6) NOT NULL
);

but phpmyadmin is complaining: 但phpmyadmin抱怨:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUBSTRING(MD5(RAND()) FROM 1 FOR 8), YGraphStartVertex CHAR(6) NOT NULL, ' at line 2

. . . and putting quotes around the formula only give a generic 并在公式周围加上引号只给出一个通用的

#1067 - Invalid default value for 'YGraphEdgeId'

I simply want a new record creation to run the default formula and stick an 8-place random alphanum in the YGraphEdgeId field. 我只想创建一个新记录来运行默认公式,并在YGraphEdgeId字段中粘贴一个8位随机字母。 I seem to remember this formula working in an INSERT. 我好像记得这个公式在INSERT中工作。 What am I doing wrong? 我究竟做错了什么?

目前您无法使用函数为列提供默认值,请参阅https://dev.mysql.com/doc/refman/5.5/en/data-type-defaults.html

As per MySQL Documentation on Data Type Default Values 根据数据类型默认值的MySQL文档

...the default value must be a constant; ...默认值必须是常量; it cannot be a function or an expression.... 它不能是一个功能或表达....

Hence you can't use functions or expressions there. 因此,你不能在那里使用函数或表达式。

Secondly, you can assign a default string value but it should abide by the length of the column data-type used. 其次,您可以指定默认字符串值,但它应该遵守所使用的列数据类型的长度。 String used to define default value was more that 8 characters. 用于定义默认值的字符串多于8字符。 And hence the error thrown. 因此错误抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM