0

I'm basically new to SQL, and I was making my own project when this question came to my mind.
I'm making a database that has an entity where an attribute would be a "number" and a "year".
In many programming languages one would create a new object or data type that takes both the number and the year. (say a tuple with INT, YEAR)
I wanted to know if this makes sense in SQL or I should just go with an INT and YEAR attribute.

To put more details into the project, I'm creating a DB for my mom who runs a business that goes with "campaings" (the INT value) directly associated with the "Year", so one would put the campaign 09 for year 2020, and so on.
I wanted to create a new Data Type for the attribute so then I don't have multiple "campaign 09" with different years.

I hope my question is clear, thanks!

EDIT: Read the comments for the answer.

4
  • 1
    please make a minimal reproducible example for your problem, you don't make datqa types in sql at least not in myslq, you can makle a new column, where you concatinate all 2 or three column) but usually you only use a selectto do that
    – nbk
    Commented Apr 25, 2021 at 16:38
  • The term you are looking for is "user defined type" (UDT). MySQL does not support UDTs, but many real DBMSes do. Some of them are free (Postgres), some other offer free development licenses.
    – mustaccio
    Commented Apr 25, 2021 at 16:45
  • Ahh, thanks mustaccio. That makes sense. Commented Apr 25, 2021 at 17:14
  • User-defined datatype which is created for external data implementation only makes no sense. Store your data in two columns of INT and YEAR datatypes, and serialize/deserialize your object during storing/retrieving.
    – Akina
    Commented Apr 25, 2021 at 19:26

1 Answer 1

1

This isn't something that requires a custom data type to solve.

If the requirement is Campaign 09 can only be associated with one year, then Year is simply an attribute for Campaign (although I'd argue perhaps StartDt or something similar, from which year can be derived might be more appropriate).

To illustrate:

CREATE TABLE Campaign
(
  CampaignNbr  INT   NOT NULL
 ,StartDt      DATE  NOT NULL
 /* Everything else */
 ,CONSTRAINT PK_Campaign PRIMARY KEY (CampaignNbr)
)
1
  • Huh, I guess I still have much to learn, thank you bbaird. Commented Apr 27, 2021 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.