Nearby lessons

28 of 37

JDBC - Create Tables

📌 What You Will Learn
  • Create tables with the CREATE TABLE statement
  • Pick column names and data types
  • Understand the PRIMARY KEY

Learn the CREATE TABLE statement — column names, MySQL data types, and the PRIMARY KEY that makes each row unique.

The CREATE TABLE Statement

A table is made of columns (the fields) and will later hold rows (the records). Each column needs a name and a data type:

Example01
JCode Cell
1CREATE TABLE employee (
2 id INT PRIMARY KEY,
3 name VARCHAR(50),
4 salary DOUBLE,
5 hiredate DATE
6);
Output
Query OK, 0 rows affected (0.02 sec)

Common MySQL Data Types

Data typeWhat it storesExample
INTWhole numbersid, age, quantity
VARCHAR(n)Text up to n charactersname, city, email
DOUBLEDecimal numberssalary, price
DATEA calendar datehiredate, dob
BOOLEANtrue / falseactive, verified

The PRIMARY KEY

The PRIMARY KEY column is special — every value in it must be unique and never empty. It is how you identify one specific row:

  • No two rows can share the same primary-key value.
  • It is usually an INT column named id.
  • It is the column you use in WHERE id = ... to update or delete one row.

Check the Table Structure

Describe shows the columns and types of a table:

Example04
JCode Cell
1DESCRIBE employee;
Output
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int         | NO   | PRI | NULL    |       |
| name     | varchar(50) | YES  |     | NULL    |       |
| salary   | double      | YES  |     | NULL    |       |
| hiredate | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
📝 Key Takeaways
  • CREATE TABLE defines the columns of a table
  • Every column has a name and a data type
  • The PRIMARY KEY column uniquely identifies each row