Docsity

BASIC COMMAND OF SQL, Lecture notes of Computer Science

Partial preview of the text

Download BASIC COMMAND OF SQL and more Lecture notes Computer Science in PDF only on Docsity!

Basic Commands

Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed COMMENT - add comments to the data dictionary RENAME - rename an object

Table Employee

 DCL

Data Control Language (DCL) statements. Some examples: GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT command

ALTER

ALTER TABLE table_name ADD column_name column-definition; ALTER TABLE table_name MODIFY column_name column_type; ALTER TABLE table_name DROP COLUMN column_name;

ALTER

ALTER TABLE table_name RENAME COLUMN old_name to new_name; ALTER TABLE table_name RENAME TO new_table_name; ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);

Foreign Keys

CREATE TABLE table_name ( column datatype null/not null, column2 datatype null/ not null, ... CONSTRAINT fk_column FOREIGN KEY (column1, column2, ... column_n) REFERENCES parent_table (column1, column2, ... column_n) );

Check Constraint

CREATE TABLE table_name ( column datatype null/not null, column2 datatype null/ not null, ... CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE] );

Unique Constraint

CREATE TABLE table_name ( column datatype [ NULL | NOT NULL ], column datatype [ NULL | NOT NULL ], ... CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n) );

AND & OR operator

SELECT ID, NAME, SALARY FROM

CUSTOMERS WHERE SALARY > 2000 AND

age < 25; SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000 OR age < 25;

NOT logical Operator

SELECT first_name, last_name, games FROM student_details WHERE NOT games = 'Football'

Not between (comparison)

SELECT * FROM customers WHERE customer_id NOT BETWEEN 3000 AND 3500;

Comparison Operators

SELECT first_name, last_name, age FROM student_details WHERE age BETWEEN 10 AND 15; The IN operator is used when you want to compare a column with more than one value. It is similar to an OR condition.