FOREIGN KEY constraint failure-cascade update deletion

Source: Internet
Author: User
public void OnCreate (Sqlitedatabase db) 
	{
		//TODO auto-generated method stub
		String classessql = ' CREATE tabl E classes (class_id varchar () primary key, "+
        		" class_name varchar) ";
		
		String studentssql = "CREATE TABLE students" (student_id varchar primary key, "+
        		" student_name varchar (), score varchar (4), class_id varchar (a), "+
        		" foreign Key (class_id) references classes (class_id) "+
        		" on DELETE cascade ON UPDATE cascade) ";
		Db.execsql (classessql);
		LOG.D ("My", "CREATE TABLE classes:" +classessql);
		Db.execsql (studentssql);
		LOG.D ("My", "CREATE TABLE Students:" +studentssql);
		
		
	}

When creating a table, there is the creation of a foreign key, cascading updates and cascading deletions, but when a class is deleted, the students who first belong to the class are not deleted, which means
On DELETE CASCADE on UPDATE cascade

It's expired.

After the information is known: SQLite in the 3.6.19 version to support foreign key constraints, but in order to compatible with the previous program, the default does not enable the feature, if you want to enable the feature each time you need to use the following statement: PRAGMA Foreign_keys = on to open.


That is, you need to perform db.execsql ("PRAGMA foreign_keys=on") when executing a statement that deletes a class.


/**
	 * Delete a class
	 * will also delete students students in the class
	 * @param class_id
	 /public
	void Deleteclass (String class_id)
	{
	    Sqlitedatabase localsqlitedatabase = This.dbhelper.getWritableDatabase ();
	    Cascade Delete and Cascade update
	    //////When cascading statements are executed you must first set "PRAGMA foreign_keys=on"
	    //otherwise the Cascade relationship defaults
	    Localsqlitedatabase.execsql ("PRAGMA foreign_keys=on");
	    object[] Arrayofobject = new object[1];
	    Arrayofobject[0] =class_id;
	    Localsqlitedatabase.execsql ("Delete from classes where class_id=?", Arrayofobject);
	    Localsqlitedatabase.close ();
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.