DEV Community

Mueni
Mueni

Posted on

Ch 6: Working With Tables and Other SQL Need-To-Knows

Creating SQL tables

When creating tables, you need to define the columns and their data types.

CREATE TABLE table_name(
  column1 datatype,
  column2 datatype,
  column3 datatype,
)
Enter fullscreen mode Exit fullscreen mode

We are going to call our table customer_data, and it will have the columns customer_id, customer_name, and dob.

CREATE TABLE customer_data(
  customer_id INT PRIMARY KEY,
  customer_name VARCHAR(50),
  dob datatype DATETIME
);
Enter fullscreen mode Exit fullscreen mode

Inserting data into the table

This is the syntax for inserting multiple rows into a table:

INSERT INTO table_name (column1, column2, column3)
VALUES
(value1,value2,value3),
(value1,value2,value3);
Enter fullscreen mode Exit fullscreen mode

For our customer data:

INSERT INTO table_name (customer_id, customer_name, dob)
VALUES
(1, 'Jacky','25-06-1980'),
(2,'Mike','02-10-2003');
Enter fullscreen mode Exit fullscreen mode

Viewing data in a table

You use the command SELECT to view data in a table. To see all data, you use an asterisk*

'SELECT * FROM table_name`

If you want to view data from a specific column, you replace the asterisk with the column name:
SELECT column_name FROM table_name;

SQL need-to-knows

  1. NULL in SQL does not mean zero. It means unknown or missing value.
  2. When using SQL tools like DBeaver, user CTRL+ENTER to run the current query (where the cursor is).
  3. Always terminate SQL queries with a semicolon (;). This signals the end of a statement.
  4. When inserting strings and varchar values in a table, put them in single quotes.
  5. SQL commands are often written in capital letters in tutorials. This is not required, but it improves readability and helps distinguish commands from table or column names.

In the next posts, I will be exploring various SQL commands in no particular order.

Top comments (0)