How to depict table joins in diagram? - database

I have several tables (let's call them A, B, C, D, etc.), where each has multiple columns (A has columns a1, a2, etc.).
I'd like to diagrammatically represent the following:
Inner join A and B where A.a1 = B.b1, then check the value of B.b2. If B.b2 = 1, inner join the result with C where A.a1 = C.c1. If B.b2 = 2, inner join the result with D where A.a1 = D.d1.
I've tried to use a traditional flowchart, but I'd like to keep track of the tables and their columns. With a database schema diagram, however, I'm not sure how to depict things like logical conditions.
What's the best way to do this?

Related

1 to many relationship involving multiple fields in power bi

How do you define 1 to many relationship between two tables in Power BI, when multiple columns are involved.
I.E. Column A, B, and C in table 1 tied to Column E, F, and G in table 2, with a 1 to many relationship.
You can create a new column concatenating the fields like:
tied01 = CONCATENATE( CONCATENATE(your_dataset[Column A], your_dataset[Column B]), your_dataset[Column C])
And to
tied02 = CONCATENATE( CONCATENATE(your_dataset[Column E], your_dataset[Column F]), your_dataset[Column G])
Once you have these two new columns, you can merge them to only one column or create a dynamic table making the reference for the new created columns

SQL optimazation

I am working on a database where a total of 788 data is currently stored and continuously increasing with time.
My code is as follows:
SELECT DISTINCT R.remarks, R.payerId, R.payername, R.payeraddress, R.collectorName, R.serialno, OOI.phone_no, CR.cr_no, F.application_no, R.series, R.txndate, R.amount, T.toda_name, B.brand_name, M.motor_no, M.chassis_no,
M.plate_no, F.date_issue, M.year_model, M.body_color, OOI.ice_person_name, OOI.ice_person_address, OOI.ice_person_contact_no, M.motor_id, F.franchise_id, LEFT(R.remarks, 4) AS franchise_no, SUBSTRING(R.remarks,
CHARINDEX('|', R.remarks) + 1, LEN(R.remarks)) AS motor_noremarks
FROM etracs_tayabas.dbo.Receipt AS R INNER JOIN
etracs_tayabas.dbo.ReceiptItem AS RI ON RI.parentid = R.objid INNER JOIN
etracs_tayabas.dbo.IncomeAccount AS IA ON IA.objid = RI.acctid LEFT OUTER JOIN
dbo.vfTA_tblMotor AS M ON M.motor_no = SUBSTRING(R.remarks, CHARINDEX('|', R.remarks) + 1, LEN(R.remarks)) LEFT OUTER JOIN
dbo.vfTA_tblOperatorOtherInfo AS OOI ON OOI.operator_id = R.payerId LEFT OUTER JOIN
dbo.vfTA_tblCertificateOfRegistration AS CR ON CR.motor_id = M.motor_id LEFT OUTER JOIN
dbo.vfTA_tblFranchise AS F ON F.or_id = R.objid LEFT OUTER JOIN
dbo.vfTA_tblTODA AS T ON T.toda_id = M.toda_id LEFT OUTER JOIN
dbo.vfTA_tblReconciledTaxpayer AS RT ON RT.payer_id = R.payerId LEFT OUTER JOIN
dbo.vfTA_tblBrand AS B ON B.brand_id = M.brand_id
WHERE (IA.objid = 'FTFA00000242') AND (F.franchise_id IS NULL) AND (R.voidId IS NULL) AND (R.remarks IS NOT NULL) AND (RT.rtp_id IS NULL)
Everytime I run this code, it always takes me up to 10 minutes long or more to load up all the values. I tried to make a SQL View of this same code but when I run it, the error Execution Timeout always shows.
I want to know:
What is the best optimization method for views and stored procedure?
How can I lessen the time it takes for the data to load given that it has 788 data and increasing over time?
How to prevent a lot of execution timeout to happen in SQL or even in a program?
Some sites that help teaches SQL optimization.
I am trying to learn optimization right now because I noticed that when I make a query, it usually takes a lot of time to load up and sometimes producing the error 'Execution Timeout'
I am currently new with this. Thanks in advance.
I think using DISTINCT against such many columns cost a lot. Is that really necessary for your query?
I also wondered the following part.
LEFT OUTER JOIN dbo.vfTA_tblMotor AS M ON M.motor_no = SUBSTRING(R.remarks, CHARINDEX('|', R.remarks) + 1, LEN(R.remarks))
It means there's no way to use INDEX for this relationship. How about adding a column to table etracs_tayabas.dbo.Receipt where you store the substring result, and calculating it when a record is inserted/updated to the table. In this way, you can make INDEX for this relationship and can optimise that part of JOIN.

NexusDB execution failed

NexusDB : Query execution failed:
Operating system error in Temporary Storage Manager: Espace insuffisant sur le disque($70/112)
[$2B27/11047]
I don't understand this message. What's the real problem?
Query code:
SELECT d.Code, c.RaisonSociale, c.EMail, m.Article, m.Quantite, m.TotalTTC, m.NumeroTicket
FROM C_Client c, C_Mouvement m, C_Depot d, C_Article a, C_Ticket t
WHERE c.Ident = m.RefClient
AND m.Article = a.Code
AND t.DateFacture BETWEEN :dateDebut AND :dateFin
The "Temporary Storage Manager" is responsible for storing dirty blocks if the Buffer Manager has reached the maximum amount of memory it is allowed and has to evict blocks from memory.
"Espace insuffisant sur le disque" is an OS error message, and I would guess that says something like "disk is full". Which meant that the the disk the TSM is using for it's storage is full.
Probably because the resultset your query is producing is so humongous that it exceeds both your available memory and disk space.
Which isn't really surprising if you look at your query:
FROM
C_Client c,
C_Mouvement m,
C_Depot d,
C_Article a,
C_Ticket t
WHERE
c.Ident = m.RefClient AND
m.Article = a.Code AND
t.DateFacture BETWEEN :dateDebut AND :dateFin
You are joining 5 tables, but you are only defining join conditions between 3 of them (c, m, and a) which will produce a full unrestricted cross join with the other 2 tables. If we only assume 1000 records for each (c, m, and a), d, and t that's already a 1,000,000,000 record resultset.
That's one of the reasons why the join syntax you are using has been deprecated by the SQL standard for over two decades already. If you try to write your query using current syntax, the problem becomes obvious very quickly:
FROM
C_Client AS c
INNER JOIN
C_Mouvement AS m ON c.Ident = m.RefClient
INNER JOIN
C_Article AS a ON m.Article = a.Code
INNER JOIN
C_Depot AS d ON --???
INNER JOIN
C_Ticket AS t ON --???
WHERE
t.DateFacture BETWEEN :dateDebut AND :dateFin

Picking out pairs from SQL Server

I am working on exercise 16 from SQL-EX.com
Find the pairs of PC models having identical speeds and RAM.
As a result, each resulting pair is shown only once, i.e. (i, j) but not (j, i).
Result set: model with higher number, model with lower number, speed, and RAM.
I used the following query
SELECT B.code, B.model AS BM, A.code, A.model, A.speed, A.ram
FROM PC A
JOIN PC B
ON A.speed = B.speed AND A.ram = B.ram
WHERE A.model <> B.model
ORDER BY B.model ASC
How do I retrieve only the pairs where BM is higher than model?
Instead of using <>, use <:
SELECT
a.model,
b.model,
a.speed,
a.ram
FROM PC a
INNER JOIN PC b
ON b.speed = a.speed
AND b.ram = a.ram
AND b.model < a.model
Change this line:
WHERE A.model <> B.model
To this:
WHERE A.model > B.model
You also need to select the correct columns, but getting that WHERE expression right was the hard part.

Resolve many to many relationship

Does anyone have a process or approach to use for determining how to resove a many-to-many relationship in a relational database? Here is my scenario. I have a group of contacts and a group of phone numbers. Each contact can be associated with multiple phone numbers and each phone number can be associated with multiple contacts.
A simple example of this situation would be an office with two employess (e1 & e2), one main voice line (v1), one private voice line (v2). e1 is the CEO so they have thier own private voice line, v1, but they can also be reached by calling the main line, v2, and asking for the CEO. e2 is just an employee and can only be reached by calling v2.
So, e1 should be related to v1 & v2. e2 should be related to v2. Conversly, v1 should be related to e1 and v2 should be related to e1 & e2.
The goal here is to ge able to run queries like "what numbers can e1 be reached at" and "what employees can be reached at v2", etc.. I know the answer will involve an intermediate table or tables but I just can't seem to nail down the exact architecture.
You don't need any temp tables for the query. There is an intermediary table for the mapping.
numbers_tbl
-----------
nid int
number varchar
employees_tbl
-----------
eid int
name varchar
employee_to_phone_tbl
-----------
eid int
nid int
How can I call Bob?
select *
from employees_tbl e
inner join employee_to_phone_tbl m
on e.eid = m.eid
inner join numbers_tbl n
on m.nid = n.nid
where e.name = 'Bob'
Who might pickup if I call this number?
select *
from numbers_tbl n
inner join employee_to_phone_tbl m
on m.nid = n.nid
inner join employees_tbl e
on e.eid = m.eid
where n.number = '555-5555'
Employees:
eID, eName
1, e1
2, e2
PhoneNumbers:
pID, pNumber
1, v1
2, v2
EmployeePhones:
eID, pID
1, 1
1, 2
2, 2
then you inner join. if you need to find out what number(s) e1 can be reached at (t-sql):
SELECT E.eName, P.pNumber
FROM dbo.Employees E
INNER JOIN dbo.EmployeePhones EP ON E.eID = EP.eID
INNER JOIN dbo.PhoneNumbers P ON EP.pID = P.eID
WHERE E.eName = 'e1'
I believe this should work (testing it right now...)
EDIT: Took me a few minutes to type up, sorry for duplication...
Others have explained the schema, but I'm going to explain the concept. What they're building for you, the table named EmployeePhones and employee_to_phone_tbl, is called an Associative Entity, which is a type of Weak Entity.
A Weak Entity does not have its own natural key and must instead be defined in terms of its foreign keys. An Associative Entity exists for the sole purpose of mapping a many-to-many relationship in a database that does not support the concept. Its primary key is the grouped foreign keys to the tables it maps.
For further information on relational theory, see this link
Normalize
Best Practices on Referential Integrity
Check this - http://statisticsio.com/Home/tabid/36/articleType/ArticleView/articleId/327/Need-Some-More-Sample-Databases.aspx
After just a little more thought, here is what I came up with. It probably goes along with the approach AviewAnew is thinking of.
employees
id (index)
name
numbers
id (index)
number
relations
employees.id (index)
numbers.id (index)
employees
1 : e1
2 : e2
numbers
1 : v1
2 : v2
relations
1 : 1
1 : 2
2 : 1
Is this the best/only approach?

Resources