【问题标题】:Create and insert table创建和插入表
【发布时间】:2019-05-02 14:54:11
【问题描述】:

我为数据库创建了一个表并创建了表,但插入值不起作用。

这是桌子

Create table patient (
Patient_ID Number(9) primary key,
First_name varchar2(15),
Last_name varchar2(10),
Contact number(10),
City varchar2(20),
Doctor_ID Number(9) references Doctor(Doctor_ID));

这是插入语句

insert into patient values ('21345', 'John', 'Smith', '111-111-1111', 'NJ');   
insert into patient values ('21346', 'Emily', 'Rose', '222-222-2222', 'LA');  
insert into patient values ('21347', 'Mark', 'Cruise', '333-333-3333', 'NY');
insert into patient values ('21348', 'Bran', 'Stark', '444-444-4444', 'TX');    
insert into patient values ('21349', 'Hailey', 'Wraith', '555-555-5555', 'AZ');

我收到一条错误消息,提示值不足。

【问题讨论】:

    标签: oracle sql-insert


    【解决方案1】:

    当您的表需要 6 个值(ParentID、First Name、Last Name、Contact、City 和 Doctor ID)时,您只插入 5 个值

    您需要传入 Doctor_ID 的值

    【讨论】:

    • 插入医生(Doctor_ID)值('30114'、'Mark'、'Miller'、'Therapist');
    • 把它扔进qith病人的价值观里?
    【解决方案2】:

    您在插入过程中忘记添加表列名称,因此它会尝试将您传递的所有数据添加到表中的每一列。当不需要您插入所有表列时,这是一种更好的输入数据的方法。

    Insert into `patient` (`table1`, `table2`, `table3`, `table4`, `table5`) values ('21345', 'John', 'Smith', '111-111-1111', 'NJ');
    

    为了进一步分解您的问题,您的表中有 6 列,但您传递的数据是 5,它会给您此错误,因为 5 小于 6。如果您不想收到此错误,请需要说明您输入的每一列,如上所示 - 但该字段需要是可为空的字段。

    在这种情况下,Doctor_ID 缺失

    Insert into `patient` (`table1`, `table2`, `table3`, `table4`, `table5`, `table6` ) values ('21345', 'John', 'Smith', '111-111-1111', 'NJ', 'DOCTOR_ID_DATA_HERE');
    

    回答:

    Insert into `patient` (`Patient_ID`, `First_name`, `Last_name`, `Contact`, `City`, `Doctor_ID`) values ('2125', 'John', 'Doe', '111-111-1111', 'LA', '30114');
    

    DOCTOR 30114 需要已经存在,因为您是从另一个表中引用的,请注意!

    【讨论】:

    • 插入医生(Doctor_ID)值('30114'、'Mark'、'Miller'、'Therapist');
    • 插入病人('Doctor', 'Patient', 'Appointment', 'Payment', 'Insurance', 'Bill')值('21345', 'John', 'Smith', '111-111-1111', 'NJ', '30114');
    • 那么,像第二个插入值一样?
    • 但是我尝试运行它,它给了我一个错误,说缺少 SELECT 关键字
    • 我已经编辑了我的答案尝试运行最后一条语句@Clark
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多