SQL Commands

Embed Size (px)

DESCRIPTION

sql commandsqueries

Citation preview

SQL Commands

SQL CommandsSQL Create TableThe SQL CREATE TABLE statement is used to create a new table. Syntax: Basic syntax of CREATE TABLE statement is as follows: CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) );Create table Using another tableA copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement.The new table has the same column definitions. All columns or specific columns can be selected. When you create a new table using existing table, new table would be populated using existing values in the old table. Syntax:CREATE TABLE NEW_TABLE_NAME AS SELECT [ column1, column2...columnN ] FROM EXISTING_TABLE_NAME [ WHERE ]

Example:Create table :CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Create Table from another table:CREATE TABLE SALARY AS SELECT ID, SALARY FROM CUSTOMERS;

SQL Drop & DeleteSQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers, constraints, and permission specifications for that table. Syntax:DROP TABLE table_name;Example: DROP TABLE CUSTOMERS;

SQL DELETE Query is used to delete the existing records from a table.

Syntax:DELETE FROM table_name WHERE [condition]; Example:DELETE FROM CUSTOMERS WHERE ID = 6; DELETE FROM CUSTOMERS ;

SQL Insert CommandSQL INSERT INTO Statement is used to add new rows of data to a table in the database. Syntax:INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)] VALUES (value1, value2, value3,...valueN);Example:INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'kaushik', 23, 'Kota', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (6, 'Komal', 22, 'MP', 4500.00 );

SQL Update QuerySQL UPDATE Query is used to modify the existing records in a table. Syntax:UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];

Example:

UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;

Alter TableIt is used to alter the table

Syntax:ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE Statement (Rename):

ALTER TABLE table_name RENAME TO new_table_name;