简体   繁体   中英

How can I alter a column in my table that has a relationship with another table using SQL?

I have three tables in my database: Book, Library, and AvailableBooks. The Book table has a primary key called "Book_ID" and similarly the Library has a "Library_ID" primary key. These two fields have the property AutoNumber and I'd like to reset the field when there are no records in the table when the application starts. I do this by:

internal void alterBookTable() {
        if (isBookTableInitialized) return;
        List<Book> booksFromDb = getAllBooks();
        if (booksFromDb.Count > 0) return;
        OleDbCommand alterTablesCommand = new OleDbCommand("ALTER TABLE Book ALTER COLUMN Book_ID AUTOINCREMENT(100,1)", DbConnection);
        alterTablesCommand.ExecuteNonQuery();
        isBookTableInitialized = true;
}

The Library table is altered a similar way except the altersTableCommad is:

 OleDbCommand alterTablesCommand = new OleDbCommand("ALTER TABLE Library ALTER COLUMN Library_ID AUTOINCREMENT(300,1)", DbConnection);

The "Book_ID" field and the "Library_ID" field are both in the AvailableBooks table. There is a one-to-many relationship between the "Book_ID" in the Book table and the "Book_ID" in the AvailableBooks table and similarly with the "Library_ID".

When I run this application, I get "System.Data.OleDb.OleDbException: Cannot change field 'Book_ID'. It is part of one or more relationships"

Is there a way to keep this same relationship but alter the field so that it is consistent with all the tables?

The following code seems to do what you desire:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace oleDbTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string myConnectionString;
            myConnectionString =
                    @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                    @"Data Source=C:\__tmp\books.accdb;";

            using (var con = new OleDbConnection())
            {
                con.ConnectionString = myConnectionString;
                con.Open();

                using (var cmd = new OleDbCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = 
                            "ALTER TABLE AvailableBooks " +
                            "DROP CONSTRAINT BookAvailableBooks";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText =
                            "ALTER TABLE AvailableBooks " +
                            "DROP CONSTRAINT LibraryAvailableBooks";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText =
                            "ALTER TABLE Book " +
                            "ALTER COLUMN Book_ID AUTOINCREMENT(100,1)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText =
                            "ALTER TABLE Library " +
                            "ALTER COLUMN Library_ID AUTOINCREMENT(100,1)";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText =
                            "ALTER TABLE AvailableBooks " +
                            "ADD CONSTRAINT BookAvailableBooks " +
                                "FOREIGN KEY (Book_ID) REFERENCES Book";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText =
                            "ALTER TABLE AvailableBooks " +
                            "ADD CONSTRAINT LibraryAvailableBooks " +
                                "FOREIGN KEY (Library_ID) REFERENCES Library";
                    cmd.ExecuteNonQuery();
                }
                con.Close();
            }
        }
    }
}

Your question says "MSAccess" but it looks like you're using C#. If you want to do it in MSAccess, you need to drop the table, re-create the table and then re-create the relationship. This Microsoft website has some code that you might want to take a look at.

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