MySQL: Naming Conventions | CSE 154 Unofficial Code Quality Guide (2024)

In this course, we expect you to use the MySQL version of SQL. There are different naming conventions in SQL, but for consistency we ask you to follow our course-specific coding guidelines.

As with any other language, names should be descriptive and distinguishable. In SQL, this particularly applies to database, table, and attribute names (alias conventions are an exception to this rule, as discussed later in the Alias Usage and Naming Conventions section).

Letter-casing Conventions

SQL commands, datatypes, and constraints (e.g. DEFAULT, PRIMARY KEY, AUTO INCREMENT, etc.) should be in ALLCAPS. It is legal to write commands in lowercase, but this often leads to subtle bugs with lowercase attribute/column names, so we expect you to use the ALLCAPS convention for any commands or datatypes.

Database names (e.g. c9 or imdb) should be lowercased (no numbers or special characters lie "_" or "-").

SQL Table Letter-casing Conventions

For table names, you may use PascalCasing (camelCasing with the first letter also capitalized) or lowercasing conventions, as long as you are consistent. In either case, table names should only contain alphabetic characters (no "_" or "-").

When creating a new table, you shoud use lowercase_underscore conventions for multi-word column names (no capitalization).

create table dogBreeds( breedID int, breedName varchar(20), PRIMARY KEY(breedID)); select breedNamefrom dogBreedswhere breedName like 'great%';
CREATE TABLE DogBreeds( breed_id INT, breed_name VARCHAR(20), PRIMARY KEY (breed_id)); SELECT breed_nameFROM DogBreedsWHERE breed_name LIKE 'great%';

Alias Usage and Naming Conventions

When writing multi-table queries, it is often helpful to use a short-hand name to avoid conflicts between two instances of the same table. As a declarative language, SQL statements tend to be relatively concise and easy to interpret with English-like command names. In this case, short alias names tend not to introduce much confusion, as long as they follow a consistent convention.

You will often find aliasing helpful for queries that reference multiple instances of the same table or when you are working with more than one instance of tables that share a column name (there may be a case where two different tables share an identical column name). Aliasing may also be used to improve readability even when it is not needed (e.g. there are no column name conflicts but you are referencing a long table name in many places) but most short queries do not include aliasing.

When naming your table aliases, you may use single-letter names using the first letter of the table name, and may distinguish two aliases for the same table name using a numerical suffix. If two different tables share the same first letter, you may break ties with the second letter, or you may use a substring of the name that is reasonably distinguishable.

The "bad example" below does not clearly distinguish between the countries and cities tables when it names all with the c# convention (# being the number appended to each). The first "good example" is an accepted alternative since it is more clear that c and ci are different tables (note that it is a little easier to follow what the "good example" is selecting). Alternatively, you could omit the alias for the countries table since it is only used once, but because both countries and cities share a column called "name" (e.g. "Italy" is a name of a country and "Milan" is a name of a city) you need to specify which table you are referencing in the first line for it to be valid SQL. The second "good example" demonstrates a good use of this convention.

SELECT DISTINCT c1.nameFROM countries c1, cities c2, cities c3WHERE c3.country_code = c1.code AND c3.country_code = c1.code AND c2.population >= 5000000 AND c3.population >= 5000000 AND c2.id < c3.id;
-- example 1, with alias for single countries tableSELECT DISTINCT c.nameFROM countries c, cities ci1, cities ci2WHERE ci1.country_code = c.code AND ci2.country_code = c.code AND ci1.population >= 5000000 AND ci2.population >= 5000000 AND ci1.id < ci2.id;-- example 2, without alias for single countries tableSELECT DISTINCT countries.nameFROM countries, cities ci1, cities ci2WHERE ci1.country_code = countries.code AND ci2.country_code = countries.code AND ci1.population >= 5000000 AND ci2.population >= 5000000 AND ci1.id < ci2.id;
MySQL: Naming Conventions | CSE 154 Unofficial Code Quality Guide (2024)

FAQs

What is the best naming convention for MySQL? ›

Naming Conventions for MySQL
  • Embedded quotation marks are not permitted.
  • Case sensitivity is specified when a server is installed. ...
  • A name cannot be a reserved word in MySQL unless you enclose the name in quotation marks. ...
  • Database names must be unique.
Jun 17, 2024

What is the naming convention of MySQL variable? ›

General Rules — Naming Conventions

Using lowercase will help speed typing, avoid mistakes as MYSQL is case sensitive. Space replaced with Underscore — Using space between words is not advised. Numbers are not for names — While naming, it is essential that it contains only Alpha English alphabets.

What are the two rules for naming a table in MySQL? ›

