如何应用条件' book ID,该书名由六个字母数字字符组成,并以B'开始。在SQL中创建表格?
我在SQL中的查询要创建“书”表(数据库):
CREATE TABLE book(
bookid VARCHAR(6) where bookid LIKE 'B_____',
bookTitle VARCHAR(50),
author VARCHAR(20),
genre VARCHAR(10) CHECK(genre IN('Mystery','Thriller')),
yearPublication INTEGER
);
当我运行此代码时,它会显示错误:
缺少右括号。
在第二行( bookid varchar(6)中,例如'b _____'
),当我删除条件()时(例如'b ______'
),该表是成功创建的,但是根据问题陈述条件必须在那里达到需求。执行后,请任何人提供此代码中的更正吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用检查约束,例如已经是您命令的一部分的限制。因此,您的Create Table命令将是:
如果要设置约束的名称(也针对流派约束),则可以使用:
如果表已经存在(也许是因为您执行了没有约束的Create Table命令),并且您需要要添加约束,您可以执行以下操作:
请注意:如果您确实需要对字母数字字符的限制而不是允许任何字符,则必须更改上面列出的命令。您可以使用这样的言论:
请参见此处正确工作:
You can use a check constraint like the constraint concerning the genre which is already part of your command. So your create table command will be:
If you want to set names for the constraints (also for the genre constraint), this will work:
If the table already exists (maybe because you executed the create table command without the constraint) and you need to add the constraint, you can do following:
Please note: If you really need a restriction for alphanumeric characters instead of allowing any characters, you have to change the commands listed above. You can use a regex like this:
Please see this is working correctly here: db<>fiddle
最好的是:
Better will be :