1

I'm a newbie in Database. I'm trying to create some tables but it fails with error "near 'INDEX' : syntax error". Here is my codes to generate tables :

        try {
        using( SqliteCommand cmd = _db.CreateCommand() ) {

            // create words table
            cmd.CommandText = "DROP TABLE words";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS words(" +
                    "word_id INTEGER AUTO_INCREMENT," +
                    "word_eng VARCHAR(128)," +
                    "PRIMARY KEY(word_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create word_classes table
            cmd.CommandText = "DROP TABLE word_classes";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS word_classes(" +
                    "word_class_id INTEGER," +
                    "word_class_name VARCHAR(50)," +
                    "PRIMARY KEY(word_class_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create meanings table
            cmd.CommandText = "DROP TABLE meanings";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS meanings(" +
                    "meaning_id INTEGER AUTO_INCREMENT," +
                    "meaning VARCHAR(100)," +
                    "word_class_id INTEGER," +
                    "PRIMARY KEY(meaning_id)," +
                    "FOREIGN KEY(word_class_id) REFERENCES word_classes(word_class_id)" + 
                    ")";
            cmd.ExecuteNonQuery();

            // create word_meaning_realationship table
            cmd.CommandText = "DROP TABLE word_meaning_realationship";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS word_meaning_realationship(" +
                    "word_id INTEGER," +
                    "meaning_id INTEGER," +
                    "FOREIGN KEY(word_id) REFERENCES words(word_id)" +
                    "FOREIGN KEY(meaning_id) REFERENCES meanings(meaning_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create decks table
            cmd.CommandText = "DROP TABLE decks";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS decks(" +
                    "deck_id INTEGER AUTO_INCREMENT," +
                    "deck_name VARCHAR(128)," +
                    "deck_version INTEGER," +
                    "PRIMARY KEY(deck_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create deck_word_relationship table
            cmd.CommandText = "DROP TABLE deck_word_relationship";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS deck_word_relationship(" +
                    "deck_id INTEGER," +
                    "word_id INTEGER," +
                    "FOREIGN KEY(deck_id) REFERENCES decks(deck_id)," +
                    "FOREIGN KEY(word_id) REFERENCES words(word_id)" +
                    ")";
            cmd.ExecuteNonQuery();
        }
    } catch( SqliteException ex ) {
        Debug.LogError( "DBError : " + ex.ToString() );
    }

It would be very appreciated if you check my other codes to create table, as I'm a totally newbie in database and mysql, and am not very confident of my codes :)

** updated ** I decided not to think about indexing for now. I updated my codes that have small errors that made my code not working. But if you tell me how to apply indexing for me, I will make it as a correct answer after application. Thanks for helps of you guys.

7
  • what is your database? Sqlite or mysql? Commented Feb 9, 2014 at 11:53
  • My database is Sqlite. Commented Feb 9, 2014 at 12:27
  • then why why tagged as mysql? Commented Feb 9, 2014 at 12:58
  • I thought they are the same;aren't they? Isn't Sqlite based on mysql? Commented Feb 9, 2014 at 13:36
  • if you think so, run the statement on ide like sqliteadmin. there are many syntax changes between mysql and Sqlite Commented Feb 9, 2014 at 13:45

2 Answers 2

1

You have different errors in your statements:

  • Spelling of the word REFERENCES for foreign keys. You always wrote this with double "F", this is wrong. Don't know if this is a typo.
  • In creation of table meaning, you have missed a NOT for checking if table exists: this should be CREATE TABLE IF NOT EXISTS
  • In creation of table deck, you want to create an index on the field text, but this field does not exists. So you should create the index on the field name or something different

After fixing all this errors, I could execute all of statements on a plain MySQL database (did not test your code, just tested the statements directly on the database).

Sign up to request clarification or add additional context in comments.

4 Comments

Still the error exists though. The error is issued for the first create table query and i don't know why.
Oh buy the way, the same query works in mysql bench but not in C# sqlite
My answer is based on your MySQL tag (which you have fixed actually). Anyway I think that my suggestions are also ok für you :)
Thank you I applied all the small errors you suggested. But about indexing, I simply took out indexing parts from my code. It was about optimization and I didn't want to think about it now. But I appreciate your help man :) I updated my code as well.
0

try to create Table first and then add the index like below

cmd.CommandText = @"CREATE TABLE IF NOT EXISTS word(" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "word VARCHAR(128))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE INDEX IF NOT EXISTS idx_word_word ON word(id)";
cmd.ExecuteNonQuery();

1 Comment

Thanks man! I 'll try that tomorrow mornin g and let you know!( 12 am in my country )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.