SQL queries create,select,insert,update,alter,delete,drop syntax

SQL queries Create Table, Select, Insert, Update, Rename a table, Modifying existing column, ALTER TABLE, DELETE, DROP TABLE, TRUNCATE Syntax



SQL queries Create Table syntax

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
column_n datatype );


SQL queries Displaying the Table structure Syntax

DESC table_name;
or
DESCRIBE table_name;


SQL queries INSERT a row into Table Syntax

INSERT INTO Table_Name (column_1, column_2, ..., column_N)
VALUES (value_1, value_2, ..., value_N);
or
INSERT INTO Table_Name VALUES (value_1, value_2, ..., value_N);
or
INSERT INTO Table_Name VALUES(&column1, &column2, ...., &columnN);


SQL queries Selecting all rows from a Table Syntax

SELECT * FROM table_name;


SQL queries Selecting specific columns from a Table Syntax

SELECT column1, column2, column_n FROM table_name;


Selecting specific rows from a Table

SELECT * FROM table_name WHERE condition;


Selecting specific rows and columns from a Table

SELECT column1, column2, column_n FROM table_name WHERE condition;


SQL queries Eliminating duplicate values from the result set

SELECT DISTINCT column1, column2, column_n FROM table_name;


Selecting specific columns from a table according to the sorted values of a
particular column

SELECT column1, column2 FROM table_name ORDER BY column name;
SELECT column1, column2 FROM table_name ORDER BY column name
DESC; [Sorting in descending order]


Selecting specific rows and columns from a Table according to sorted values
of a particular column

SELECT column1, column2, column_n FROM table_name WHERE condition
ORDER BY column name;


SQL queries Combining multiple conditions in WHERE clause using AND, OR, and
NOT operators.

SELECT column1, column2 FROM table_name WHERE condition1 AND
condition2 AND condition3;
SELECT column1, column2 FROM table_name WHERE condition1 OR
condition2 OR condition3;
SELECT column1, column2 FROM table_name WHERE NOT condition;


SQL queries Updating the contents of a table (all rows)

UPDATE table_name SET column1=value1, column2=value2;


Updating the contents of specific rows in a table

UPDATE table_name SET column1= value1, column2=value2 WHERE
condition;


Sql queries Modifying the structure of a table
 Adding new columns

ALTER TABLE table_name ADD ( newcolumn_name1 Datatype(size),
newcolumn_name2 Datatype(size),...., newcolumn_namen Datatype(size));


SQL queries Dropping a column from a table

ALTER TABLE table_name DROP COLUMN column_name;


SQL queries Modifying existing column of a table

ALTER TABLE table_name MODIFY
( column_name newDatatype(newsize));


SQL queries Rename a column of a table

ALTER TABLE table_name RENAME COLUMN oldcolumn_name TO
newcolumn_name;
Note: Restrictions on the ALTER table
-- Change the name of the table
-- Decrease the size of a column if table data exists


SQL queries Rename a table

RENAME table_name TO newtable_name ;


SQL queries Removal of all rows from a table

DELETE FROM table_name;


SQL queries Removal of specific rows from a table

DELETE FROM table_name WHERE condition;


SQL queries Truncating table

TRUNCATE TABLE table_name ;


SQL queries Destroying a table

DROP TABLE table_name ;

Post a Comment

0 Comments