in a relational database, can we have a table without any relation with the other tables? - sql

in a relational database, can we have a table without any relation with the other tables?

Yes. The way relations are expressed are with foreign keys. If a table you generate has no Foreign keys, and no foreign keys in other tables point to this table, it has no relationships.
It can still be given a relationship later though so don't worry about shooting yourself in the foot.

Of course. Even you can create a table without fields.

Yes you can. Tables do not have to have any relation to each other. Relations can always be added through the use of foreign keys if you want to add them later.

I'm creating a company management database and I have a "Company_Expenses" table that I just need for summery reports on the Company's revenues. I can't think of anything this table might need to be connected to so it's just on it's own for now.

Your question belies a larger problem with your understanding of databases.
A relation is a set of tuples (a relation can be thought of as a table).
A tuple can be thought of as a row.
Relationship in English means connectedness and has nothing to do with relation from relational databases.
Both relation and relationship share word roots, but in database theory, these words/concepts do not have anything to do with each other.
The root of the word relation in database theory comes from math (see function vs relation).

Related

How can I find the relationship between dimension and fact tables when there is no FK?

I need to create a bus matrix and in order to do that i need to know which fact table has relationships with which dimension tables.
Unfortunately, in this new project I'm in, it seems to be no FK (crazy, i know).
What I thought about is to use ETL queries and check the joins between the Fact table with the dimension tables.
What I'm worried about is that there might be more relationships that are not included in ETL queries...any advice?
You can use the system metadata tables to list the foreign key references:
select tbname, pkcolnames, reftbname, fkcolnames, colcount
from SYSIBM.SYSRELS B;
If the database does not have properly declared foreign key relationships, then the database does not have the information you are looking for.
Assuming the DB holds no information about the FKs (or information that would help you derive them, like identical column names) then, as you mentioned, examining the ETL code used to load each fact table is probably the only other way of doing this. The ETL must be running a look up on each dimension to get the PK to insert into the fact record, so the information will be there.
There shouldn't be any relationships involving facts that you couldn't determine with this approach. There may be additional relationships between dimensions (bridge tables, more complex SCD types, etc.) but if you sorted out the fact relationships then what remains should be a small enough subset to resolve manually (i.e. by intelligent guesswork)

How to get relationship between tables

Hi I am creating a ER diagram for my organization. How can i get the relationship between the tables in snowflake
Thanks
If you are trying to reverse engineer a current Snowflake database, you may have trouble. Foreign key constraints can be included in the table definition, however they are not enforced. If your developers included the constraints when they defined the tables, you can get the relationship from the table definitions. It is a good practice to include the constraints as many third party tools use them.
Do a deep study of all the relations of tables.
And try writing all the conditions on which two or more tables are connected
SELECT
`TABLE_NAME',
`COLUMN_NAME`,
FROM `YOUR_DATABASE`
WHERE 'PUT YOUR CONDITIONS '
This should solve your question
Click Here

Designing Tables Sql Server

Good Morning,
in the design of a database, I have a table (TabA's call it) that could have relationships with four other tables. In the sense that this table can be connected both with the first of four, and with the second, and the third to the fourth, but could not have links with them; or it could have one (with any of the tables), or two links (always with two of any of them), and so on.
The table TabA I added four fields that refer to the four tables which could be "null" when they do not have any connection.
Wondering is this the kind of optimal design (say the four fields in the TabA) or you can make a better design for this type of situation?
Many thanks for your reply.
dave
In answer to the question and clarification in your comment, the answer is that your design can't be improved in terms of the number of foreign key columns. Having a specific foreign key column for every potential foreign key relationship is a best practice design.
However, the schema design itself seems questionable. I don't have enough information to tell whether the "Distributori_[N]_Livello" tables are a truly hierarchical structure or not. If it is, it is often possible to use a self-referential table for hierarchical structures rather than a set of N tables, as the diagram you linked seems to use. If you are able to refactor your design in such a way, it might be possible to reduce the number of foreign key columns required.
Whether this is possible or not is not for me to say given the data provided.

DB: advantages of relations

I always think that the relations between tables are needed to perform cross-table operations, such as join. But I noticed that I can inner join two tables that are not linked at all (hasn't any foreign keys).
So, my questions:
Are some differences (such as speed) in joining linked and not-linked tables?
What are the advantages/disadvantages of using relations bwtween tables?
Thank you in advance.
The primary advantage is that foreign key constraints ensure the relational integrity of the data.. ie it stops you from deleting something that has a related entry in another table
You only get a performance advantage if you create an index on your FK
The FK/PK relationship is a logical feature of the data that would exist even if it were not declared in a given database. You include FKs in a table precisely to establish these logical relationships and to make them visible in a way that makes useful inner joins possible. Declaring an FK as referencing a given PK has the advantage, as said in other answers, of preventing orphaned references, rows that reference a non existent PK.
Indexes can speed up joins. In a complicated query, the optimizer may have a lot of strategies to evaluate, and most of these will not use every available index. Good database systems have good optimizers. In most database systems, declaring a PK will create an index behind the scenes. Sometimes, but not always, creating an index on the FK with the same structure as the index n the PK will enable the optimizer to use a strategy called a merge-join. In certain circumstances a merge-join can be much faster than the alternatives.
When you join tables that are apprently unrelated, there are several cases.
One case is where you end up matching every row from table A with every row from table B. This is called a cartesian join. It takes a long time, and nearly always produces unintended results. One time in ten years I did an intentional cartesian join.
Another case is where both tables contain the same FK, and you match along those two FK. An example might be matching by ZIPCODE. Zipcodes are really FKs to some master zipcode table somewhere out there in post office land, even though most people who use zipcodes never realize that fact.
A third case is where there is a third table, a junction table, containing FKs that reference each of the two tables in question. This implements a many-to-many relationship. In this case, what you probably want to be doing is a three way join with two inner joins each of which has an FK/PK matchup as the join condition.
Either I'm telling a lot that you already know, or you would benefit by going through a basic tutorial on relational databases.
In relational database terms a relation is (more or less) the data structure you have called a table - it is not something that exists "between" tables. A important advantage of the relational model is that there are no predefined links or other navigational structures that limit the way data can be joined or otherwise combined. You are free to join relations (tables) in a query however you like.
What you are asking about is actually called a foreign key constraint. A foreign key is a type of constraint that helps ensure data integrity by preventing inconsistent values being populated in the database.

Translating ER to SQL DDL

When I am translating an ER to SQL DDL I need to Create a table only for the entities or for the relations too?
Yes, you need to create tables for both entities and relationships. Also, keep in mind that you have to include foreign keys and link your tables
It depends what type of relationship you have. If it is many to many relationship then it must require a separate table for the relationship itself. Any way you can search on google ER diagram to relational database or look the text book on the relational model chapter of Modern database management sytem of author Hoffer.
You require a CREATE TABLE statement for each ENTITY.
Your relations ships are generally implemented as FOREIGN KEY CONSTRAINTS or FOREIGN KEY INDEXES between those tables.
Each entity becomes a table and each many to many relationship becomes a table.
Add the child columns (FK column) to the (child) tables also. When you create a N:M (many to many) relationship in for example DeZign for Databases, you see that an intersection table is create automatically. The columns which are added automatically are in first instance the columns of the primary key of both tables. You can see that in this video:
http://www.datanamic.com/support/vd-dez001.html