Doug Smith
Doug Smith

Reputation: 29316

How does this MySQL statement work?

CREATE TABLE `myDB`.`usersystem` (
`userid` INT NOT NULL AUTO_INCREMENT , 
`username` VARCHAR( 50 ) NOT NULL , 
`password` VARCHAR( 32 ) NOT NULL , 
`email` VARCHAR( 50 ) NOT NULL , 
PRIMARY KEY ( `userid` )
);

Sorry, I would search but I have no idea how to search for such a specific misunderstanding.

I'm starting to learn MySQL, and I'm getting comfortable, but I'm confused what the backticks do, and why when the table was created it has a period between them. What does usersystem denote?

Upvotes: 0

Views: 55

Answers (3)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

All of the stuff you don't understand could be removed without changing the meaning of this statement!

The backticks are just for quoting; they're optional, but they distinguish between a keyword and the name of a table or column. If you wanted a table named SELECT, putting the table name in quotes like this would let you create one.

Backticks are used for quoting identifiers by default, but there's a special ANSI_QUOTES mode that, if activated, lets you use double-quote characters for this instead. Single quotes are used for quoting character strings.

myDB.usersystem refers to a table named usersystem in the database named myDB. Naming the database is optional, as it defaults to the connected one. You can also set the implicit database by using the use command, as in "use myDB".

Upvotes: 1

Travis McHattie
Travis McHattie

Reputation: 67

usersystem is the table name, myDB is the database name.

<database name>.<table name> is the fully qualified way to reference a table.

The back ticks are used to denote the name of the resource. They are not always necessary but it is a good practice to use them.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881153

Backkticks prevent names from being interpreted as SQL keywords so you can create table names like create or date.

The dot separates the schema name from the table name.

Schemas allow you to have identically named tables for different purposes, such as a users table in N different applications.

Upvotes: 1

Related Questions