gpt4 book ai didi

sql - 将日期插入SQLite表

转载 作者:行者123 更新时间:2023-12-03 19:16:11 25 4
gpt4 key购买 nike

我是SQL的新手,我想创建一个包含学生生日的学生表。这是我的代码。我在Visual Studio Code上运行SQLite

create table student
(std_code varchar(8),
std_fname varchar(15) constraint student_std_lname_nn not null,
std_lname varchar(15) constraint student_std_fname_nn not null,
std_gend varchar(8),
maj_code varchar(10) constraint student_maj_code_fk references
major (maj_code),
std_dob date,
constraint student_std_code_pk primary key (std_code));

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC', date(1962-
03-10));
insert into student values ('S02', 'Charles', 'Barkley', 'M', null, date(1964-
09-12));


该代码运行没有错误,但是当我运行 SELECT * FROM student时,日期看起来都错了。 enter image description here

任何帮助解决该问题的帮助将不胜感激。谢谢

最佳答案

您需要使用单引号'包含日期字符串。


日期(时间字符串,修饰符,修饰符,...)


因此,您需要传递DateTime字符串作为参数。

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC',
'1962-03-10');
insert into student values ('S02', 'Charles', 'Barkley', 'M', null,
'1964-09-12');


或只使用 Date字符串

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC',
'1962-03-10');
insert into student values ('S02', 'Charles', 'Barkley', 'M', null,
'1964-09-12');


查询#1

select * from student;

| std_code | std_fname | std_lname | std_gend | maj_code | std_dob |
| -------- | --------- | --------- | -------- | -------- | ---------- |
| S01 | Michael | Jordan | M | FINC | 1962-03-10 |
| S02 | Charles | Barkley | M | | 1964-09-12 |




View on DB Fiddle

关于sql - 将日期插入SQLite表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52575502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com