Unit4-5 Server Tier database
Unit4-5 Server Tier database
Types:
Each relational database management system (RDBMS), like MySQL, PostgreSQL, SQL
Server, Oracle, etc., may have specific data types and syntax variations, but the core
concepts of SQL remain consistent.
4/4/2024 Prepared By : Basanta Chapagain 121
Create table using SQL
• To create a table using SQL in MySQL, you would use the
`CREATE TABLE` statement.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
Replace `table_name` with the name you want to give to your table. Then, for each column you
want to add, provide the column name and its corresponding data type.
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
hire_date DATE
In this example:
); - `employee_id` is an integer column and is designated as the primary key.
- `first_name` and `last_name` are variable-length character string columns.
- `birth_date` and `hire_date` are date columns.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
• Replace `table_name` with the name of your table, and for each column, provide the
corresponding values you want to insert.
• This example inserts a new employee record with the provided values into the
`employees` table. You can continue using similar `INSERT INTO` statements to add
more records as needed.
4/4/2024 Prepared By : Basanta Chapagain 124
Select Record- SQL syntax
• To retrieve data from the `employees` table using a `SELECT`
query in MySQL, you can use the following syntax:
SELECT column1, column2, column3, ...
FROM table_name
WHERE condition;
• Replace `column1, column2, column3, ...` with the names of the
columns you want to retrieve, `table_name` with the name of
your table, and `condition` with any optional filtering conditions
you want to apply.
Above query will return all rows and all columns from the `employees` table.
• If you want to retrieve specific columns or apply a condition, you can modify the
query accordingly. For example, to retrieve only the first and last names of employees:
• These are just a few examples of `SELECT` queries. You can customize your queries to
retrieve the specific data you need from your table.
• This query will update the `last_name` column of the employee with an
`employee_id` of 1 to 'Smith'.
• This query will update the `first_name` and `last_name` columns of the
employee with an `employee_id` of 2 to 'Jane' and 'Johnson', respectively.
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT,
grade CHAR(1)
);
4/4/2024 Prepared By : Basanta Chapagain 131
Insert Record Example for Students
Single:
INSERT INTO students (student_id, first_name, last_name, age, grade) VALUES (1,
'John', 'Doe', 18, 'A'),
Multiple:
VALUES
This query updates the grade of the student with `student_id` 2 to 'A'.
Delete Record:
This query deletes the student with `student_id` 1 from the table
This query retrieves only the `first_name` and `last_name` columns from the `students`
table.
This query retrieves all students and orders the result by last name and then first
name.
This query calculates the total number of students and the average age of all
students.