0% found this document useful (0 votes)
0 views

SQL BASICS

The document provides SQL commands for creating and managing an 'employees' table, including creating the table, inserting, updating, deleting records, and renaming columns and tables. It also includes examples of adding columns, truncating tables, and creating foreign key relationships with a 'departments' table. Key SQL syntax and commands are outlined for each operation, demonstrating how to manipulate database records effectively.

Uploaded by

manjumestri68
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

SQL BASICS

The document provides SQL commands for creating and managing an 'employees' table, including creating the table, inserting, updating, deleting records, and renaming columns and tables. It also includes examples of adding columns, truncating tables, and creating foreign key relationships with a 'departments' table. Key SQL syntax and commands are outlined for each operation, demonstrating how to manipulate database records effectively.

Uploaded by

manjumestri68
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

CREATE Table
This query creates a table named employees with columns for id, name, and salary.

CREATE TABLE employees (

id INT PRIMARY KEY,

name VARCHAR(100),

salary DECIMAL(10, 2)

);

DESC employees;

2. INSERT Data
This query inserts a new record into the employee

INSERT INTO employees (id, name, salary)

VALUES (1, 'John Doe', 50000.00);

INSERT INTO employees (id, name, salary)

VALUES (2, 'Abraham’, 60000.00);

select * from employees;


This query retrieves all columns and all records from the employees table.
 SELECT * means "select all columns."
 FROM employees indicates the table from which to retrieve the data.

3. UPDATE Data
This query updates an existing record in the employees table (updating the salary for the employee
with id = 1).

UPDATE employees

SET salary = 55000.00

WHERE id = 1;

select * from employees; check the updated database

4. DELETE Data (Optional - if you want to delete a record)


This query deletes a record from the employees table where id = 1.
5. Renaming a column name
ALTER TABLE employees
RENAME COLUMN name TO full_name;

6. Example for Adding a Column in SQL Server


ALTER TABLE employees
ADD email VARCHAR(100);

7. Rename the Table


If you want to rename the employees table to something else, like staff:
ALTER TABLE employees
RENAME TO staff;
DESC staff;

Syntax for TRUNCATE:


TRUNCATE TABLE table_name;
 table_name: The name of the table from which you want to remove all rows.
Example 1: Truncating a Table
If you want to remove all records from the employees table:
TRUNCATE TABLE employees;
desc employees;
select * from employees;

Syntax for Creating a Foreign Key


ALTER TABLE child_table
ADD CONSTRAINT fk_constraint_name
FOREIGN KEY (child_column)
REFERENCES parent_table (parent_column);
 child_table: The table containing the foreign key.
 fk_constraint_name: A name for the foreign key constraint (optional).
 child_column: The column in the child table that holds the foreign key.
 parent_table: The referenced table (the parent table).
 parent_column: The column in the parent table that the foreign key refers to (usually
the primary key).
Example 1: Creating a Foreign Key
Let's say you have two tables:
1. employees: Contains information about employees.
2. departments: Contains information about departments.
You want to create a relationship where each employee belongs to a department.
1. departments table:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
2. employees table, where each employee has a department_id (foreign key):
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2),
department_id INT,
CONSTRAINT fk_department
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
);
Here:
 department_id in the employees table is a foreign key that references the
department_id in the departments table.
 The CONSTRAINT fk_department gives a name to the foreign key constraint
(optional but good practice for naming constraints).

You might also like