Sample Header Ad - 728x90

Database Administrators

Q&A for database professionals who wish to improve their database skills

Latest Questions

2 votes
3 answers
4838 views
Naming foreign key columns
Now I hear that the best practice for naming foreign key columns is the name of the table and field (and maybe the function if there are multiple foreign keys to the same table), for example, a foreign key that references the users table id column would be: creatorUserId Now the thing is that right...
Now I hear that the best practice for naming foreign key columns is the name of the table and field (and maybe the function if there are multiple foreign keys to the same table), for example, a foreign key that references the users table id column would be: creatorUserId Now the thing is that right now I am building my application and for the sake of development speed, I am going to be referencing everything by a surrogate foreign key (INT AUTO_INCREMENT). I really don't want to get into the whole which is faster than what in which case this early in development, I rather leave that stuff to the end (to try to prevent premature optimizations and I am more interested in working on the front-end of this application then the backend). With that being said, a concern I have is if I in-fact want to change some of the foreign keys from the surrogate key to a natural key (all relavent tables will have both) and I use column names like creatorUserId I am going to have to go through all the backend code in order to make the change, a process that would not be very pleasant. I was thinking about doing something that just use the function and table name for foreign keys column like: creatorUser In the case, if I change what column I am using for the foreign key, the backend code changes are a lot simpler and there would be a lot less of them (if you are using an ORM which I am). Also, since surrogate keys are generally numbers and natural keys are generally alpha characters, it should still be relative easy to know which one is being used just by looking at the value. Would this be considered bad practice?
ryanzec (505 rep)
Feb 15, 2013, 12:37 AM • Last activity: Sep 30, 2024, 12:49 AM
21 votes
3 answers
9781 views
Column name naming conventions and best practices
I would like some expert opinion on best practices when it comes to **column naming**. The background is that [according to Wikipedia](http://en.wikipedia.org/wiki/Join_(SQL)#Equi-join), the following syntax, SELECT ... FROM Employees JOIN Timesheets USING (EmployeeID); is more efficient than SELECT...
I would like some expert opinion on best practices when it comes to **column naming**. The background is that [according to Wikipedia](http://en.wikipedia.org/wiki/Join_(SQL)#Equi-join) , the following syntax, SELECT ... FROM Employees JOIN Timesheets USING (EmployeeID); is more efficient than SELECT ... FROM Employees JOIN Timesheets ON (Employees.EmployeeID = Timesheets.EmployeeID); However, the JOIN ... USING syntax only works of *all primary key columns have globally unique names*. Thus I wonder if this is considered The Right Thing to do. Personally, I always used to create tables with PK column id, and foreign key column othertable_id. But that way it's not possible to use USING or NATURAL JOIN. Any links to design styles or best practice guides for table design would be appreciated, too!
Kerrek SB (313 rep)
Jul 7, 2011, 03:28 AM • Last activity: Jun 12, 2024, 07:11 PM
28 votes
7 answers
8132 views
Is table aliasing a bad practice?
I remember learning to do this in a DBMS course for Master of Information Services students. To save yourself some typing, you can type: SELECT t1.id, t2.stuff FROM someTable t1 INNER JOIN otherTable t2 ON t1.id=t2.id ; But... Why is this acceptable in stored procedures and such? It seems like all i...
I remember learning to do this in a DBMS course for Master of Information Services students. To save yourself some typing, you can type: SELECT t1.id, t2.stuff FROM someTable t1 INNER JOIN otherTable t2 ON t1.id=t2.id ; But... Why is this acceptable in stored procedures and such? It seems like all it does is harm the readability of the statement while saving an extremely minor amount of time. Is there any functional or logical reason to do this? It seems to add ambiguity rather than remove it; the only acceptable reason I can see for using this format is if you were adding a semantically meaningful alias -- for example, FROM someTable idsTable -- when the table name isn't descriptive enough. Is table aliasing a bad practice or is this just a misuse of a helpful system?
Zelda (2103 rep)
Sep 20, 2011, 02:17 PM • Last activity: May 15, 2024, 08:04 AM
0 votes
1 answers
469 views
How to name tables which are responsible for storing dictionaries and configuration?
Below I attached diagram which corresponds to part of my database : [![enter image description here][1]][1] [1]: https://i.sstatic.net/8SzFJ.png My questions: 1. In your opininon Do I have to divide `ServiceInsance` and `ConfServiceInstance` ? I did it like that because I would like to have separate...
Below I attached diagram which corresponds to part of my database : enter image description here My questions: 1. In your opininon Do I have to divide ServiceInsance and ConfServiceInstance ? I did it like that because I would like to have separated IDs and Configuration columns. 2. What do you think about name of these tables ? I mean Is it worth to have Dict and Conf in table names ? 3. Is it a problem that PK does not corespond to table name by convention: TableName + ID in Conf and Dict tables ? 4. Is it a better idea to create schemas conf and dict maybe ?
axdna (119 rep)
Apr 8, 2020, 06:30 AM • Last activity: Jan 5, 2024, 07:02 AM
0 votes
2 answers
277 views
Why should I manually name foreign key constraints?
I see people talking about the best way to name foreign key constraints. I have never manually named them, I just get whatever my RDBMS gives it, like `tableName_ibfk_1`. If I've never seen a need to name them, should I care about this? Why do some people care?
I see people talking about the best way to name foreign key constraints. I have never manually named them, I just get whatever my RDBMS gives it, like tableName_ibfk_1. If I've never seen a need to name them, should I care about this? Why do some people care?
aswine (153 rep)
Aug 24, 2023, 02:18 PM • Last activity: Aug 24, 2023, 03:07 PM
20 votes
2 answers
9632 views
Naming conflict between function parameter and result of JOIN with USING clause
Given this setup in current Postgres 9.4 ([from this related question][1]): CREATE TABLE foo (ts, foo) AS VALUES (1, 'A') -- int, text , (7, 'B'); CREATE TABLE bar (ts, bar) AS VALUES (3, 'C') , (5, 'D') , (9, 'E'); *db fiddle [here](https://dbfiddle.uk/?rdbms=postgres_13&fiddle=1cbb193db0f3662c4192...
Given this setup in current Postgres 9.4 (from this related question ): CREATE TABLE foo (ts, foo) AS VALUES (1, 'A') -- int, text , (7, 'B'); CREATE TABLE bar (ts, bar) AS VALUES (3, 'C') , (5, 'D') , (9, 'E'); *dbfiddle [here](https://dbfiddle.uk/?rdbms=postgres_13&fiddle=1cbb193db0f3662c4192ec82b0f5e609)* (also from the previous question). I wrote a SELECT with a FULL JOIN to achieve the objective of the referenced question. Simplified:
SELECT ts, f.foo, b.bar
FROM   foo f
FULL   JOIN bar b USING (ts);
As per specifications, the correct way to address the column ts is without table qualification. Either of the *input* values (f.ts or b.ts) can be NULL. The USING clause creates a bit of an odd case: introducing an "input" column that's not actually present in the input. So far so elegant. I put this in a plpgsql function. For convenience (or requirements) I want the same column names for the result of the table function. So we have to avoid naming conflicts between identical column names and function parameters. Should best be avoided by picking different names, but here we are:
CREATE OR REPLACE FUNCTION f_merge_foobar()
  RETURNS TABLE(ts int, foo text, bar text)
  LANGUAGE plpgsql AS
$func$
BEGIN
   FOR ts, foo, bar IN
      SELECT COALESCE(f.ts, b.ts), f.foo, b.bar
      FROM   foo f
      FULL   JOIN bar b USING (ts)
   LOOP
      -- so something
      RETURN NEXT;
   END LOOP;
END
$func$;
Bold emphasis to highlight the **problem**. I can't use ts without table qualification like before, because plpgsql would raise an exception (not strictly necessary, but probably useful in most cases): > ERROR: column reference "ts" is ambiguous > LINE 1: SELECT ts, f.foo, b.bar > ^ > DETAIL: It could refer to either a PL/pgSQL variable or a table column. I know I can use different names or a subquery or use another function. But I wonder if there's a way to reference the column. I can't use table-qualification. One would think there ***should*** be a way. Is there?
Erwin Brandstetter (185717 rep)
Jul 2, 2015, 11:54 PM • Last activity: Aug 1, 2023, 03:50 PM
70 votes
9 answers
46017 views
Plural vs Singular Table Name
**How should I name my Tables when creating a new database?** Singular: `Client` or Plural: `Clients`?
**How should I name my Tables when creating a new database?** Singular: Client or Plural: Clients?
John Isaiah Carmona (803 rep)
Feb 23, 2012, 08:01 AM • Last activity: Jun 5, 2023, 05:49 PM
23 votes
9 answers
13777 views
Is there a reason to use extremely abbreviated table names?
We're using a database setup from a vendor's application that has horrifically hard to read database table names, and no documentation on what is stored where. I can see why one might want to obfuscate their table structure in a proprietary app, but one of the selling points of this application (Ent...
We're using a database setup from a vendor's application that has horrifically hard to read database table names, and no documentation on what is stored where. I can see why one might want to obfuscate their table structure in a proprietary app, but one of the selling points of this application (Enterprise Resource Planning) was it's customizability. Table names are like aptrx (Accounts Payable Transactions) and apmaster_all (curiously, this is the vendors table). It's an extremely complex database, so I was wondering if there was any logic to the convention or if it was simply being obfuscated intentionally or otherwise. To the best of my knowledge the length of the table name won't affect performance noticeably, correct? The database is very complex (hundreds of tables) so sorting makes sense, but I can't imagine why AccountsPayableTransactions isn't preferable to aptrx....
Zelda (2103 rep)
Sep 9, 2011, 07:57 PM • Last activity: Apr 4, 2023, 08:21 PM
-1 votes
1 answers
78 views
Is there any pattern behind the naming conventions of PostgreSQL Table-Level Locks?
I know that PostgreSQL has 8 level of table locks which are, from least restrictive to most: 1. ACCESS SHARE 2. ROW SHARE 3. ROW EXCLUSIVE 4. SHARE UPDATE EXCLUSIVE 5. SHARE 6. SHARE ROW EXCLUSIVE 7. EXCLUSIVE 8. ACCESS EXCLUSIVE However, I can't really determine any pattern or convention to these n...
I know that PostgreSQL has 8 level of table locks which are, from least restrictive to most: 1. ACCESS SHARE 2. ROW SHARE 3. ROW EXCLUSIVE 4. SHARE UPDATE EXCLUSIVE 5. SHARE 6. SHARE ROW EXCLUSIVE 7. EXCLUSIVE 8. ACCESS EXCLUSIVE However, I can't really determine any pattern or convention to these names. There are literally 5 words total: ACCESS, SHARE, ROW, EXCLUSIVE, and UPDATE, which are grouped together, seemingly at random, for all 8 levels. I find myself preferring to refer to them as just L1 through L8 rather than their actual names. Are these names just the result of historical conventions that evolved organically in a way which doesn't really have any meaning besides "look at the docs and see what level each is?"
DaleSwanson (113 rep)
Dec 7, 2022, 01:18 AM • Last activity: Dec 7, 2022, 08:33 AM
90 votes
13 answers
86134 views
How should I best name my timestamp fields?
When I am looking to create some timestamp fields (or other date/time style fields), what is the best way to name them? Should I just put record_timestamp?
When I am looking to create some timestamp fields (or other date/time style fields), what is the best way to name them? Should I just put record_timestamp?
garik (6782 rep)
May 12, 2011, 12:29 PM • Last activity: Sep 12, 2022, 06:57 PM
16 votes
2 answers
40515 views
Should I not use camelCase in my column names?
I am creating a GraphQL API using Node.js, which foces me to return all the field names in `camelCase`. In my PostgreSQL database, I currently have all my columns named following a `camelCase` convention, but I am thinking: is that the best idea? Should I use `snake_case` in the database columns and...
I am creating a GraphQL API using Node.js, which foces me to return all the field names in camelCase. In my PostgreSQL database, I currently have all my columns named following a camelCase convention, but I am thinking: is that the best idea? Should I use snake_case in the database columns and convert them in the back-end?
Luiz Felipe (269 rep)
Oct 13, 2019, 02:06 AM • Last activity: Jul 19, 2022, 12:26 PM
1 votes
1 answers
1207 views
What does the "wi" stand for in SQL Azure Automatic Index Naming?
I have a SQL Database on Azure that is giving me a performance recommendation for an index. What does the "wi" part of the name? I'm guessing something to do with the include part of the statement. With Include? ``` CREATE NONCLUSTERED INDEX [nci_wi_TableName_468BA2FCEC4455FB9A6ACA94430E4935] ON [db...
I have a SQL Database on Azure that is giving me a performance recommendation for an index. What does the "wi" part of the name? I'm guessing something to do with the include part of the statement. With Include?
CREATE NONCLUSTERED INDEX [nci_wi_TableName_468BA2FCEC4455FB9A6ACA94430E4935] ON [dbo].[TableName] ([Id]) INCLUDE ([ColA], [ColB]) WITH (ONLINE = ON)
JC5577 (625 rep)
May 26, 2021, 12:03 PM • Last activity: Jun 15, 2022, 11:51 AM
20 votes
2 answers
12020 views
Should I call my UUID primary key column ID or not?
I'm working on a database design that extensively uses `UUID`s for `PRIMARY KEY`s. However, this confronts me with a very consequential choice. How do I name these columns? I would call them `uuid`, except that, `UUID` being an identifier, I then have to quote the field name everywhere: ``` CREATE T...
I'm working on a database design that extensively uses UUIDs for PRIMARY KEYs. However, this confronts me with a very consequential choice. How do I name these columns? I would call them uuid, except that, UUID being an identifier, I then have to quote the field name everywhere:
CREATE TABLE thingie (
  "uuid" UUID PRIMARY KEY DEFAULT public.gen_random.uuid(),
  foo VARCHAR,
  bar VARCHAR,
);
A straight-forward alternative seems to be to instead call these columns id:
CREATE TABLE thingie (
  id UUID PRIMARY KEY DEFAULT public.gen_random.uuid(),
  foo VARCHAR,
  bar VARCHAR,
);
That way, I don't have the column name, and semantically I can argue that a UUID is indeed a sort of ID; in a Venn diagram, the UUID circle would be placed wholly in the ID circle. *However*, I (and I am sure many others with) have grown so accustomed to associating id with an auto-incrementing INTEGER column that I'm afraid to break some sort of unspoken rule by calling these IDs ids. If you would brighten up my confusion with some solid bike-shedding, I would be much obliged. Indeed my question is: **How would _you_ call your UUID-typed surrogate keys and why?**
BigSmoke (814 rep)
Apr 24, 2022, 09:33 AM • Last activity: Apr 26, 2022, 09:31 AM
108 votes
18 answers
45469 views
Is adding the ‘tbl’ prefix to table names really a problem?
I’m watching some Brent Ozar videos ([like this one, for instance][1]) and he suggests not prefixing tables with `‘tbl’` or `‘TBL’`. On the internet I found some blogs saying it adds nothing to documentation, and also that “it takes longer to read it”. **Questions and considerations** - **It this re...
I’m watching some Brent Ozar videos (like this one, for instance ) and he suggests not prefixing tables with ‘tbl’ or ‘TBL’. On the internet I found some blogs saying it adds nothing to documentation, and also that “it takes longer to read it”. **Questions and considerations** - **It this really a problem?** Because I’m prefixing tables with ‘tbl’ since my first dba job (the senior DBA told me to do that for organization). - **Is this something that I need to get rid of?** I made some tests, copying a really big table and giving it the ‘tbl’ prefix, while keeping the other one without it, and I didn’t notice any performance issue.
Racer SQL (7546 rep)
Nov 4, 2016, 12:13 PM • Last activity: Mar 21, 2022, 03:30 PM
5 votes
1 answers
4953 views
Naming conventions for views and materialized views: "*_v", "v_*", "*_mv", "mv_*" or none?
My view stores quality metrics for various gizmos 1 in the database. Today I designed it to be a view. In a few years, as we produce more gizmos, it may need to be changed to a materialized view. I was thinking of naming the view either of these 3 ways: `gizmo_quality_metrics` `gizmo_quality_metrics...
My view stores quality metrics for various gizmos1 in the database. Today I designed it to be a view. In a few years, as we produce more gizmos, it may need to be changed to a materialized view. I was thinking of naming the view either of these 3 ways: gizmo_quality_metrics gizmo_quality_metrics_v v_gizmo_quality_metrics Which one should I choose? This may need to be renamed (or not) a few years later, when switching it to a materialized view: gizmo_quality_metrics gizmo_quality_metrics_mv mv_gizmo_quality_metrics From the way I worded the question, you get the idea that I favor keeping the same name (gizmo_quality_metrics), which is based on the data, and disregard the implementation (thus, no prefixes and extensions). However, I have seen all of the above naming conventions in practice, and I am wondering what is the most maintainable one for the long term. --- 1 I am simplifying - of course, they are not gizmos. They are actually deoxyribonucleic acid (DNA) molecules, if you really want to know. But this is irrelevant. --- **[EDIT, Mar 18, 2022]** Related question (thanks to *a_horse_with_no_name* for mentioning it!): https://dba.stackexchange.com/q/154251/192715 Note that I do not link to any one particular answer in that literal *wall* of answers, from high to low rep users. It is instructive to read a few, though.
Timur Shtatland (125 rep)
Mar 17, 2022, 09:47 PM • Last activity: Mar 18, 2022, 02:13 PM
1 votes
1 answers
263 views
Proper name for reference tables in OLTP
I know this might be a silly question. But I really appreciate accepting and answering this question. When I have an OLTP database with some big tables like orders and items. I have other tables (similar to dimension tables in OLAP) where we store reference values, such as: currency, city, category....
I know this might be a silly question. But I really appreciate accepting and answering this question. When I have an OLTP database with some big tables like orders and items. I have other tables (similar to dimension tables in OLAP) where we store reference values, such as: currency, city, category. What is the proper name for those reference tables? Can we still call them dimension tables in OLTP or do they have their own generic name?
Lamar (123 rep)
Jan 14, 2021, 08:14 AM • Last activity: Jan 14, 2021, 12:17 PM
7 votes
2 answers
23149 views
PostgreSQL naming conventions
Maybe double post but I could not find any answer here. I saw couple of posts how to name my tables and I understood that (semi-officially) it should contain lower cases, should be underscored and singular (f.e. my_table) My question arose when I had to do the following. I have an app where I have u...
Maybe double post but I could not find any answer here. I saw couple of posts how to name my tables and I understood that (semi-officially) it should contain lower cases, should be underscored and singular (f.e. my_table) My question arose when I had to do the following. I have an app where I have user, platform and user_info, where platform is what the user is working on (f.e. MAC, PC, LINUX). If I need to create junction table between user and platform, how do I name it in order the next person browsing the schema to understand that user and platform have a junction table named user_platform, but to not be confused when he sees the table user_info? BR
Pentarex (73 rep)
Aug 17, 2019, 11:56 PM • Last activity: Dec 27, 2020, 04:25 PM
3 votes
1 answers
434 views
Can we change Log Shipping backup file naming convention
Seems the Log Shipping backup files naming convention (especially the date-time stamp in the name) is followed by UTC time, i believe that is default and standard. I would like to clarify on following cases: 1. I'm wondering if there any possibility to change the naming convention to follow the date...
Seems the Log Shipping backup files naming convention (especially the date-time stamp in the name) is followed by UTC time, i believe that is default and standard. I would like to clarify on following cases: 1. I'm wondering if there any possibility to change the naming convention to follow the date-time as per local time-zone from the server where the BACKUP performed. 2. Is the naming convention (UTC time stamp as suffix) really matter, since anyway the Log Shipping copy/restore job considers the LSN between each backup file to maintain valid log backup chain. The aim is to avoid issues during compressing process that i'm following to maintain one zip file per day, and as per following screenshot it would combine additional files (20191015xxxxx) which are belongs different date (2019-10-16) enter image description here
Shekar Kola (2477 rep)
Oct 16, 2019, 09:47 AM • Last activity: Oct 31, 2020, 12:31 PM
2 votes
1 answers
334 views
Does the SQL-1992 standard restrict naming identifiers to 18 characters?
In [this draft of SQL:1992][1], I found… Section 5.2 > Leveling Rules > > .... > > 2. The following restrictions apply for Entry SQL in addition to any Intermediate SQL restrictions: > a) No ` ` or ` ` **shall contain more than 18 ` `s**. …and… > Annex A.1 > 1. Subclause 5.2, "` ` and ` `": > a) In...
In this draft of SQL:1992 , I found… Section 5.2 > Leveling Rules > > .... > > 2. The following restrictions apply for Entry SQL in addition to any Intermediate SQL restrictions: > a) No ` or **shall contain more than 18 `s**. …and… > Annex A.1 > 1. Subclause 5.2, "` and `": > a) In conforming Intermediate SQL language, a ` or a **shall not comprise more than 18 `s.** > b) A ` shall not be a or a `. > c) Conforming Intermediate SQL language shall contain no ` that ends in an `. ## For *implementors* of SQL, or for *users* of SQL? I searched the document multiple times, but could never get a sense of the meaning of identifier. - Is the spec referring to labeling within an *implementation* of SQL (such as PostreSQL or Oracle)? So labels used within the SQL product cannot contain identifiers that are over 18 characters or end in an underscore. - Or is the spec referring to *users* of a SQL implementation? So we who are naming our schemas, tables, columns, and other such database objects cannot use names over 18 characters or end in an underscore?
Basil Bourque (11188 rep)
Apr 23, 2018, 05:10 AM • Last activity: Aug 25, 2020, 08:03 AM
2 votes
1 answers
351 views
Data modelling for specific units of measurement
I am currently adding a feature in our software where we save a timeout in the database to initialize cronjobs which get active after that timeout. To model this I see different ways but can't say which of them is the cleanest: 1. Create 2 columns "timeout" and "timeout_measure", where *timeout_meas...
I am currently adding a feature in our software where we save a timeout in the database to initialize cronjobs which get active after that timeout. To model this I see different ways but can't say which of them is the cleanest: 1. Create 2 columns "timeout" and "timeout_measure", where *timeout_measure* is a MySQL Enum with seconds, minutes, hours, etc. 2. Create 1 column "timeout" where the timeout is always stored in seconds Even though I tended to the 1. way at first I saw the problem that additionally to this second column a MySQL trigger would be needed to check that *if a timeout is set, the timeout_measure cannot be NULL* , which would be an unnecessary performance hit as far as I am concerned, because we can implement the same functionality with only one column where we save the seconds of the timeout. So I am now tending to the 2. way but I don't like the readability of this. Having a column like so with entries in seconds, one might not understand but only guess what this means without reading a documentation of this table. | timeout | | 3600 | | 25200 | | 86400 | So I thought about saving the unit of measurement in the column name like *timeout_in_secs*. But then I am curious if this wouldn't be kind of boilerplate because if there is a documentation this additional info would not be needed in each query made and whenever the column is referenced at in the code. So whenever one wants to reference to the column of this entity in the sourcecode (we use NodeJS and Sequelize as ORM) he would need to write obj.timeoutInSecs instead of just obj.timeout. What to do? I don't want to change the whole table structure once this feature is productive, since this could end up in a big mess. Do you think it suffices to have this documented in the database documentation, or would you still go to choose *timeout_in_secs* as the column name? EDIT: I know this might be opinion based but in this case different opinions and experiences might help a lot. I am on my own in this decision and simply can't decide which solution is the best/cleanest.
Vegaaaa (121 rep)
Mar 8, 2018, 09:52 AM • Last activity: Jul 23, 2020, 02:02 PM
Showing page 1 of 20 total questions