如何应用条件' book ID,该书名由六个字母数字字符组成,并以B'开始。在SQL中创建表格?

发布于 2025-01-28 03:20:21 字数 455 浏览 2 评论 0 原文

我在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 ______'),该表是成功创建的,但是根据问题陈述条件必须在那里达到需求。执行后,请任何人提供此代码中的更正吗?

My query in SQL to create 'book' table(database):

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
);

When I ran this code it shows error:

missing right parenthesis.

On second line (bookid VARCHAR(6) where LIKE 'B_____'), when I remove the condition (where LIKE 'B_____') the table is successfully created but according to problem statement the condition must be there to achieve the demands. Could please anyone provide the correction in this code after executing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2025-02-04 03:20:21

您可以使用检查约束,例如已经是您命令的一部分的限制。因此,您的Create Table命令将是:

CREATE TABLE book(bookid VARCHAR(6) 
CHECK (bookid LIKE 'B_____'), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CHECK(genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

如果要设置约束的名称(也针对流派约束),则可以使用:

CREATE TABLE book(bookid VARCHAR(6)
CONSTRAINT chkBookId CHECK (bookid LIKE 'B_____'), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CONSTRAINT chkGenre CHECK (genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

如果表已经存在(也许是因为您执行了没有约束的Create Table命令),并且您需要要添加约束,您可以执行以下操作:

ALTER TABLE book 
ADD CONSTRAINT chkBookId
CHECK (bookid LIKE 'B_____');

请注意:如果您确实需要对字母数字字符的限制而不是允许任何字符,则必须更改上面列出的命令。您可以使用这样的言论:

CREATE TABLE book(bookid VARCHAR(6) 
CONSTRAINT chkBookId CHECK (bookid LIKE 'B_____' 
AND PATINDEX('%[^a-zA-Z0-9 ]%',bookid) = 0), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CONSTRAINT chkGenre CHECK(genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

请参见此处正确工作:

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:

CREATE TABLE book(bookid VARCHAR(6) 
CHECK (bookid LIKE 'B_____'), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CHECK(genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

If you want to set names for the constraints (also for the genre constraint), this will work:

CREATE TABLE book(bookid VARCHAR(6)
CONSTRAINT chkBookId CHECK (bookid LIKE 'B_____'), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CONSTRAINT chkGenre CHECK (genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

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:

ALTER TABLE book 
ADD CONSTRAINT chkBookId
CHECK (bookid LIKE 'B_____');

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:

CREATE TABLE book(bookid VARCHAR(6) 
CONSTRAINT chkBookId CHECK (bookid LIKE 'B_____' 
AND PATINDEX('%[^a-zA-Z0-9 ]%',bookid) = 0), 
bookTitle VARCHAR(50), 
author VARCHAR(20), 
genre VARCHAR(10) 
CONSTRAINT chkGenre CHECK(genre IN('Mystery','Thriller')), 
yearPublication INTEGER);

Please see this is working correctly here: db<>fiddle

对你的占有欲 2025-02-04 03:20:21

最好的是:

CREATE TABLE book(
bookid VARCHAR(6) CHECK (bookid LIKE 'B' + REPLICATE([A-Z0-9], 5)) ,
...
)
  • 如果您只想字母丢弃“ 0-9”部分,则
  • 如果您不想要变性字符(口音,结扎等等...)检查使用AI整理的约束定义,例如Latin1_general_ci_as
  • ,如果您只需要大写字母,请使用latin1_general_cs_ai之类的CS collat​​ions(
  • 如果您想要的(不使用diacrictics and Capitals),请使用CS_AS Collat​​ration
  • ,如果只需要较低的字符替换[A-Z0-9]由[A-Z0-9]与CS排序

Better will be :

CREATE TABLE book(
bookid VARCHAR(6) CHECK (bookid LIKE 'B' + REPLICATE([A-Z0-9], 5)) ,
...
)
  • If you want only letters drop the "0-9" part of the like
  • If you do not want diacritic characters (accents, ligature and so on...) just add a COLLATE operator after the "bookid" in the CHECK constraint definition with a AI collation like Latin1_General_CI_AS
  • If you want only capital letters, use a CS collations like Latin1_General_CS_AI
  • If you want both (no diacrictics and capitals) use a CS_AS collation
  • If you want only lower characters replace [A-Z0-9] by [a-z0-9] with CS collation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文