Support Online
Skip to main content

Guide to Creating MySQL Tables and Adding Data

Guide to Creating MySQL Tables and Adding Data

MySQL is a powerful relational database management system widely used for modern web applications and e-commerce platforms. In this guide, you will learn step by step how to create a database and table using MySQL and how to add and update your data.

What Will You Learn in This Guide?

  • Creating a database and establishing a table structure
  • Using Primary Key and Auto Increment
  • Data addition and data update operations
  • Data validation and error management

1. Step: Creating and Selecting a Database

  • Database is the area where you will store your data. In this step, we will create and activate the database.
-- Yeni bir veritabanı oluşturur
CREATE DATABASE genixnode_db;

-- Oluşturulan veritabanını aktif hale getirir
USE genixnode_db;
  • With these commands, we will create a database called genixnode_db and perform the next operations in this database.

2. Step: Creating a Table (CREATE TABLE)

  • We need to create a table to store data in the database. When creating the table, determining the data type of each column is a critical step.

CREATE TABLE kullanicilar (
id INT PRIMARY KEY AUTO_INCREMENT, -- Benzersiz kimlik
isim VARCHAR(100) NOT NULL, -- Kullanıcı adı
eposta VARCHAR(255) UNIQUE, -- Tekil e-posta adresi
kayit_tarihi DATE -- Kayıt günü
);

AUTO_INCREMENT: Every time you add a new record, the id column automatically increases by one.

PRIMARY KEY: Ensures that the id column is unique in each record.

UNIQUE: prevents the values ​​in the email column from repeating.


3. Step: Insert Data (INSERT INTO)

  • Our table is ready, now we can add data. Let's learn how to add single and multiple data.

3.1 Adding Single Data:


INSERT INTO kullanicilar (isim, eposta, kayit_tarihi)
VALUES ('Ahmet Yılmaz', 'ahmet@ornek.com', '2025-01-10');
  • This command saves a new user in the users table.

3.2 Adding Multiple Data:


INSERT INTO kullanicilar (isim, eposta, kayit_tarihi)
VALUES
('Mehmet Öz', 'mehmet@ornek.com', '2025-01-11'),
('Ayşe Gül', 'ayse@ornek.com', '2025-01-12');
  • This command adds multiple users with a single query and improves performance by reducing server load.

4. Step: Verify and Update Data

  • We can query the data using the SELECT command to make sure the data is added correctly. We can also update existing data with the UPDATE command.

4.1 Listing Data:


SELECT * FROM kullanicilar;

4.2 Updating Data:


UPDATE kullanicilar
SET eposta = 'ahmet.yeni@ornek.com'
WHERE id = 1;
  • This command updates the email address of the user whose id is 1.

Frequently Asked Questions (FAQ)

1. I get the "Table already exists" error when creating the table, what should I do?

By using the CREATE TABLE IF NOT EXISTS table_name command, you can prevent the attempt to recreate the table if it exists.

2. What is the difference between CHAR and VARCHAR?

CHAR is fixed length (adds space), while VARCHAR only takes up the size of the data; so generally VARCHAR is more efficient.

3. What can I do to prevent incorrect data entry?

You can ensure data integrity by adding NOT NULL (cannot be empty) and UNIQUE (unique) constraints to your columns.

Result

Get a solid start on your backend development journey by learning how to create databases and tables in MySQL, and how to insert and update data.

You can immediately try professional solutions on the GenixNode platform to carry out your database operations on a safe and fast infrastructure.