932

Whenever I design a database, I always wonder if there is a best way of naming an item in my database. Quite often I ask myself the following questions:

  1. Should table names be plural?
  2. Should column names be singular?
  3. Should I prefix tables or columns?
  4. Should I use any case in naming items?

Are there any recommended guidelines out there for naming items in a database?

8
  • 8
    I think we should name plural for Tables and singular for columns.
    – AZ_
    Commented May 5, 2011 at 6:04
  • 8
    I see a table as "storage" with multiple items, not single "entity" so I name it plural. When I mapped tables into objects, I would name the objects singular. This is just my personal opinion.
    – kazinix
    Commented Jul 19, 2013 at 2:40
  • 4
    @Tryinko Using ID all over the place is LIVING HELL for anyone doing joins of multiple tables. There's no possible way that the slight advantage of knowing this is the PK outweighs the incredible annoyance of re-aliasing the dang ID column in every bloody query over and over again. If you want a way to denote PK in a table, make it the first column. Also, denoting FKs in the names of columns is in my mind another solidly evil anti-pattern.
    – ErikE
    Commented Feb 2, 2016 at 23:05
  • 2
    Have a look at this Answer. Commented Feb 6, 2019 at 9:16
  • 2
    About the casing, I suggest snake_case, so you don't have to worry about capitalization of acronyms like it happens with PascalCase. Eg: PHPVersion or PhpVersion? In snake case it's clearly php_version, etc. Commented Aug 17, 2021 at 12:45

23 Answers 23

387

I recommend checking out Microsoft's SQL Server sample databases: https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks

The AdventureWorks sample uses a very clear and consistent naming convention that uses schema names for the organization of database objects.

  1. Singular names for tables
  2. Singular names for columns
  3. Schema name for tables prefix (E.g.: SchemeName.TableName)
  4. Pascal casing (a.k.a. upper camel case)
17
  • 17
    wilsonmar.com/sql_adventureworks.htm is an excellent analysis of the AdventureWorks schema. Commented Jan 11, 2011 at 0:40
  • 262
    I wouldn't rely on Microsoft for any standard - if you look at their northwind database you'll see they use Plural Tables, Singular Column Names, Schema Prefixes for Tables, Table Prefixes for Primary Key Columns, Hungarian-esque Constraint Prefixes and worst of all SPACES " " for multi-word table names. Additionally system tables for SQLServer use plurals so it seems AdventureWorks was the black sheep in this bunch. Commented Mar 20, 2012 at 20:09
  • 88
    I think the main issue here is that the Singular table name crowd seem to consider the table as the entity, rather than the row in the table which the Plural crowd does. You have to ask your self which it is. If the table is just a container of rows, isn't it more logical to use plural naming? You would never name a collection in code singular, then why would you name the table singular? Why the inconsistency? I hear all the arguments about how they sort and use in joins but those all seem very flimsy arguments. If it all comes down to preference, I will go with the consistency and pluralize.
    – Jason
    Commented Apr 10, 2012 at 16:34
  • 6
    Also consider which direction the tide is going. It seems like its going in the direction of plural table names especially since all the system tables in SQL Server are all plural, and the default for Entity Framework is plural out of the box. If that's Microsoft's stance, I want to go the direction where we will be in 20 years. Even Oracle's database conventions say plural table names. Just think how many c# developers hated the "var" keyword when it was introduced, now its the widely accepted way to define variables.
    – Jason
    Commented Apr 10, 2012 at 16:39
  • 7
    @Jasmine - I see your point of view, though I think you inadvertently named your example table backwards. "TableOfInvoices" should be shortened to "Invoices," which is what I prefer. You probably instead meant "InvoiceTable," which makes sense to shorten "Invoice."
    – Derek
    Commented Mar 5, 2013 at 14:53
385

Late answer here, but in short:

  1. Plural table names: My preference is plural
  2. Singular column names: Yes
  3. Prefix tables or columns:
  • Tables: *Usually* no prefixes is best.
  • Columns: No.
  1. Use any case in naming items: PascalCase for both tables and columns.

Elaboration:

(1) What you must do. There are very few things that you must do a certain way, every time, but there are a few.

  • Name your primary keys using "[singularOfTableName]ID" format. That is, whether your table name is Customer or Customers, the primary key should be CustomerID.
  • Further, foreign keys must be named consistently in different tables. It should be legal to beat up someone who does not do this. I would submit that while defined foreign key constraints are often important, consistent foreign key naming is always important
  • You database must have internal conventions. Even though in later sections you'll see me being very flexible, within a database naming must be very consistent . Whether your table for customers is called Customers or Customer is less important than that you do it the same way throughout the same database. And you can flip a coin to determine how to use underscores, but then you must keep using them the same way. If you don't do this, you are a bad person who should have low self-esteem.

