在idea里运行自己写的学生表sql脚本,为什么为失败?

在idea里运行自己写的学生表sql脚本,为什么为失败?

文件是以.sql结尾


http://img1.sycdn.imooc.com//climg/60b346ad097d63f504410576.jpg

错误提示:

[2021-05-30 15:59:24] Connecting to @localhost...

[2021-05-30 15:59:24] Using batch mode (1000 insert/update/delete statements max)

[2021-05-30 15:59:24] Run C:\Users\User\Desktop\MyBatis源代码\Student.sql

CREATE TABLE student(

id int NOT NULL PRIMARY KEY AUTO_INCREMENT,

reg_no int NOT NULL,

name VARCHAR(20) NOT NULL,

sex VARCHAR(20),

age int NOT NULL,

grade VARCHAR(20) NOT NULL,

major VARCHAR(50) NOT NULL

)

[2021-05-30 15:59:24] [3D000][1046] No database selected

INSERT INTO student values (null,20171201,"梅长苏","男",31,"2008","哲学系")

.

[2021-05-30 15:59:24] [3D000][1046] No database selected

[2021-05-30 15:59:24] Summary: 8 of 8 statements executed, 8 failed in 14 ms (683 symbols in file)


sql文件代码:

CREATE TABLE student(

id int NOT NULL PRIMARY KEY AUTO_INCREMENT,

reg_no int NOT NULL,

name VARCHAR(20) NOT NULL,

sex VARCHAR(20),

age int NOT NULL,

grade VARCHAR(20) NOT NULL,

major VARCHAR(50) NOT NULL

);

INSERT INTO student values (null,20171201,"梅长苏","男",31,"2008","哲学系");

INSERT INTO student values (null,20171202,"小明","男",30,"2008","政治系");

INSERT INTO student values (null,20171203,"kate","女",31,"2016","人体系");

INSERT INTO student values (null,20171204,"yap","男",31,"2008","体育系");

INSERT INTO student values (null,20171205,"Dan","男",31,"2008","哲学系");

INSERT INTO student values (null,20171206,"Tom","男",31,"2008","哲学系");

INSERT INTO student values (null,20171207,"Jack","男",31,"2008","通信专业");


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿满 2021-05-31 10:10:34

同学你好,按照如下顺序执行代码,如:

http://img1.sycdn.imooc.com//climg/60b444a309785a0706640356.jpg

先查看所有数据库,如果没有student数据库,则先创建student数据库。存在后,则使用use  student使用数据库。最后执行建表语句,插入数据等。

在同学的截图中,只看到了建表语句,建议同学使用use 数据库名;先选中某个数据库,然后再执行建表语句。

祝学习愉快~

好帮手慕阿满 2021-05-30 17:17:36

同学你好,报错提示没有选择数据库,如:

http://img1.sycdn.imooc.com//climg/60b357d4099605c309490270.jpg

建议同学可以在运行代码先选择某个数据库,然后再运行试试。

选择数据库的语句:

use  数据库名

祝学习愉快~

  • 提问者 慕设计7678942 #1

    加了之后,出现新错误了提示找不到这个database

    http://img1.sycdn.imooc.com//climg/60b35b40099f25dd10480472.jpg

    USE student;

    CREATE TABLE student(

    id int NOT NULL PRIMARY KEY AUTO_INCREMENT,

    reg_no int NOT NULL,

    name VARCHAR(20) NOT NULL,

    sex VARCHAR(20),

    age int NOT NULL,

    grade VARCHAR(20) NOT NULL,

    major VARCHAR(50) NOT NULL

    );

    INSERT INTO student values (null,20171201,"梅长苏","男",31,"2008","哲学系");

    INSERT INTO student values (null,20171202,"小明","男",30,"2008","政治系");

    INSERT INTO student values (null,20171203,"kate","女",31,"2016","人体系");

    INSERT INTO student values (null,20171204,"yap","男",31,"2008","体育系");

    INSERT INTO student values (null,20171205,"Dan","男",31,"2008","哲学系");

    INSERT INTO student values (null,20171206,"Tom","男",31,"2008","哲学系");

    INSERT INTO student values (null,20171207,"Jack","男",31,"2008","通信专业");


    2021-05-30 17:30:47
  • 好帮手慕阿满 回复 提问者 慕设计7678942 #2

    同学你好,问一下同学数据库中有student这个数据库吗?同学可以使用show databases;查看一下。如果没有,建议同学先使用create  database student;创建数据库,然后再use student;使用数据库,最后创建表,添加数据库等。

    祝学习愉快~

    2021-05-30 18:17:16
  • 提问者 慕设计7678942 回复 好帮手慕阿满 #3

    按照思路 我一步一步去试过了,还是不行,我直接在console控制台直接先创建table,还是提示我需要先选择database. 表都还没建,我怎么选

    http://img1.sycdn.imooc.com//climg/60b38081092922b507950614.jpg

    2021-05-30 20:10:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师