Installing MySQL and Your First Queries
Installing MySQL and Your First Queries
Getting MySQL Running
MySQL is one of the most popular open-source relational databases. To begin, download MySQL Community Server from the official site and run the installer. Choose the Developer Default preset and set a password for the root user. On Windows the installer can also set up MySQL as a service, so it starts automatically.
You interact with MySQL through a client. The MySQL Command Line Client is included, and MySQL Workbench provides a visual editor that shows schemas and query results. For scripts and automation, use the mysql command-line program.
Connecting and Exploring
Connect by supplying a username and password:
mysql -u root -p
Once connected you see a mysql> prompt. The SHOW statement lists databases and tables, and SELECT of a constant confirms the server is alive.
SHOW DATABASES;
SELECT VERSION();
Every SQL statement ends with a semicolon. A few navigation commands keep you oriented:
USE mysql;
SHOW TABLES;
EXIT;
USE selects the active database so later statements run against it. SHOW TABLES lists its tables, and EXIT leaves the client.
Statement Types
SQL splits into several families. Data Query Language (SELECT) reads data. Data Manipulation Language (INSERT, UPDATE, DELETE) changes data. Data Definition Language (CREATE, ALTER, DROP) manages the schema, the structure of tables and indexes. Throughout this tutorial you will learn one family at a time.
A Mental Model
Think of a query as a question about the data. You always start with what you want (the columns), then name the source (the table), then describe the conditions. Getting that sentence pattern into your head makes every later topic easier.
Key Points
- Install MySQL Community Server and set a root password.
- Connect with mysql -u root -p and explore with SHOW DATABASES.
- Every statement ends with a semicolon.
- USE picks the active database; EXIT quits the client.
- SQL has query, data-change, and schema-change statement families.