Commands of SQL
Commands of SQL
SQL commands are instructions used to communicate with the database to perform specific task that work with data. SQL commands can be used not only for searching the database but also to perform various other functions like, for example, you can create tables, add data to tables, or modify data, drop the table, set permissions for users.
datatype,...column_nameN datatype);
table_name - is the name of the table. column_name1, column_name2.... - is the name of the datatype - is the datatype for the column like char, date,
columns
number etc. For Example: If you want to create the employee table, the statement would be like, CREATE TABLE employee ( id number(5) , name char(20) ,
dept char(10) , age number(2) , sala ry number(10) , location char(10) ); In Oracle database, the datatype for an integer column is represented as "number". In Sybase it is represented as "int". Oracle provides another way of creating a table. CREATE TABLE temp_employee; SELECT * FROM employee; In the above statement, temp_employee table is created with the same number of columns and datatype as employee table.
While inserting a row, if you are adding value for all the columns of the table you need not specify the column(s) name in the sql query. But you need to make sure the order of the values is in the same order as the columns in the table. The sql insert query will be as follows INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN); For Example: If you want to insert a row to the employee table, the query would be like,
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105,'Srinath', 'Aeronautics', 27, 33000);
When adding a row, only the characters or date values should be enclosed with single quotes. If you are inserting data to all the columns, the column names can be omitted. The above insert statement can also be written as, INSERT INTO employee VALUES (105, 'Srinath', 'Aeronautics', 27, 33000); Inserting data to a table through a select statement. Syntax for SQL INSERT is: INSERT INTO table_name [(column1, column2, ... columnN)] SELECT column1, column2, ...columnN FROM table_name [WHERE condition]; For Example: To insert a row into the employee table from a temporary table, the sql insert query would be like, INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id,
emp_name, dept, age, salary, location FROM temp_employee; If you are inserting data to all the columns, the above insert statement can also be written as, INSERT INTO employee SELECT * FROM temp_employee; We have assumed the temp_employee table has columns
emp_id, emp_name, dept, age, salary, location in the above given order and the same datatype. IMPORTANT NOTE: 1) When adding a new row, you should ensure the datatype of the value and the column matches 2) You follow the integrity constraints, if any, defined for the table.
table-name is the name of the table from which the information is column_list includes one or more columns from which data is The code within the brackets is optional.
retrieved.
retrieved.
database table student_details; id first_name last_name age subject 100 101 102 103 104 Rahul Anjali Stephen Shekar Priya Sharma Bhagwat Fleming Gowda Chandra 10 12 09 18 Science Maths Science Maths games Cricket Football Cricket Badminton Chess
15 Economics
These database tables are used here for better explanation of SQL commands. In reality, the tables can have different columns and different data. For example, consider the table student_details. To select the first name of all the students the query would be like: SELECT first_name FROM student_details;
The commands are not case sensitive. The above SELECT statement can also be written as "select first_name from students_details;" You can also retrieve data from more than one column. For example, to select first name and last name of all the students. SELECT first_name, last_name FROM student_details; You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement. We will discuss these commands in coming chapters. In a SQL SELECT statement only SELECT and FROM statements are mandatory. Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.
Output: first_name || ' ' || last_name --------------------------------Rahul Sharma Anjali Bhagwat Stephen Fleming Shekar Gowda Priya Chandra
You can also provide aliases as below. SELECT first_name || ' ' || last_name AS emp_name FROM employee; Output: emp_name ------------Rahul Sharma Anjali Bhagwat Stephen Fleming Shekar Gowda Priya Chandra
The WHERE clause in the sql delete command is optional and it identifies the rows in the column that gets deleted. If you do not include the WHERE clause all the rows in the table is deleted, so be careful while writing a DELETE query without WHERE clause. For Example: To delete an employee with id 100 from the employee table, the sql delete query would be like, DELETE FROM employee WHERE id = 100; To delete all the rows from the employee table, the query would be like, DELETE FROM employee;
Difference between DELETE and TRUNCATE Statements: DELETE Statement: This command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. But it does not free the space containing the table. TRUNCATE statement: This command is used to delete all the rows from the table and free the space containing the table.
the relationships with other tables should be established again. But, if a table is truncated, the table structure remains the same, therefore any of the above problems will not exist.
For Example: To modify the column salary in the employee table, the query would be like ALTER TABLE employee MODIFY (salary number(15,2));