(2) What you should probably do.

  • Fields representing the same kind of data on different tables should be named the same. Don't have Zip on one table and ZipCode on another.
  • To separate words in your table or column names, use PascalCasing. Using camelCasing would not be intrinsically problematic, but that's not the convention and it would look funny. I'll address underscores in a moment. (You may not use ALLCAPS as in the olden days. OBNOXIOUSTABLE.ANNOYING_COLUMN was okay in DB2 20 years ago, but not now.)
  • Don't artifically shorten or abbreviate words. It is better for a name to be long and clear than short and confusing. Ultra-short names is a holdover from darker, more savage times. Cus_AddRef. What on earth is that? Custodial Addressee Reference? Customer Additional Refund? Custom Address Referral?

(3) What you should consider.

  • I really think you should have plural names for tables; some think singular. Read the arguments elsewhere. Column names should be singular however. Even if you use plural table names, tables that represent combinations of other tables might be in the singular. For example, if you have a Promotions and an Items table, a table representing an item being a part of a promotion could be Promotions_Items, but it could also legitimately be Promotion_Items I think (reflecting the one-to-many relationship).
  • Use underscores consistently and for a particular purpose. Just general tables names should be clear enough with PascalCasing; you don't need underscores to separate words. Save underscores either (a) to indicate an associative table or (b) for prefixing, which I'll address in the next bullet.
  • Prefixing is neither good or bad. It usually is not best. In your first db or two, I would not suggest using prefixes for general thematic grouping of tables. Tables end up not fitting your categories easily, and it can actually make it harder to find tables. With experience, you can plan and apply a prefixing scheme that does more good than harm. I worked in a db once where data tables began with tbl, config tables with ctbl, views with vew, proc's sp, and udf's fn, and a few others; it was meticulously, consistently applied so it worked out okay. The only time you NEED prefixes is when you have really separate solutions that for some reason reside in the same db; prefixing them can be very helpful in grouping the tables. Prefixing is also okay for special situations, like for temporary tables that you want to stand out.
  • Very seldom (if ever) would you want to prefix columns.
24
  • 13
    "Fields representing the same kind of data on different tables should be named the same. Don't have Zip on one table and ZipCode on another." Yes yes a million times yes. Can you tell our database was not designed that way? A personid might be refered to in any of a dozen different ways, very annoying to maintain. I've always kept to this rule in any database I had control over designing and it makes life much simpler.
    – HLGEM
    Commented Mar 26, 2010 at 14:09
  • 123
    I think the primary key should just be "ID". Such a simple convention makes the primary key predictable and quickly identifiable. I would, however, prepend the table name ("PersonID") when its used as a foreign key in other tables. This convention could help distinguish between a primary key and foreign keys in the same table.
    – Triynko
    Commented Apr 1, 2010 at 21:00
  • 69
    @Tryinko Using ID all over the place is LIVING HELL for anyone doing joins of multiple tables. There's no possible way that the slight advantage of knowing this is the PK outweighs the incredible annoyance of re-aliasing the dang ID column in every bloody query over and over again. If you want a way to denote PK in a table, make it the first column. Also, denoting FKs in the names of columns is in my mind another solidly evil anti-pattern.
    – ErikE
    Commented Jun 20, 2011 at 20:03
  • 20
    @Triynko if you use just "ID", it also become programatically impossible to determine the table it belongs to. With the table name prefix, you can simply cut off the last two digits of a primary key and know the table name it belongs to via code. A lot of times IT and DBA people don't realize that there are coding advantages for programmers in designing databases in certain ways.
    – dallin
    Commented Apr 29, 2013 at 21:19
  • 30
    @ErikE I mean you don't know whether CustomerID is the primary key from the Customer table, or a foreign key in some other table. It's a minor issue. Why would you want to use poor names like c? CustomerID = Customer.ID is very clear in that you see that you are joining a foreign key with a primary key; it is not redundant as the two sides are two different things. Single character naming is poor practice IMO. Commented Feb 2, 2016 at 22:21
105

Ok, since we're weighing in with opinion:

I believe that table names should be plural. Tables are a collection (a table) of entities. Each row represents a single entity, and the table represents the collection. So I would call a table of Person entities People (or Persons, whatever takes your fancy).

For those who like to see singular "entity names" in queries, that's what I would use table aliases for:

SELECT person.Name
FROM People person

A bit like LINQ's "from person in people select person.Name".

As for 2, 3 and 4, I agree with @Lars.

18
  • 18
    @Emtucifor: In English, we don't say "Look at all the person out there in that crowd of person!" Having a conceptual problem with things that are multiple being referred to by a singular word is to be expected. It's neither usual nor proper. "Data" is exceptional and often used to refer to a piece of a volume of substance, much like "cake". "Would you like (a piece of) cake?" Naming a table "People" because it contains information on multiple individuals makes far more sense than naming it "Person". A data class named "Person" for the ROW makes sense, as do singular column names.
    – Triynko
    Commented Apr 1, 2010 at 19:37
  • 7
    @Emtucifor: Ultimately all language is arbitrary and conventional. I was just arguing that conventionally we refer to a collection of items as the plural of the type of item therein. So a collection of rows where each row has information about a single person would be refferred to as a collection of People. But if you want to refer to it as a collection of Person, go right ahead.
    – Triynko
    Commented Apr 21, 2010 at 17:02
  • 4
    @Emtucifor: Yes, lol. Naming the table "PersonCollection" would be equivalent to naming it "People". Contrast that with naming such a collection just "Person", which does not make sense :)
    – Triynko
    Commented Apr 22, 2010 at 15:04
  • 4
    @Emtucifor: Then let's think of it from another angle to put the naming convention in a context. Suppose you have object classes for representing both the row and the table. "Person" obviously makes sense for the class that represents a row of data. If you're table was also named "Person", then you might have a naming conflict or some confusion. I just think that it makes more sense to name objects with accurate plurality. A row with data about a person should be called Person, and a table with information about people or multiple persons is called People, PersonCollection, Persons, etc.
    – Triynko
    Commented Apr 23, 2010 at 16:15
  • 5
    @Josh M. Well, not either way you go. If you go with my way you can alias the People table as "person" and have SELECT person.Name. Problem solved. ;-) Commented Nov 12, 2010 at 4:48
