How is a database created in MySQL?
Working with databases involves creating an initial structure to store information. In this aspect, the CREATE
command is essential in SQL, allowing you to create new databases and other elements. Here I explain how to use it properly:
- Create a database: Use
CREATE DATABASE database_name;
.
- Add duplicate protection: To avoid errors if the database already exists, you can use
CREATE DATABASE IF NOT EXISTS database_name;
.
For example:
CREATE DATABASE IF NOT EXISTS platzi_operation;
By using this statement, you prevent the system from trying to recreate an existing database, generating only a warning instead of an error.
How do we select and manage databases?
Once the database has been created, it is essential to select which one you are going to use to perform your operations. This is where commands like SHOW
and USE
come into play:
- List existing databases: Use
SHOW DATABASES;
to see all the databases available in the system.
- Select a database: Employ
USE database_name;
to choose on which database your queries would be executed.
For example:
USE platzi_operation;
With this command, you tell the system that you want to work with platzi_operation
, which directs any subsequent action to that specific database.
How do you create a table in SQL and define its columns?
Once you have your database ready, it is time to think about creating tables, the fundamental storage for your data. Here are several key elements for creating tables in SQL:
- Table creation basics: Use
CREATE TABLE
followed by the table name.
- Plural and English table names: It is a good practice to make future work easier by maintaining consistency and making it easier to understand.
For example, to create a table called "books":
CREATE TABLE IF NOT EXISTS books ( book_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY );
What data types and constraints can we define?
When defining the columns of a table, you must consider the data types and constraints to be applied for proper data management:
- Numeric and auto-incremental fields: use
INT
, TINYINT
, BIGINT
with options such as UNSIGNED
and AUTO_INCREMENT
for unique IDs.
- Uniqueness constraints: A
PRIMARY KEY
ensures that each entry is unique and identifiable.
- Text options:
VARCHAR
is used for text strings with delimited size, while TEXT
allows large amounts of text.
Example of column definition with various characteristics:
CREATE TABLE IF NOT EXISTS books ( book_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100) NOT NULL, year INT UNSIGNED NOT NULL DEFAULT 1900 );
How to optimize space with data types?
Choosing the right data type is crucial to optimize space and query performance:
- Unsigned integers (
UNSIGNED
): save space by avoiding the storage of the sign, useful for numbers that will never be negative.
-
VARCHAR
length: Specifies only the space needed. For example, use VARCHAR(2)
for standardized language codes.
Here is an approach on how to include a language with a specific standard and comments:
lang VARCHAR(2) NOT NULL DEFAULT 'EN' COMMENT 'ISO 639-1 Language Code'.
The above example also demonstrates how to add useful explanatory annotations within your database structure.
This journey through the creation of databases and tables in SQL represents only the beginning of how you can structure information efficiently. As you continue, you will discover the importance of correctly defining each parameter and best practices that will increase the performance of your databases and make them easier to maintain.
Want to see more contributions, questions and answers from the community?