Getting in a muddle with numeric primary keys - mysql

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.

Related

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]

concept of domains and subdomains for small database entities

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.

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.

Dynamic Columns In Reporting Services Tablix

I have a data query that returns data in the following format:
Name Period Value
-----------------------------
Bob Jan 123
Bob Feb 456
Bob Mar 789
Tom Jan 321
Tom Feb 654
Tom Mar 987
Joe Jan 147
Joe Feb 258
Joe Mar 369
The different periods are constant between names, but will be different between executions of the report (ie, I may query a report on Jan/Feb/Mar now, or Apr/May/Jun later). I'm trying to put that into a table in my Reporting Services report, that would look something like this:
Name Jan Feb Mar
----------------------------
Bob 123 456 789
Tom 321 654 987
Joe 147 258 369
Can anyone point me to an example of doing something like that? I'm not even sure how to describe that 'rotation'(?) of the data. The columns should be dynamic based on what Period values are in the dataset.
Found the answer right after posting. Here's what I did.
Created a new Tablix. Dragged the Name field to the data row of the first column. Dragged the Value column to the data row of the second column. Dragged the Period field to the header row of the second column. That created a new group, and a new second level header column. I then copied the value from that new top header column to the header cell below it, and deleted the whole new header row. When asked whether to delete the row and associated groups, or just row, choose just the row. You should be left with a Row Group and two Column Groups, and it should render as intended.

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.