SQL Flashcards
(42 cards)
Insert query
INSERT INTO employees (id, name, age, department, salary) VALUES (1, ‘John Doe’, 30, ‘IT’, 50000);
UPDATE
UPDATE employees
SET salary = 55000
WHERE id = 1;
DROP
DROP TABLE employees;
CREATE query
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
department VARCHAR(50),
salary DECIMAL(10,2)
);
Join syntax
SELECT Customers.customer_id, Customers.first_name, Orders.item
FROM Customers
JOIN Orders
ON Customers.customer_id = Orders.customer_id
char vs varchar
char 255, varchar 65535
binary
stream of data in binary form
SQL vs MYSQL
SQL from microsoft, MYSQL from oracle
VARCHAR vs TEXT
name VARCHAR(255), – Stores up to 255 characters inline
description TEXT stores here and there,
Varchar best for indexing , and faster in search
for big text like description, comment text is best because of less search
decimal datatype with digits
DECIMAL(10,2)
date datatypes
Data Type Description Example
DATE Stores only date (YYYY-MM-DD) ‘2025-03-07’
DATETIME Stores date & time (YYYY-MM-DD HH:MM:SS) ‘2025-03-07 14:30:00’
TIMESTAMP Similar to DATETIME, but auto-updates with time zone support Used for recording changes
TIME Stores only time (HH:MM:SS) ‘14:30:00’
YEAR Stores a year (YYYY) ‘2025’
Cross join possible combination
m*n
Primary key syntax
CREATE TABLE Students (
student_id INT PRIMARY KEY, – Unique identifier for each student
name VARCHAR(50),
age INT,
class VARCHAR(10)
);
Foreign key syntax
CREATE TABLE Marks (
mark_id INT PRIMARY KEY, – Unique ID for each mark entry
student_id INT, – Foreign Key referring to Students table
subject VARCHAR(50),
marks INT,
FOREIGN KEY (student_id) REFERENCES Students(student_id) – Linking to Students table
);
Composite key syntax
CREATE TABLE Enrollments (
student_id INT,
course_id INT,
enrollment_date DATE,
PRIMARY KEY (student_id, course_id) – Composite Key (student_id + course_id)
);
Candidate Key and its syntax
CREATE TABLE Students (
student_id INT UNIQUE, – Candidate Key
email VARCHAR(100) UNIQUE, – Candidate Key
name VARCHAR(50),
PRIMARY KEY (student_id) – Only one is chosen as the Primary Key
);
inno db engine
If primary key not declared mysql create its own column and asign as primary key by itself, Reason: Having primary key or unique key is mandatory in DBMS, But not compulsory to declare
What field type foreign key can refer
Primary key, Unique ( Candidate key )
super key as foreign key
CREATE TABLE Employees (
emp_id INT NOT NULL,
email VARCHAR(100) UNIQUE,
name VARCHAR(50),
UNIQUE (emp_id, email) – Super Key without Primary Key
);
CREATE TABLE Salaries (
salary_id INT PRIMARY KEY,
emp_ref_id INT,
emp_email VARCHAR(100),
FOREIGN KEY (emp_ref_id, emp_email) REFERENCES Employees(emp_id, email)
);
CHECK
CREATE TABLE students (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT CHECK (age >= 18),
email VARCHAR(100) UNIQUE
);
AUTO_INCREMENT
We can insert manually but next row will start from manually entered value
DDL Data Definition Language
CREATE Creates a new table/database
ALTER Modifies an existing table
DROP Deletes a table/database
TRUNCATE
DML Data Manipulation Language
INSERT Adds new records
UPDATE Modifies existing records
DELETE
DQL Data Query Language
SELCT