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,
)
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
);
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);
For our customer data:
INSERT INTO table_name (customer_id, customer_name, dob)
VALUES
(1, 'Jacky','25-06-1980'),
(2,'Mike','02-10-2003');
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
- NULL in SQL does not mean zero. It means unknown or missing value.
- When using SQL tools like DBeaver, user CTRL+ENTER to run the current query (where the cursor is).
- Always terminate SQL queries with a semicolon (;). This signals the end of a statement.
- When inserting strings and varchar values in a table, put them in single quotes.
- 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)