foreign-keys

How to create foreign key that is also a primary key in MySQL?

懵懂的女人 提交于 2019-11-30 12:33:50
问题 This should be a fairly straightforward question, but I'm unable to find an easy answer. How do you create a foreign key that is also a primary key in MySQL? Here's my current attempt: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY KEY(sale_id), discount DOUBLE, type VARCHAR(255), price DOUBLE, ); CREATE TABLE Normal_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); CREATE TABLE Special_Sale( sale_id CHAR(40), PRIMARY KEY(sale_id); ); What am I missing here? Thanks in advance. 回答1: Add FOREIGN

create foreign key without a primary key

拈花ヽ惹草 提交于 2019-11-30 12:23:25
Why is it necessary to have a primary key on a column of one table to which a column of the other table having foreign key references. create table D(Did int) create table E(Eid int foreign key references D(Did)) The above query gives error: There are no primary or candidate keys in the referenced table 'D' that match the referencing column list in the foreign key 'FK__E__Eid__79C80F94'. Easy. If you have 2 values the same in the parent table, how do you know which one to associate child rows to? One side of foreign key must be unambiguous The requirement is also "unique key", not just a

Django: How to get data connected by ForeignKey via Template?

心已入冬 提交于 2019-11-30 12:07:51
Since a few weeks I am learning Python and Django. Up to this point it has been enough to read the questions and the answers of other users.But now the moment of my first own question has come. I will try to describe my problem as best i can. My problem is that I cant query or get the data I want. I want to get the url of the first object of class Image which is associated by ForeignKey to a Gallery, which is associated by ForeignKey to the class Entry. Here the models.py so far: class BlogEntry(models.Model): ... title = models.CharField(max_length=100) ... class Gallery(models.Model): entry

Django models filter by foreignkey

雨燕双飞 提交于 2019-11-30 11:56:08
I'm having some trouble in filtering objects from a set of models. Here is the problem: I have 3 classes: class Autor(models.Model): nome = models.CharField(max_length=50) slug = models.SlugField(max_length=50, blank=True, unique=True) foto = models.ImageField(upload_to='autores/', null=True, blank=True) ... class CategoriaRecolha(models.Model): categoria = models.CharField(max_length=30) descricao = models.TextField() slug = models.SlugField(max_length=30, blank=True, unique=True) ... class Recolha(models.Model): titulo = models.CharField(max_length=100) slug = models.SlugField(max_length=100

Drop down menu with value from another model

人盡茶涼 提交于 2019-11-30 11:40:13
问题 I have products belonging to collections. A collection is just a name. Products have a collection_id. In my _form view that is used for creation and edition of products, i'd like to have a drop down menu with the name of all collection. Problem, it seems there is no select method affiliated to form.for and i am trying to use : select(method, choices, options = {}, html_options = {}) from the doc but i do not understand it. I must write a methode to create a form? What are the choices, and the

Foreign keys and NULL in mySQL

你说的曾经没有我的故事 提交于 2019-11-30 11:12:41
Can I have a column in my values table (value) referenced as a foreign key to knownValues table, and let it be NULL whenever needed, like in the example: Table: values product type value freevalue 0 1 NULL 100 1 2 NULL 25 3 3 1 NULL Table: types id name prefix 0 length cm 1 weight kg 2 fruit NULL Table: knownValues id Type name 0 2 banana Note: The types in the table values & knownValues are of course referenced into the types table. NULLs in foreign keys are perfectly acceptable. Dealing with NULLs in foreign keys is tricky but that does not mean that you change such columns to NOT NULL and

Why to use foreign keys with no action on delete or update

风格不统一 提交于 2019-11-30 11:01:20
I have a question of interest: I have 2 tables in mysql with InnoDb . table tbl_a has a primary key, named a_id ; table tbl_b has a primary b_id and a foreign key on tbl_a.a_id with " ON DELETE NO ACTION ". +-------------+---------------+---------------+ | Table Name | Primary Key | Foreign Key | +-------------+---------------+---------------+ | tbl_a | a_id | | | tbl_b | b_id | a_id | +-------------+---------------+---------------+ why should I still use InnoDb and foreign keys, if i don't really use the magic of foreign keys in the end in anyway? Is there still a point of using innodb and

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

久未见 提交于 2019-11-30 10:52:43
How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs? At present, I have: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get; set; } } public class Restuarant { public int RestaurantId { get; set; } [Required] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] public string Telephone { get; set; } [Required] public int StatusId { get; set; } public List<Menu> Menus { get; set; } // NAVIGATION PROPERTIES public virtual Status Status { get;

Can a database attribute be primary and foreign key?

[亡魂溺海] 提交于 2019-11-30 09:48:48
I have 2 tables, User and Employee . Each user is given a User_ID and that is a primary key in the User table and a foreign key in the Employee table. Can that attribute in the Employee table also be a primary key? If you have a one-to-one relation between two tables, then the primary key of the details table is a foreign key as well. master detail (1 : 1) +----------+ 1:1 +-------------+ | PK id |<---o| PK FK id | +----------+ +-------------+ | col1 | | col1 | | col2 | | col2 | | etc. | | etc. | +----------+ +-------------+ If you have a m-to-n relation, the junction table has columns

Foreign key support in SQLite3

眉间皱痕 提交于 2019-11-30 09:31:43
问题 According to this thread from 2010, an "EnforceFKConstraints" connection string property was supposed to be implemented into future releases of SQLite. Does anyone know if the developers have gotten around to doing that? If not, is there another way I can enable foreign key support without doing "PRAGMA foreign_keys = ON" on each connection? I need this to make sure that deletes always cascade. 回答1: Future development of System.Data.SQLite ADO.NET provider for SQLite is done by this group.