MySQL工作台数据导入,外键错误

时间:2018-11-27 17:03:46

标签: mysql mysql-workbench

我是在mySQL Workbench 8.0CE中工作的,我创建了两个表,一个表用于人,另一个表用于人,我试图导出数据,但是它会抛出错误

public partial class Form1 : Form
{
    SqlConnection con = new SqlConnection("Data Source=LAPTOP\\SQLEXPRESS02;Initial Catalog=Greenwich_Butcher;Integrated Security=True");

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {


        string dat = "Insert into [CustomerDetails](CustomerID, CustomerName, CustomerAddress, CustomerAddedDate) Values('" + Convert.ToInt32(textBox1.Text) + "','" + textBox2.Text + "','" + textBox3.Text + "','" + Convert.ToString(dateTimePicker1.Text) + "')";
        SqlCommand com = new SqlCommand(dat, con);
        con.Open();
        int rowsAffected = com.ExecuteNonQuery();
        if (rowsAffected > 0)
        {

            MessageBox.Show("Record inserted succesfully");
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            dateTimePicker1.Text = "";
        }
        else
        {
            MessageBox.Show("Record not inserted succesfully");
        }
        con.Close();

    }
    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

这是sql代码

ERROR 1064 (42000) at line 67: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
  CONSTRAINT `fk_PersonaDireccion`
    FOREIGN KEY (`idPersona`)
    REFERENCES' at line 8

Operation failed with exitcode 1

接下来我附加EER图img EER Diagram

注意:因为我删除了外键连接,所以“ Personas” ID字段终于出现了。

1 个答案:

答案 0 :(得分:0)

我以某种方式解决了这个问题,在模型选项中未选中用户默认的全局设置,并且

INDEX `fk_PersonaDireccion_idx` (`idPersona` ASC) VISIBLE

消失了,现在的“ direccion”表变成了这样

CREATE TABLE IF NOT EXISTS `dinSchema`.`direccion` (
  `pais` VARCHAR(6) NOT NULL DEFAULT 'México',
  `estado` VARCHAR(20) NOT NULL,
  `ciudad` VARCHAR(25) NOT NULL,
  `direccion` VARCHAR(150) NOT NULL,
  `cp` INT(8) NOT NULL,
  `idPersona` INT NOT NULL,
  CONSTRAINT `fk_PersonaDireccion`
    FOREIGN KEY (`idPersona`)
    REFERENCES `dinSchema`.`Personas` (`idPersonas`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE)
ENGINE = InnoDB;