85

I work in a database support team with three DBAs and our considered options are:

  1. Any naming standard is better than no standard.
  2. There is no "one true" standard, we all have our preferences
  3. If there is standard already in place, use it. Don't create another standard or muddy the existing standards.

We use singular names for tables. Tables tend to be prefixed with the name of the system (or its acronym). This is useful if the system complex as you can change the prefix to group the tables together logically (ie. reg_customer, reg_booking and regadmin_limits).

For fields we'd expect field names to be include the prefix/acryonm of the table (i.e. cust_address1) and we also prefer the use of a standard set of suffixes ( _id for the PK, _cd for "code", _nm for "name", _nb for "number", _dt for "Date").

The name of the Foriegn key field should be the same as the Primary key field.

i.e.

SELECT cust_nm, cust_add1, booking_dt
FROM reg_customer
INNER JOIN reg_booking
ON reg_customer.cust_id = reg_booking.cust_id

When developing a new project, I'd recommend you write out all the preferred entity names, prefixes and acronyms and give this document to your developers. Then, when they decide to create a new table, they can refer to the document rather than "guess" what the table and fields should be called.

6
  • 9
    Especially for number 3, we had a agroup of folks who all got hired from the same company and they tried to impose their old naming standard (which none of the rest of us used) on anything they did. Very annoying.
    – HLGEM
    Commented Jan 22, 2010 at 16:11
  • 46
    Certainly makes the SQL unreadable; but i think i can translate. cust_nm should be CustomerName, booking_dt should be BookingDate. reg_customer, well i have no idea what that is.
    – Ian Boyd
    Commented Jan 22, 2010 at 23:17
  • 3
    @Ian. The intention is that you stick to the naming convension your used to, and keep it consistent. I ALWAYS know that any date field is _dt, any name field is _nm. 'reg' is an example, of a "registration" system (bookings, customers etc) and all the related tables would have the same prefix. But each to their own...
    – Guy
    Commented Jan 25, 2010 at 19:30
  • 9
    i agree that a particular standard is not as important as having a consistent standard. But some standards are wrong. DB2 and column names like CSPTCN, CSPTLN, CSPTMN, CSDLN. People should learn that long names have been invented - we can afford to make things readable.
    – Ian Boyd
    Commented Jan 26, 2010 at 0:13
  • 20
    Throughout the years, I have added new columns at the end of my tables in the app I developed and market. Sometimes, I use english names in my columns, sometimes I use spanish and sometimes I re-use columns for something else, instead of deleting them and adding a new column with a proper descriptive name for what it is used. I purposely did this in order to OBFUSCATE my source code in case someone else tries to hack or reverse-engineer my code. Only I can understand it, someone else will get frustrated!..This way, they always have to rely on me for anything!
    – Joe R.
    Commented Nov 11, 2010 at 6:15
57
  1. No. A table should be named after the entity it represents. Person, not persons is how you would refer to whoever one of the records represents.
  2. Again, same thing. The column FirstName really should not be called FirstNames. It all depends on what you want to represent with the column.
  3. NO.
  4. Yes. Case it for clarity. If you need to have columns like "FirstName", casing will make it easier to read.

Ok. Thats my $0.02

10
  • 5
    Adding some clarity to number 3 - prefixes are a way of embedding metadata into the column name. There should be no need to do this in any modern DB for the same reasons as (overuse of) Hungarian notation. Commented Jan 4, 2010 at 14:13
  • 32
    `select top 15 from order' or 'select top 15 from orders'? The latter is my (human) preference.
    – Ian Boyd
    Commented Jan 22, 2010 at 23:15
  • 11
    @Ian Boyd: Yep: SELECT TOP 100 * FROM Report R INNER JOIN VisitReport VR ON R.ReportID = VR.ReportID. It all depends on how you think about it. If you put a picture of a lemon on a canister, you'd know there were lemons inside, without needing two lemons on the outside to indicate that it could be plural. Sure, you might label it with the written word "lemons." But it might just as well be "lemon". To acquire the resource named "lemon", go here.
    – ErikE
    Commented Jan 29, 2010 at 0:35
  • 6
    add $0.01 for using UpperCase in column names and add another $0.01 for using underscore in column names so that its easier to distinguish column names in plain sight. Total = My $0.02 donation to you!
    – Joe R.
    Commented Nov 11, 2010 at 6:04
  • 7
    "A table should be named after the entity it represents" A table is a collection of entities. While a table is also an entity, it is an entity of type "Table" which is pointless to add to its name.
    – Trisped
    Commented Apr 9, 2012 at 19:01
53

I hear the argument all the time that whether or not a table is pluralized is all a matter of personal taste and there is no best practice. I don't believe that is true, especially as a programmer as opposed to a DBA. As far as I am aware, there are no legitimate reasons to pluralize a table name other than "It just makes sense to me because it's a collection of objects," while there are legitimate gains in code by having singular table names. For example:

  1. It avoids bugs and mistakes caused by plural ambiguities. Programmers aren't exactly known for their spelling expertise, and pluralizing some words are confusing. For example, does the plural word end in 'es' or just 's'? Is it persons or people? When you work on a project with large teams, this can become an issue. For example, an instance where a team member uses the incorrect method to pluralize a table he creates. By the time I interact with this table, it is used all over in code I don't have access to or would take too long to fix. The result is I have to remember to spell the table wrong every time I use it. Something very similar to this happened to me. The easier you can make it for every member of the team to consistently and easily use the exact, correct table names without errors or having to look up table names all the time, the better. The singular version is much easier to handle in a team environment.

  2. If you use the singular version of a table name AND prefix the primary key with the table name, you now have the advantage of easily determining a table name from a primary key or vice versa via code alone. You can be given a variable with a table name in it, concatenate "Id" to the end, and you now have the primary key of the table via code, without having to do an additional query. Or you can cut off "Id" from the end of a primary key to determine a table name via code. If you use "id" without a table name for the primary key, then you cannot via code determine the table name from the primary key. In addition, most people who pluralize table names and prefix PK columns with the table name use the singular version of the table name in the PK (for example statuses and status_id), making it impossible to do this at all.

  3. If you make table names singular, you can have them match the class names they represent. Once again, this can simplify code and allow you to do really neat things, like instantiating a class by having nothing but the table name. It also just makes your code more consistent, which leads to...

  4. If you make the table name singular, it makes your naming scheme consistent, organized, and easy to maintain in every location. You know that in every instance in your code, whether it's in a column name, as a class name, or as the table name, it's the same exact name. This allows you to do global searches to see everywhere that data is used. When you pluralize a table name, there will be cases where you will use the singular version of that table name (the class it turns into, in the primary key). It just makes sense to not have some instances where your data is referred to as plural and some instances singular.

To sum it up, if you pluralize your table names you are losing all sorts of advantages in making your code smarter and easier to handle. There may even be cases where you have to have lookup tables/arrays to convert your table names to object or local code names you could have avoided. Singular table names, though perhaps feeling a little weird at first, offer significant advantages over pluralized names and I believe are best practice.

2
  • 6
    This is excellent reasoning. The debate seems to be whether to match the collection name or the class name, and this is a great explanation of the class name approach. Thank you!
    – Evan Moran
    Commented Aug 5, 2021 at 17:37
  • 2
    Brilliantly explained! Some SQL people are so used to thinking in Set-Based operations; they incorrectly think of Table-Schema as a literal group of Records! In OO (Object-Oriented) Terms: A Table-Schema is analogous to a Type/Class-Definition, a Property/Field to a Column, a Record to an Object, and a Table to a Collection (of Objects). e.g. The Class "Person" is just a "Type", not even an Object, just like how the Table-Schema is an abstract Template to hold your data. The Person p; Object is representative of 1 Record and the List<Person> lp; Collection is a Select of 0+ Records.
    – MikeTeeVee
    Commented Mar 23, 2023 at 22:52
36

I'm also in favour of a ISO/IEC 11179 style naming convention, noting they are guidelines rather than being prescriptive.

See Data element name on Wikipedia:

"Tables are Collections of Entities, and follow Collection naming guidelines. Ideally, a collective name is used: eg., Personnel. Plural is also correct: Employees. Incorrect names include: Employee, tblEmployee, and EmployeeTable."

As always, there are exceptions to rules e.g. a table which always has exactly one row may be better with a singular name e.g. a config table. And consistency is of utmost importance: check whether you shop has a convention and, if so, follow it; if you don't like it then do a business case to have it changed rather than being the lone ranger.

2
  • 2
    -1: The referenced text has nothing to do with ISO/IEC 11179. The referenced wikipedia page should not be trusted; read the actual standard instead (metadata-standards.org/11179/#A5)
    – mkadunc
    Commented Jun 29, 2012 at 11:24
  • 1
    @onedaywhen: I don't know enough about the subject to correct the wikipedia page; Also, the wikipedia page is not so much wrong as it is misleading - it doesn't explicitly say that ISO/IEC 11179 includes the database naming conventions, it just says that "ISO/IEC 11179 is applicable when naming tables and columns within a relational database". It then goes on to provide an example of naming conventions that might be used for relational database. It lets you think that the example is something taken from the standard, when it's really something made up by the writer of the wikipedia article.
    – mkadunc
    Commented Jul 3, 2012 at 15:21
29

our preference:

  1. Should table names be plural?
    Never. The arguments for it being a collection make sense, but you never know what the table is going to contain (0,1 or many items). Plural rules make the naming unnecessarily complicated. 1 House, 2 houses, mouse vs mice, person vs people, and we haven't even looked at any other languages.

    Update person set property = 'value' acts on each person in the table.
    Select * from person where person.name = 'Greg' returns a collection/rowset of person rows.

  2. Should column names be singular?
    Usually, yes, except where you are breaking normalisation rules.

  3. Should I prefix tables or columns?
    Mostly a platform preference. We prefer to prefix columns with the table name. We don't prefix tables, but we do prefix views (v_) and stored_procedures (sp_ or f_ (function)). That helps people who want to try to upday v_person.age which is actually a calculated field in a view (which can't be UPDATEd anyway).

    It is also a great way to avoid keyword collision (delivery.from breaks, but delivery_from does not).

    It does make the code more verbose, but often aids in readability.

    bob = new person()
    bob.person_name = 'Bob'
    bob.person_dob = '1958-12-21'
    ... is very readable and explicit. This can get out of hand though:

    customer.customer_customer_type_id

    indicates a relationship between customer and the customer_type table, indicates the primary key on the customer_type table (customer_type_id) and if you ever see 'customer_customer_type_id' whilst debugging a query, you know instantly where it is from (customer table).

    or where you have a M-M relationship between customer_type and customer_category (only certain types are available to certain categories)

    customer_category_customer_type_id

    ... is a little (!) on the long side.

  4. Should I use any case in naming items? Yes - lower case :), with underscores. These are very readable and cross platform. Together with 3 above it also makes sense.

    Most of these are preferences though. - As long as you are consistent, it should be predictable for anyone that has to read it.

4
  • 3
    SELECT * FROM people AS person WHERE person.name = 'Greg' sounds the most natural to me.
    – Kenmore
    Commented Dec 28, 2016 at 0:25
  • 1
    @Zuko Mostly, the naming convention for table primary key is <table name><id>, for example PersonID or Person_ID etc. Therefore, it makes more sense that you NOT name your tables in plural as each record is a separate person not people.
    – Mr. Blond
    Commented Apr 2, 2018 at 23:19
  • 1
    "You never know what the table is going to contain (0,1 or many items)", so why singular if you never know? in 99% of times tables will contain more than 1 row, otherwise you may consider redesigning your system. Commented Aug 1, 2020 at 11:03
  • 1
    bob.person_name = 'Bob' bob.person_dob = '1958-12-21' Sorry, but I don't think that's readable code. First off, person_name, underscore in code is far from readable. And certainly in code it should just be bob.name and bob.dob. And about the naming? sorry again all lowercase with underscores seems very old and unreadable to me.
    – SuperDre
    Commented Aug 30, 2021 at 9:06
28

I know this is late to the game, and the question has been answered very well already, but I want to offer my opinion on #3 regarding the prefixing of column names.

All columns should be named with a prefix that is unique to the table they are defined in.

E.g. Given tables "customer" and "address", let's go with prefixes of "cust" and "addr", respectively. "customer" would have "cust_id", "cust_name", etc. in it. "address" would have "addr_id", "addr_cust_id" (FK back to customer), "addr_street", etc. in it.

When I was first presented with this standard, I was dead-set against it; I hated the idea. I couldn't stand the idea of all that extra typing and redundancy. Now I've had enough experience with it that I'd never go back.

The result of doing this is that all of the columns in your database schema are unique. There is one major benefit to this, which trumps all arguments against it (in my opinion, of course):

You can search your entire code base and reliably find every line of code that touches a particular column.

The benefit from #1 is incredibly huge. I can deprecate a column and know exactly what files need to be updated before the column can safely be removed from the schema. I can change the meaning of a column and know exactly what code needs to be refactored. Or I can simply tell if data from a column is even being used in a particular portion of the system. I can't count the number of times this has turned a potentially huge project into a simple one, nor the amount of hours we've saved in development work.

Another, relatively minor benefit to it is that you only have to use table-aliases when you do a self join:

SELECT cust_id, cust_name, addr_street, addr_city, addr_state
    FROM customer
        INNER JOIN address ON addr_cust_id = cust_id
    WHERE cust_name LIKE 'J%';
10
  • 1
    Then you can no longer reliably find every line of code that touches a particular column... Isn't that the point?
    – raveren
    Commented Mar 4, 2013 at 7:52
  • 6
    @Raveren - You still can. If all you do is "SELECT *", then the query is irrelevant for this purpose. When/If later, you use the results of that query, you have to use the column name to do something with its data, so that is the place you need to worry about in your code, not the SQL statement.
    – Granger
    Commented Apr 26, 2013 at 13:04
  • 1
    I wouls be curious as to what situations require SELECT *? I certainly would not want anyone to use that in production code. Yes it is useful for ad hoc queries and for finding out which piece of data is making your multiple join query results be odd, but I can think of no place in production code where it is required.
    – HLGEM
    Commented Apr 29, 2013 at 20:30
  • 2
    Unless you are coding your whole app in a non-OO language, then having a decent ORM layer makes this argument redundant.
    – Adam
    Commented Apr 15, 2015 at 15:15
  • 9
    So due to this answer, I decided to try using table prefixes on a large project and thought I'd report back. It did make refactoring tables extremely easy, which was awesome! However, it was a bigger pain than I anticipated. Our database had lots of complex named tables. It's easy to remember Cust is the prefix for Customer, but not as easy to remember the prefix for HazardVerificationMethod. Every time I wrote a table or field I had to pause to think about the prefix. In the end I decided speed and convenience was more important than searchability, but I did feel it was a valuable experience.
    – dallin
    Commented Mar 12, 2018 at 22:05
26

Take a look at ISO 11179-5: Naming and identification principles You can get it here: http://metadata-standards.org/11179/#11179-5

I blogged about it a while back here: ISO-11179 Naming Conventions

2
  • 21
    Your answer would be more accessible (=better) if you gave a summary here. Great pointer, though!
    – Ola Eldøy
    Commented Apr 27, 2009 at 9:50
  • That link is no longer valid,
    – Pontiac_CZ
    Commented Nov 23 at 17:12
18

My opinions on these are:

1) No, table names should be singular.

While it appears to make sense for the simple selection (select * from Orders) it makes less sense for the OO equivalent (Orders x = new Orders).

A table in a DB is really the set of that entity, it makes more sense once you're using set-logic:

select Orders.*
from Orders inner join Products
    on Orders.Key = Products.Key

That last line, the actual logic of the join, looks confusing with plural table names.

I'm not sure about always using an alias (as Matt suggests) clears that up.

2) They should be singular as they only hold 1 property

3) Never, if the column name is ambiguous (as above where they both have a column called [Key]) the name of the table (or its alias) can distinguish them well enough. You want queries to be quick to type and simple - prefixes add unnecessary complexity.

4) Whatever you want, I'd suggest CapitalCase

I don't think there's one set of absolute guidelines on any of these.

As long as whatever you pick is consistent across the application or DB I don't think it really matters.

8
  • 4
    What the heck is CapitalCase? Commented Jun 23, 2016 at 9:30
  • @ViRuSTriNiTy he probably meant pascal case
    – marctrem
    Commented Feb 28, 2017 at 18:18
  • Keith, on number #3 I do both, and I'm inconsistent (but I digress), but I do not get why it is bad to have a descriptive column name as long as it is not overboard, same with a table, a variable, etc.
    – johnny
    Commented Jan 15, 2018 at 17:45
  • @johnny it's not bad, as such, just not needed. Why type stuff you don't have to? Also most intellisense mainly uses the start of the name, so if you have Product.ProductName, Product.ProductID, Product.ProductPrice etc typing Product.P gives you all the prefixed fields.
    – Keith
    Commented Jan 15, 2018 at 22:25
  • snake_case is the only correct way to name tables or columns. for example sqlite is case insensitive, so now your capitalization is rendered in effective and now you have somereallylongtablename instead of some_really_long_table_name Commented Feb 12 at 16:58
14

In my opinion:

  1. Table names should be plural.
  2. Column names should be singular.
  3. No.
  4. Either CamelCase (my preferred) or underscore_separated for both table names and column names.

However, like it has been mentioned, any convention is better than no convention. No matter how you choose to do it, document it so that future modifications follow the same conventions.

1
  • 3
    regarding #4, PascalCase... camelCase... snake_case...
    – Andrew
    Commented Dec 17, 2019 at 13:36
11

I think the best answer to each of those questions would be given by you and your team. It's far more important to have a naming convention then how exactly the naming convention is.

As there's no right answer to that, you should take some time (but not too much) and choose your own conventions and - here's the important part - stick to it.

Of course it's good to seek some information about standards on that, which is what you're asking, but don't get anxious or worried about the number of different answers you might get: choose the one that seems better for you.

Just in case, here are my answers:

  1. Yes. A table is a group of records, teachers or actors, so... plural.
  2. Yes.
  3. I don't use them.
  4. The database I use more often - Firebird - keeps everything in upper case, so it doesn't matter. Anyway, when I'm programming I write the names in a way that it's easier to read, like releaseYear.
11
  1. Definitely keep table names singular, person not people
    1. Same here
    2. No. I've seen some terrible prefixes, going so far as to state what were dealing with is a table (tbl_) or a user store procedure (usp_). This followed by the database name... Don't do it!
    3. Yes. I tend to PascalCase all my table names
4
  • 30
    OMG. NO. Table names DEFINITELY plural. It's a COLLECTION. It has multiple things in it. "select * from PEOPLE". You're not selecting from a single person, you're selecting from multiple PEOPLE!
    – Triynko
    Commented Apr 1, 2010 at 19:43
  • 4
    I've always liked the way that the select statement sounds better if it is plural. SELECT id,name FROM contacts WHERE email_address LIKE '%gmail%' tables plural, columns singular. Again always a matter of personal opinion.
    – scunliffe
    Commented Jul 1, 2012 at 1:46
  • prefixing tbl, qry etc can be extremely useful when you're handling database metadata, If you're examining the object in a database having a quick, simple naming convention can speed up comprehension dramatically
    – Cruachan
    Commented May 19, 2020 at 14:19
  • @Triynko Until you bump into 'XyzStatus' table. Plural of status is statuses, but that doesn't make sense. But you cannot choose to pluralize one and singularize the other.
    – Trace
    Commented Dec 2, 2021 at 10:11
9

Naming conventions allow the development team to design discovereability and maintainability at the heart of the project.

A good naming convention takes time to evolve but once it’s in place it allows the team to move forward with a common language. A good naming convention grows organically with the project. A good naming convention easily copes with changes during the longest and most important phase of the software lifecycle - service management in production.

Here are my answers:

  1. Yes, table names should be plural when they refer to a set of trades, securities, or counterparties for example.
  2. Yes.
  3. Yes. SQL tables are prefixed with tb_, views are prefixed vw_, stored procedures are prefixed usp_ and triggers are prefixed tg_ followed by the database name.
  4. Column name should be lower case separated by underscore.

Naming is hard but in every organisation there is someone who can name things and in every software team there should be someone who takes responsibility for namings standards and ensures that naming issues like sec_id, sec_value and security_id get resolved early before they get baked into the project.

So what are the basic tenets of a good naming convention and standards: -

  • Use the language of your client and your solution domain
  • Be descriptive
  • Be consistent
  • Disambiguate, reflect and refactor
  • Don’t use abbreviations unless they are clear to everyone
  • Don’t use SQL reserved keywords as column names
2
  • 3
    tables are by definition relations. which are in fact singular. prefixes suck. Have you ever needed to change a table into a view or vice-versa? try that with prefixes. what difference does it make if it is a view or a table? Commented Apr 7, 2017 at 15:57
  • Prefix might help where we have same name for two objects - like function and stored procedure. I would have a function with name as 'GetApproverList' and with same name I would like to create a stored procedure which will call this function internally. Sql will not allow creation of two objects with same name. Commented Jul 6, 2019 at 19:29
9

Here's a link that offers a few choices. I was searching for a simple spec I could follow rather than having to rely on a partially defined one.

http://justinsomnia.org/writings/naming_conventions.html

0
9
SELECT 
   UserID, FirstName, MiddleInitial, LastName
FROM Users
ORDER BY LastName
4
  • 2
    Note the standards used: tables hold multiple things, users have one first name, T-SQL keywords in uppercase, table definitions in Pascal case.
    – Ian Boyd
    Commented Jan 26, 2010 at 0:14
  • 3
    typo: Lastname should be LastName Commented Jun 2, 2017 at 21:42
  • 1
    Table name should be singular i.e: User instead of Users
    – AZ_
    Commented Jan 22, 2020 at 8:44
  • 2
    And note how table names are plural; as they hold Users, not User.
    – Ian Boyd
    Commented Jan 23, 2020 at 0:16
5

Table names should always be singular, because they represent a set of objects. As you say herd to designate a group of sheep, or flock do designate a group of birds. No need for plural. When a table name is composition of two names and naming convention is in plural it becomes hard to know if the plural name should be the first word or second word or both. It’s the logic – Object.instance, not objects.instance. Or TableName.column, not TableNames.column(s). Microsoft SQL is not case sensitive, it’s easier to read table names, if upper case letters are used, to separate table or column names when they are composed of two or more names.

1
  • 2
    A herd is a group of sheep. A User is not a group of users. Commented Jul 14, 2017 at 17:04
4

Table Name: It should be singular, as it is a singular entity representing a real world object and not objects, which is singlular.

Column Name: It should be singular only then it conveys that it will hold an atomic value and will confirm to the normalization theory. If however, there are n number of same type of properties, then they should be suffixed with 1, 2, ..., n, etc.

Prefixing Tables / Columns: It is a huge topic, will discuss later.

Casing: It should be Camel case

My friend, Patrick Karcher, I request you to please not write anything which may be offensive to somebody, as you wrote, "•Further, foreign keys must be named consistently in different tables. It should be legal to beat up someone who does not do this.". I have never done this mistake my friend Patrick, but I am writing generally. What if they together plan to beat you for this? :)

1
  • 3
    So you are saying the table is the entity? Or is the row in the table the entity? To me a table is a collection of rows - hence a collection of entities which implies plural.
    – Jason
    Commented Apr 10, 2012 at 16:29
4

Very late to the party but I still wanted to add my two cents about column prefixes

There seem to be two main arguments for using the table_column (or tableColumn) naming standard for columns, both based on the fact that the column name itself will be unique across your whole database:

1) You do not have to specify table names and/or column aliases in your queries all the time

2) You can easily search your whole code for the column name

I think both arguments are flawed. The solution for both problems without using prefixes is easy. Here's my proposal:

Always use the table name in your SQL. E.g., always use table.column instead of column.

It obviously solves 2) as you can now just search for table.column instead of table_column.

But I can hear you scream, how does it solve 1)? It was exactly about avoiding this. Yes, it was, but the solution was horribly flawed. Why? Well, the prefix solution boils down to:
To avoid having to specify table.column when there's ambiguity, you name all your columns table_column!
But this means you will from now on ALWAYS have to write the column name every time you specify a column. But if you have to do that anyways, what's the benefit over always explicitly writing table.column? Exactly, there is no benefit, it's the exact same number of characters to type.

edit: yes, I am aware that naming the columns with the prefix enforces the correct usage whereas my approach relies on the programmers

4
  • 1
    As you mentioned, you cannot rely on every case having table.column. Programmers will forget in one place and then your global find and replace just broke your whole program. Or you'll make it a rule and someone will think he's fulfilling the rule by using an alias of the table, thus again foiling a global find. In addition, if you want to organize your code by having some sort of database class (which any good programmer will), there will be times when you'll just pass a column name to a db function or just have the column name alone in a variable.
    – dallin
    Commented Apr 29, 2013 at 19:33
  • 2
    @janb: I totally support your answer. I want also to add that using text search to find dependencies is barbarian way to navigate code. Once people get rid of that barbarian search practice - they will start using good naming, which is table.column. So the problem is not naming style, the problem is bad tools made for barbarians.
    – alpav
    Commented Feb 17, 2015 at 2:24
  • Your argument is flawed. The problem with it is it works both ways and doesn't add any advantage. You say, to solve this, just always write table.column, since you are already writing table_column. Well, you can also say just write table_column because you are already writing table.column. In other words, there is no difference between your answer other than it introduces possible errors and doesn't enforce conventions. It's the reason we have a 'private' keyword'. We could trust programmers to always use class variables correctly, but the keyword enforces it and eliminates possible errors.
    – dallin
    Commented Sep 8, 2015 at 23:11
  • For context: I worked on a system that was about 15 years old when I started on it, 20 years old when I left it. It had strongly enforced adherence to coding standards that I disliked. In particular, every table had a three letter prefix associated with it, and all columns used that prefix. When I joined the project, I hated this. It made the columns clunky, and you had to learn the mapping, which wasn't always obvious. By the time I left, I completely agreed with this answer. The value in discoverability cannot be overstated, especially when going from DB flagged queries to the code. Commented Feb 13 at 12:44
3

Essential Database Naming Conventions (and Style) (click here for more detailed description)

table names choose short, unambiguous names, using no more than one or two words distinguish tables easily facilitates the naming of unique field names as well as lookup and linking tables give tables singular names, never plural (update: i still agree with the reasons given for this convention, but most people really like plural table names, so i’ve softened my stance)... follow the link above please

3
2

Table names singular. Let's say you were modelling a realtionship between someone and their address. For example, if you are reading a datamodel would you prefer 'each person may live at 0,1 or many address.' or 'each people may live at 0,1 or many addresses.' I think its easier to pluralise address, rather than have to rephrase people as person. Plus collective nouns are quite often dissimlar to the singular version.

-4

--Example SQL

CREATE TABLE D001_Students
(
    StudentID INTEGER CONSTRAINT nnD001_STID NOT NULL,
    ChristianName NVARCHAR(255) CONSTRAINT nnD001_CHNA NOT NULL,
    Surname NVARCHAR(255) CONSTRAINT nnD001_SURN NOT NULL,
    CONSTRAINT pkD001 PRIMARY KEY(StudentID)
);

CREATE INDEX idxD001_STID on D001_Students;

CREATE TABLE D002_Classes
(
    ClassID INTEGER CONSTRAINT nnD002_CLID NOT NULL,
    StudentID INTEGER CONSTRAINT nnD002_STID NOT NULL,
    ClassName NVARCHAR(255) CONSTRAINT nnD002_CLNA NOT NULL,
    CONSTRAINT pkD001 PRIMARY KEY(ClassID, StudentID),
    CONSTRAINT fkD001_STID FOREIGN KEY(StudentID) 
        REFERENCES D001_Students(StudentID)
);

CREATE INDEX idxD002_CLID on D002_Classes;

CREATE VIEW V001_StudentClasses
(
    SELECT
        D001.ChristianName,
        D001.Surname,
        D002.ClassName
    FROM
        D001_Students D001
            INNER JOIN
        D002_Classes D002
            ON
        D001.StudentID = D002.StudentID
);

These are the conventions I was taught, but you should adapt to whatever you developement hose uses.

  1. Plural. It is a collection of entities.
  2. Yes. The attribute is a representation of singular property of an entity.
  3. Yes, prefix table name allows easily trackable naming of all constraints indexes and table aliases.
  4. Pascal Case for table and column names, prefix + ALL caps for indexes and constraints.
3
  • 8
    ChristianName ... that's an odd convention. Commented Feb 25, 2009 at 0:31
  • 3
    Serial numbers on your tables? Does anyone seriously think this makes sense works for the developers?
    – ErikE
    Commented Jun 20, 2011 at 20:05
  • Since this example brought it up... I'm personally against uppercasing acronyms in table or column names, as I think it makes it trickier to read. So in this case, I would say StudentId is preferable to StudentID. Not a big deal when the acronym is at the end, but I've seen countless examples in my job where acronyms were in the Front or middle of the name, and it made it more difficult to parse in your mind. Ex: StudentABCSSN vs StudentAbcSsn. Commented Jan 5, 2019 at 14:01

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