简体   繁体   中英

Update statement conflicted with foreign key

Here is my tables,i can update ad,soyad and email but when i try to update telefon it conflicts with the foreign key [userFk],i already have foreign key update on cascade so,i can't figure out the issue here.Thanks in advance

CREATE TABLE [dbo].[ogrenci] (
[ogrenciNo]  INT           NOT NULL,
[ad]         NVARCHAR (20) NOT NULL,
[soyad]      NVARCHAR (20) NOT NULL,
[email]      NVARCHAR (50) NOT NULL,
[fakulte_no] INT           NOT NULL,
[bolum_ad]   NVARCHAR (30) NOT NULL,
[bolum_no]   INT           DEFAULT ((1)) NOT NULL,
[telefon]    NVARCHAR (50) DEFAULT ((1)) NOT NULL,
PRIMARY KEY CLUSTERED ([ogrenciNo] ASC),
UNIQUE NONCLUSTERED ([ogrenciNo] ASC),
UNIQUE NONCLUSTERED ([email] ASC),
CONSTRAINT [bolumFk] FOREIGN KEY ([bolum_no]) REFERENCES [dbo].[bolum] ([bolumNo]) ON DELETE CASCADE,
CONSTRAINT [fakulteFk1] FOREIGN KEY ([fakulte_no]) REFERENCES [dbo].[fakulte] ([fakulteId]) ON DELETE CASCADE,
CONSTRAINT [userFk] FOREIGN KEY ([telefon]) REFERENCES [dbo].[loginusers] ([upassword]) ON DELETE CASCADE ON UPDATE CASCADE

);

and the second one,

CREATE TABLE [dbo].[loginusers] (
[username]  NVARCHAR (50) NOT NULL,
[upassword] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([upassword] ASC)

);

and here is the update button,

private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtOgrenciNo.Text.Length != 0 && txtAd.Text.Length != 0 && txtSoyad.Text.Length != 0 && txtEmail.Text.Length != 0 && txtTelefon.Text.Length != 0)
            {
                string query = "UPDATE ogrenci SET ogrenciNo=@ogrenciNoVal,ad=@adVal,soyad=@soyadVal,email=@emailVal,telefon=@telefonVal WHERE ogrenciNo=@ogrenciNoVal";
                string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE username=@telefonVal";
                using (connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                using (SqlCommand cmd = new SqlCommand(query1, connection))
                {
                    connection.Open();
                    command.Parameters.AddWithValue("@ogrenciNoVal", txtOgrenciNo.Text);
                    command.Parameters.AddWithValue("@adVal", txtAd.Text);
                    command.Parameters.AddWithValue("@soyadVal", txtSoyad.Text);
                    command.Parameters.AddWithValue("@emailVal", txtEmail.Text);
                    command.Parameters.AddWithValue("@telefonVal", txtTelefon.Text);
                    cmd.Parameters.AddWithValue("@emailVal", txtEmail.Text);
                    cmd.Parameters.AddWithValue("@telefonVal", txtTelefon.Text);
                    command.ExecuteNonQuery();
                    cmd.ExecuteNonQuery(); 
                    gridDoldur();
                }
            }
            else
            {
                MessageBox.Show("Öğrenci bilgileri boş girilemez.", "Bilgilendirme", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

As upassword column is primarykey in your loginusers table, if you want to update telefon on ogrenci with update cascade property , so you need to remove ,telefon=@telefonVal code from update ogrenci query, like this

string query = "UPDATE ogrenci SET ogrenciNo=@ogrenciNoVal,ad=@adVal,soyad=@soyadVal,email=@emailVal WHERE ogrenciNo=@ogrenciNoVal";

Your second query will update table ogrenci too

WARNING: It will not good if some student will think like this - "If my password is my telephone, so lets try login as another student with his/her telephone number as password and do something" :)

EDIT:

Your second query where clause is wrong I think,

string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE username=@telefonVal";

It should change to this

string query1 = "UPDATE loginusers SET username=@emailVal,upassword=@telefonVal WHERE upassword=@oldtelefonVal ";

要解决您的问题,您需要在loginusers中插入行(而不是update),之后需要更新表ogrenci,最后可以取消您的loginusers row。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM