concept of domains and subdomains for small database entities - mysql

recently I learned a concept called by my teacher "Domains and subdomains" in which small tables are generalized in which less than 5000 data or rows will be saved, I show you an example:
Entities
enter image description here
level dates:
id_level description
01 continent
02 country
03 state
domain dates
id_domain detail level parent
01 America 01 -
02 USA 02 01
03 Europe 01 -
04 Africa 01 -
05 UK 02 03
06 Aberdeen 03 05
If this concept has another name, it would help me a lot to know it.
Then we had to make an abstraction of this and take it to common entities like the following example:
enter image description here
level dates:
id_level description
01 address
02 phone
03 gender
domain dates
id_domain detail level parent
01 male 03 -
02 female 03 -
03 mobile phone 02 -
04 home address 01 -
05 home phone 02 -
06 work address 01 -
Person dates
id_person name phone_type phone_value address_type adress_value gender
01 Jhon 03 39249927 06 a place... 01
02 Mary 05 2489540300 04 a place2... 02
but i get an error saying, the duplicate key name when i try to make the relation
ALTER TABLE person
ADD CONSTRAINT FK_PERSON_DOMAIN
FOREIGN KEY (address_type)
REFERENCES domain(id_domain);
Error
Duplicate key name 'FK_PERSON_DOMAIN'
Something I should add is that I already did the nesting with phone_type, after this it does not let me nest the other two attributes as shown in the diagram
This is the relationship that worked perfectly:
ALTER TABLE person
ADD CONSTRAINT FK_PERSON_DOMAIN
FOREIGN KEY (phone_type)
REFERENCES domain(id_domain);

I call those "lookup tables." They usually have characteristics of having a small number of rows, and also they don't change frequently. They may have an integer id primary key, or they may have a natural primary key.
The error you got about the duplicate key name is caused by the rule that constraint names must be unique across all tables in a schema. In other words, there must be only one constraint named FK_PERSON_DOMAIN_PERSON in the schema.
You said you already created a constraint with this name for the phone_type column. If you want another constraint for the address_type column, you must choose a different name for that constraint.
Here's an example of what I mean:
ALTER TABLE person
ADD CONSTRAINT FK_PERSON_PHONE
FOREIGN KEY (phone_type)
REFERENCES domain(id_domain);
ALTER TABLE person
ADD CONSTRAINT FK_PERSON_ADDRESS
FOREIGN KEY (address_type)
REFERENCES domain(id_domain);
See that the constraint names are different.

Related

Removing Partially Matched Duplicates from a List

I have a list of many email addresses. Formatted similarly as follows:
Business Name 01 email#business01.com; other#business01.com; info#business01.com; email#business01.com; other#business01.com; info#business01.com; other#business01.com; email#business01.com; other#business01.com; other#business01.com; email#business01.com;
Business Name 02 email#business02.com; info#business02.com; other#business02.com; email#business02.com; email#business02.com; other#business02.com; other#business02.com; info#business02.com; email#business02.com; info#business02.com; other#business02.com;
I am looking to delete the duplicate addresses. So the list should look like:
Business Name 01 email#business01.com; other#business01.com; info#business01.com;
Business Name 02 email#business02.com; info#business02.com; other#business02.com;
This has me stumped. Any ideas?

SQL - Case when lineid='01' then newid else id

I have this kind of data in my table
lineid
price €
01
100.00
02
200.00
01
10.34
01
311.12
01
14.33
02
36.44
03
89.70
04
11.33
and i would like my output to be like this
docid
lineid
price €
1
01
100
1
02
200.00
2
01
10.34
3
01
311.12
4
01
14.33
4
02
36.44
4
03
89.70
4
04
11.33
Its data for invoices and for every line that has lineid='01' it means that the info is for different invoice so i have to mark it with new documentID that i want you to help me create it with a command.
Its probably something easy but i am searching like a maniac here and i cant find the solution.
EDIT: Yes , it Is "increment docid each time lineid equals 01" what i want
You could use running counts using something like below (assuming this is MS SQL you are talking about)
SELECT ROW_NUMBER() over(partition by [LineId] order by [LineId]) as DocId,
[LineId],
[Price]
FROM [StackOverflow].[dbo].[RunningCount] order by [LineId]

Getting in a muddle with numeric primary keys

I hope someone can help. I'm trying to set up something along the below but am getting in a bit of a muddle. From what I understand, deriving a numeric ID variable (eg auto-incremented) for the primary key is more efficient that using a composite primary key of the 'natural' variables that define a record (especially if they're character variables (and more so if the collation is UTF-8))
As in the below example, each customer has a list of items (ITEMID) which are all members of a category (CATID), however, the problem is that I need customers to additionally be able to assign their items as a component of a collection set (SETID) which is a non-identifying reference table - any customer could have multiple versions of one SETID.
The items required for a set are specified by CATID. Therefore, in the example below which is for one customer, they could choose to assign Item 2, or 4 (or neither or both) to SET 001.
**ITEMS**
ITEMID CATID
1 04
2 02
3 01
4 02
5 05
**SETS**
SETID CATID
001 01
001 02
002 04
003 05
**CATEGORY**
CATID
01
02
03
04
05
**Wanted result:**
ITEMID CATID SETNUMBER SETID
1 04 (customer chose not to assign to SET 002)
2 02 1 001
3 01 1 001
4 02 2 001
5 05 003
Many thanks in advance!
From your description, it sounds like the "ITEM" table could stand to have a NULLable "SETID" foreign-key column.

Reset counter ID to 1 for every new year in MS Access

I have two tables , tblContacts and tblTrackJob. Every time a new record is about to be added in tblContacts , id like to assign its JobNo field to the value of whatever ID is in tblTrackJob has stored. Id like the value to reset to 1 every year.
For example...
first name, last name, JobNo, dateEntered
bob smith 01 1/1/2013
john doe 02 1/2/2013
mary Thomas 03 1/3/2013
Joe Henry 01 1/1/2014
Since we're dealing with Access 2010 we can create a Before Change data macro for the [tblContacts] table to assign the [JobNo] value:
The macro will find the largest [JobNo] in the table from records with a corresponding Year([dateEntered]), and then add 1 to that value to give us the [JobNo] for the new record. If no existing records are found for the corresponding year then the new record gets a [JobNo] equal to 1.

How to display a table order by code (like 01, 02… then null columns)?

How to display a table order by code (like 01, 02… then null columns)?
Using Access 2003 Database
Table
Name Title code Nationality code
Raja 05 03
Ramu 03
Vijay 01 02
John 04 01
Roby 06
Abilash 02 05
So on…,
I want to display a table order by title code, nationality code
In my “nationality code” field some of the columns are null, so I want to display a table order by title code, nationality code (like 01, 02… then null columns)
My query.
Select * from table order by nationality code, title code
Name Title Nationality
Ramu 03
Roby 06
John 04 01
Vijay 01 02
Raja 05 03
Abilash 02 05
But Null value is coming first in the nationality code, I want to display nationality code like 01, 02, 03, 05 then null values
Need Query Help.
Try using the Nz function to provide a value for NULL columns, for example ORDER BY Nz(Nationality,9999999)
Note the NZ() will only work within the Access user interface. For a more neutral approach, you could try an expression in the ORDER BY clause e.g. something like
ORDER BY (LEN(nationality_code) > 0), nationality_code, title_code;
Are you sure those values are NULL rather than zero-length values? If they are, then you should replace them with NULL then set Allow Zero Length to false for the column or add a Validation Rule or CHECK constraint to do the same.
If they are NULL then this may be a bug. I assume you are using Jet 4.0, for which NULL collation should sort NULL to the end of the resultset. In which case, you need the workaround (i.e. currently selected answer).
A note on Nulls/ZLS: Access 2003 (or maybe it was 2002, which I barely ever used) changed the default in its table designer from AllowZLS: No to AllowZLS: Yes. It's very, very annoying.