SQL Operations PDF
Document Details
Uploaded by UnconditionalAmethyst1737
Faculty of Computer Science and Information Technology
Tags
Summary
This document is a tutorial on SQL operations, including `ALTER TABLE`, `UPDATE`, `EXEC`, `SELECT`, and `INSERT INTO` commands. It provides examples and explanations for each command, useful for learning SQL.
Full Transcript
# SQL Operations ## ALTER TABLE These are the uses of the `ALTER TABLE` command: - Add a column to a table - Command ```sql ALTER TABLE table_name ADD column_name datatype constraints; ``` - Edit a column - Command ```sql ALTER TABLE table_n...
# SQL Operations ## ALTER TABLE These are the uses of the `ALTER TABLE` command: - Add a column to a table - Command ```sql ALTER TABLE table_name ADD column_name datatype constraints; ``` - Edit a column - Command ```sql ALTER TABLE table_name ALTER COLUMN column_name datatype constraints; ``` - Delete a column - Command ```sql ALTER TABLE table_name DROP COLUMN column_name; ``` - Add a constraint to a table - Command ```sql ALTER TABLE table_name ADD CONSTRAINT column_name_uq UNIQUE col_name ``` - Delete a constraint from a table - Command ```sql ALTER TABLE table_name DROP CONSTRAINT column_name; ``` ## UPDATE The `UPDATE` command is used to modify data in a table. - Command ```sql UPDATE table_name SET C1 = "N1", ... Cn = newValue WHERE id = 8; ``` - `C1` is the name of the column to be updated. - `"N1"` is the new value of the column. - `Cn` represents other columns being updated. - `newValue` represents the new value of those columns. - `WHERE id = 8` is a condition for specifying which rows to update. ## EXEC The `EXEC` command is used for various purposes, including renaming tables and columns. - Rename a table - Command ```sql EXEC sp_rename 'old_table_name', 'new_table_name'; ``` - Rename a column - Command ```sql EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN'; ``` ## SELECT The `SELECT` command is used to retrieve data from a table. - Select all columns in a table - Command: ```sql SELECT * FROM table_name; ``` - Select all columns from a table based on a condition - Command ```sql SELECT * FROM table_name WHERE id = 8; ``` - Select specific columns from a table and apply a condition - Command ```sql SELECT column1, column2 FROM table_name WHERE id = 8; ``` - Select specific columns from a table and apply a WHERE condition - Command ```sql SELECT column1, column2 FROM table_name WHERE column1 = "value1" and column2 = "value2"; ``` - Select specific columns from a table and apply a WHERE condition with OR - Command ```sql SELECT column1, column2 FROM table_name WHERE column1 = "value1" OR column2 = "value2"; ``` - Select all unique items from a table - Command ```sql SELECT DISTINCT column_name FROM table_name; ``` ## INSERT INTO The `INSERT INTO` command is used to insert new data into a table. - Command ```sql INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,...); ``` - `table_name` is the name of the table to which data is being inserted. - `column1, column2,...` are the names of the columns in the table. - `value1, value2,...` are the values to be inserted into the columns.