Table names are lower case, use underscores to separate words, and are singular (e.g. foo , foo_bar , etc.

What are the SQL naming convention standards? ›

No special characters or spaces should be used. In addition to the general naming standards regarding no special characters, no spaces, and limited use of abbreviations and acronyms, common sense should prevail in naming variables; variable names should be meaningful and natural.

What is the best naming convention for programming? ›

According to PSR-1, class names should be in PascalCase, class constants should be in MACRO_CASE, and function and method names should be in camelCase.

What is the primary key naming convention in SQL? ›

A primary key is a field or a set of fields in the database table that uniquely identifies records in the database table. A table can have only one primary key. The naming conventions for a primary key constraint should have a "PK_" prefix, followed by the table name. The syntax should be "PK_<TableName>".

What are the constraint naming conventions in MySQL? ›

Constraints naming conventions
TypeSyntax
Primary Keypk_<table name>
Foreign Keyfk_<table name>_<column name>[_and_<column name>]*_<foreign table name>
Indexindex_<table name>_on_<column name>[_and_<column name>]*[_and_<column name in partial clause>]*
Unique Constraintunique_<table name>_<column name>[_and_<column name>]*
2 more rows

Which is a correct naming convention for a variable? ›

The rules and conventions for naming your variables can be summarized as follows: Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign " $ ", or the underscore character " _ ".

What is the naming convention for database names? ›

Database Naming Convention
  • Warning! ...
  • Avoid quotes. ...
  • Ex: Avoid using names like "FirstName" or "All Employees" .
  • Lowercase. ...
  • Ex: Use first_name , not "First_Name" .
  • Data types are not names. ...
  • Underscores separate words. ...
  • Ex: Use word_count or team_member_id , not wordcount or wordCount .

What are invalid table names in MySQL? ›

The MySQL docs state that: Database and table names cannot contain “/”, “\”, “.”, or characters that are not allowed in file names. I suspect you violate that on two counts, the . in your domain name and (probably) the @ seperator.

What characters are not allowed in MySQL? ›

ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers. Identifiers may begin with a digit but unless quoted may not consist solely of digits. Database, table, and column names cannot end with space characters.

What are the valid characters for table names in MySQL? ›

Database, Table, Index, Column, and Alias Names
IdentifierMaximum Length (bytes)Allowed Characters
Database64Any character that is allowed in a directory name, except '/', '\', or '.'
Table64Any character that is allowed in a filename, except '/', '\', or '.'
Column64All characters
Index64All characters
1 more row

What is an example of a naming convention? ›

Examples of naming conventions may include: Children's names may be alphabetical by birth order. In some Asian cultures, siblings commonly share a middle name. In many cultures the son is usually named after the father or grandfather.

What is the variable naming convention in SQL? ›

Naming Conventions for PL/SQL

Using case sensitive variable names force developers to use double quotes for each reference to the variable. Our recommendation is to write all names in lowercase and to avoid double quoted identifiers. A widely used convention is to follow a {prefix}variablecontent{suffix} pattern.

What are the rules for naming a table? ›

  • 1 Use singular nouns for tables. ...
  • 2 Use descriptive and meaningful names for columns. ...
  • 3 Use prefixes or suffixes for constraints. ...
  • 4 Use consistent naming conventions and case styles. ...
  • 5 Use abbreviations and acronyms sparingly and consistently. ...
  • 6 Here's what else to consider.
Mar 16, 2023

What should MySQL hostname be? ›

The MySQL hostname will always be 'localhost' in your configuration files.

Which file naming convention is best? ›

  • Keep file names short and relevant.
  • If using a date, use the format Year-Month-Day (four digit year, two digit month, two digit day): YYYY-MM-DD or YYYY-MM or YYYY-YYYY.
  • Include a leading zero for numbers 0-9.
  • Order the elements in a file name according to the way the file will be retrieved.

Should MySQL table naming convention be plural or singular? ›

The correct answer is that there is no right or wrong when naming database tables — just be consistent from the start. The only wrong answer with database table names is to use a combination of plural and singular. It's easier for your team to remember one or the other once and stick to it.

What is the naming convention for constraints in MySQL? ›

This will help us identify the field as a foreign key column and also point us to the referenced table. The naming convention for a foreign key constraint is to have an "fk_" prefix, followed by the target table name, followed by the source table name. Hence, the syntax should be "fk_<target_table>_<source_table>".

Top Articles
India Travel Insurance | Allianz Assistance UK
CEOs Vastly Overpaid, Most Americans Say
O'reilly's Auto Parts Closest To My Location
Fat Hog Prices Today
Tabc On The Fly Final Exam Answers
St Petersburg Craigslist Pets
Grange Display Calculator
Aces Fmc Charting
سریال رویای شیرین جوانی قسمت 338
Calamity Hallowed Ore
Xrarse
Skip The Games Norfolk Virginia
Music Archives | Hotel Grand Bach - Hotel GrandBach
About Goodwill – Goodwill NY/NJ
Blog:Vyond-styled rants -- List of nicknames (blog edition) (TouhouWonder version)
Foodland Weekly Ad Waxahachie Tx
Craigslist Maui Garage Sale
bode - Bode frequency response of dynamic system
Www Craigslist Com Bakersfield
Daytonaskipthegames
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
The Many Faces of the Craigslist Killer
Naval Academy Baseball Roster
Drift Hunters - Play Unblocked Game Online
Malluvilla In Malayalam Movies Download
Osrs Important Letter
Salemhex ticket show3
Chicago Pd Rotten Tomatoes
Sports Clips Flowood Ms
Att U Verse Outage Map
Capital Hall 6 Base Layout
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
John F Slater Funeral Home Brentwood
Domino's Delivery Pizza
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
All Characters in Omega Strikers
Weather In Allentown-Bethlehem-Easton Metropolitan Area 10 Days
Pgecom
22 Golden Rules for Fitness Beginners – Barnes Corner Fitness
Eat Like A King Who's On A Budget Copypasta
Tom Kha Gai Soup Near Me
26 Best & Fun Things to Do in Saginaw (MI)
Canvas Elms Umd
Actress Zazie Crossword Clue
Online College Scholarships | Strayer University
Washington Craigslist Housing
Craigslist Pet Phoenix
Craigslist Cars For Sale By Owner Memphis Tn
Craigslist Psl
Inloggen bij AH Sam - E-Overheid
Gainswave Review Forum
Bunbrat
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5925

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.