Questions tagged [database-relations]

A relation is a data structure which consists of a heading and an unordered set of tuples which share the same type.

A relation is a data structure which consists of a heading and an unordered set of tuples which share the same type.

When Edgar F. Codd invented the relational model, he generalized the concept of binary relation (mathematical relation) to n-ary relation. Relation is a fundamental concept in relational model.

  • A relation has zero or more tuples.
  • A relation value is an instance of a relation.
  • A relation variable (relvar) is a variable which has a relation value.

In some contexts, relation means relation variable. In other contexts, relation means relation value.

In SQL, a database language for relational databases, a relation variable is called a table. Relational model concepts including relation

A relation value, which is assigned to a certain relation variable, is time-varying. By using a Data Definition Language (DDL), it is able to define relation variables.

  • A heading is the unordered set of certain attributes (columns). A heading has zero or more attributes.
  • A body is the unordered set of tuples, which constitutes a relation value. In other words, a relation value consists of a heading and a body.
  • A tuple is a data structure which consists of the unordered set of zero or more attributes.
  • An attribute (column) is a pair of its attribute name and domain name. Domain can be considered data type, or simply, type.
  • An attribute has an attribute value which conforms to its domain. An attribute value is a scalar value or a more complex structured value.
  • The degree of a relation is the number of attributes which constitute a heading. The degree of a relation value is zero or more integer. An n-ary relation is a relation value in which its degree is n.
  • The cardinality of a relation is the number of tuples which constitutes a relation value. The cardinality of a relation value is zero or more integer.

There are no duplicate tuples in a relation value. A candidate key is a certain minimal set of one or more attributes that can uniquely identify individual tuples in a relation value.

319 questions
258
votes
11 answers

How to express a One-To-Many relationship in Django?

I'm defining my Django models right now and I realized that there wasn't a OneToManyField in the model field types. I'm sure there's a way to do this, so I'm not sure what I'm missing. I essentially have something like this: class…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
154
votes
3 answers

Rails: Using build with a has_one association in rails

In this example, I create a user with no profile, then later on create a profile for that user. I tried using build with a has_one association but that blew up. The only way I see this working is using has_many. The user is supposed to only have at…
espinet
  • 1,703
  • 2
  • 11
  • 7
142
votes
5 answers

How do you track record relations in NoSQL?

I am trying to figure out the equivalent of foreign keys and indexes in NoSQL KVP or Document databases. Since there are no pivotal tables (to add keys marking a relation between two objects) I am really stumped as to how you would be able to…
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
61
votes
1 answer

Relations on composite keys using sqlalchemy

I have this simple model of Author - Books and can't find a way to make firstName and lastName a composite key and use it in relation. Any ideas? from sqlalchemy import create_engine, ForeignKey, Column, String, Integer from sqlalchemy.orm import…
mdob
  • 2,224
  • 3
  • 22
  • 25
52
votes
1 answer

Cascade delete in Ruby ActiveRecord models?

I was following the screencast on rubyonrails.org (creating the blog). I have following models: comment.rb class Comment < ActiveRecord::Base belongs_to :post validates_presence_of :body # I added this end post.rb class Post <…
38
votes
3 answers

Using EntityRepository::findBy() with Many-To-Many relations will lead to a E_NOTICE in Doctrine

For a Symfony2 project I had to create a relationship between a blog post and so called platforms. A platform defines a specific filter based on the domain you use to view the site. For example: If you join the site by url first-example.com, the…
devsheeep
  • 2,076
  • 1
  • 22
  • 31
23
votes
5 answers

Foreign keys and NULL in mySQL

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 …
Industrial
  • 41,400
  • 69
  • 194
  • 289
22
votes
8 answers

When to use 1-to-1 relationships between database tables?

A DB design question: when do you decide to use 1 to 1 relation tables? One of the places I see this is, for example, when you have a User and UserProfile table, people split them instead of putting all columns just in a User table. Technically, you…
Ray
  • 12,101
  • 27
  • 95
  • 137
18
votes
1 answer

How to work with nested relationships in Room

I have entities: @Entity public class A { @PrimaryKey(autoGenerate = true) public long id; public A() {} } @Entity() public class B { @PrimaryKey @NonNull public String id; public String oneCId; public String…
9
votes
2 answers

Language features to implement relational algebra

I've been trying to encode a relational algebra in Scala (which to my knowlege has one of the most advanced type systems) and just don't seem to find a way to get where I want. As I'm not that experienced with the academic field of programming…
John Nilsson
  • 17,001
  • 8
  • 32
  • 42
9
votes
2 answers

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

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…
apelliciari
  • 8,241
  • 9
  • 57
  • 92
8
votes
4 answers

Are there any classification algorithms which target data with a one to many (1:n) relationship?

Has there been any research in the field of data-mining regarding classifying data which has a one to many relationship? For example of a problem like this, say I am trying to predict which students are going to drop out of university based on…
8
votes
5 answers

How to sync multiple values of the same Attribute in Laravel?

I developing an eCommerce ( with Multiple Product Attributes feature ) website using Laravel 5.4. Everything is working fine. But When I try to sync multiple values of the same Attribute in Pivot table. Laravel ignores the duplicate pares. For…
Rizwan Khan
  • 474
  • 2
  • 7
  • 21
7
votes
2 answers

How to use an auto incremented primary key as a foreign key as well?

This is what I'm trying to do: I have 2 tables... CREATE TABLE `parent` ( `id` int(11) NOT NULL AUTO_INCREMENT, `data` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `child` ( `parent_id`…
Rolf
  • 5,550
  • 5
  • 41
  • 61
7
votes
9 answers

Why are positional queries bad?

I'm reading CJ Date's SQL and Relational Theory: How to Write Accurate SQL Code, and he makes the case that positional queries are bad — for example, this INSERT: INSERT INTO t VALUES (1, 2, 3) Instead, you should use attribute-based queries like…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
1
2 3
21 22