foreign-keys

SQLite: Bad performance with ON DELETE CASCADE

六眼飞鱼酱① 提交于 2020-01-04 04:31:28
问题 I am using a SQLite Database in my application for storing measurement data. It is organized in groups in projects. The tables are interconnected with foreign keys, that use ON DELETE CASCADE to handle the links. The database looks like this: Table Projects Table Groups (Foreign Key: project_id) Table Files (Foreign Key: group_id) Table Datapoints (Foreign Key: file_id) All foreign keys reference columns declared like project_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT . The foreign keys

SQL - How do I delete two entities referencing each other

烈酒焚心 提交于 2020-01-04 03:51:09
问题 So I have two entities referencing each other, parent , child . child must be deleted if parent is deleted, but cannot be deleted while there's still a parent referencing it. These are the two constraints I've been given: ALTER TABLE public.parent ADD CONSTRAINT parent__child_id__fk FOREIGN KEY (child_id) REFERENCES child(id) ON DELETE CASCADE ; ALTER TABLE public.child ADD CONSTRAINT child__parent_code__id__fk FOREIGN KEY (parent_code, id) REFERENCES parent(code, child_id) ON UPDATE CASCADE

Using foreign key in SQL server and DB locks

大城市里の小女人 提交于 2020-01-03 15:34:12
问题 I have been working with SQL Server for a long time I always use FKs and indexes when a logic connection between tables exist Example: MyTable1 { ID BIGINT IDENTITY (1, 1) NOT NULL, SomeData NVARCHAR(255) NOT NULL, MyFK BIGINT NULL -- this is a FK to MyTable2.ID } MyTable2 { ID BIGINT IDENTITY (1, 1) NOT NULL, SomeData NVARCHAR(255) NOT NULL } Now to the problem, When I execute some bulk update operations on MyTable1 that update MyFK, and in the same time execute an insert statements to

Django: get related set from a related set of a model

南笙酒味 提交于 2020-01-03 08:43:27
问题 class Book(models.Model): # fields class Chapter(models.Model): book = models.ForeignKey(Book) class Page(models.Model): chapter = models.ForeignKey(Chapter) I want all the pages of the book A, possibly without cycling every chapter to fetch the pages. book = Book.objects.get(pk=1) pages = book.chapter_set.page_set #?!? 回答1: You can't do it that way. chapter_set is a query set, it doesn't have an attribute page_set. Instead, turn it around: Page.objects.filter(chapter__book=my_book) 回答2: When

can't add foreign key in mysql?

守給你的承諾、 提交于 2020-01-03 07:27:08
问题 I used MySQL workbench to add a foreign key in a table, but some strange error happened, this is the SQL statement: ALTER TABLE `tansung`.`Declaration` ADD COLUMN `goodsId` INT(11) NOT NULL AFTER `declarationId` , ADD CONSTRAINT `goodsId` FOREIGN KEY (`goodsId` ) REFERENCES `tansung`.`Goods` (`goodsId` ) ON DELETE NO ACTION ON UPDATE NO ACTION , ADD INDEX `goodsId` (`goodsId` ASC) ; When i click apply, the surprise comes out! ERROR 1005: Can't create table 'tansung.#sql-1b10_1' (errno: 150)

SQLite foreign key in android/

别说谁变了你拦得住时间么 提交于 2020-01-03 04:34:11
问题 i am working on Android 2.2 i have created two table and second table have the foreign key as a primary key of first table but at the time of insertion the foreign key column remain blank.. here's the tables.. public static final String KEY_Username = "username"; public static final String KEY_Password = "password"; public static final String KEY_SITE = "site"; private static final String DATABASE_NAME = "test"; private static final int DATABASE_VERSION = 1; private static final String

Error 1005 in MySQL (from foreign key syntax?)

♀尐吖头ヾ 提交于 2020-01-03 04:04:54
问题 I'm getting an error while creating the following tables in MySQL. Here's the error: ERROR 1005 (HY000): Can't create table 'Works' (errno: 150) Could you guys take a look at the foreign keys of Works() and let me know if you see anything weird about them that would cause this error to happen? Here's my overall schema: create table Employee( Lastname varchar(10), FirstName varchar(10), MidInitial char(1), gender char(1), street varchar(10), city varchar(10), primary key(Lastname, FirstName,

Relational SQLite on Windows RT

最后都变了- 提交于 2020-01-03 01:55:24
问题 i am using SQLite for Windows Runtime but i dont see any way to define relationships between tables. Is this feature supported in SQLite for WinRT? If not, are there any alternative solutions for relational local database for Windows RT? 回答1: The database engine itself supports relationships. One way is to specify the foreign keys when creating the tables: CREATE TABLE Foo( Id integer primary key autoincrement not null , ... ) CREATE TABLE Bar( Id integer primary key autoincrement not null,

How do I properly design a database? Foreign Keys vs Secondary Keys?

雨燕双飞 提交于 2020-01-02 18:21:29
问题 here are some generic tables, I am trying to fully understand how to properly setup databases tables. Are these setup correctly? I want to be able to lookup a user's Items and Item Details as fast as possible. FYI for this example ItemDetailsX do not share the same data fields. I am a little bit stuck on Foreign Keys and Secondary keys. When do you use a Secondary Key vs a Foreign Key? tbl_Users 1:* tbl_Item //relationship tbl_Item 1:1 tbl_ItemDetail1 & tbl_ItemDetail2 // relationship tbl

Create Foreign Key SQL

▼魔方 西西 提交于 2020-01-02 10:28:12
问题 How do I create a foreign key from table tGeoAnswers column 'locationId' to table tLocations column 'id'? ALTER TABLE tGeoAnswers ADD FK_Answer_Location FOREIGN KEY (locationId) REFERENCES tLocations(id) I am trying this code that I found but I get the following error: The definition for column 'FK_Answer_Location' must include a data type 回答1: ALTER TABLE tGeoAnswers ADD CONSTRAINT FK_Answer_Location ... Otherwise it assumes you're adding a column called FK_Answer_Location . 回答2: Assuming