3.SQL Queries DDL
3.SQL Queries DDL
SQL
DDL-COMMANDS
CREATE
• SQL CREATE TABLE statement is used to create
table in a database.
• If you want to create a table, you should name the
table and define its column and each column's data
type.
• To create the table:
• create table table-name (column-name1 datatype1,
column-name2 datatype2, column-name3
datatype3, column-name4 datatype4 );
create table STUDENTS(id number(10), name
varchar(20), age number(4));
•You can verify it, if you have created the table
successfully by looking at the message displayed by the
SQL Server, else you can use DESC command as follows:
•SQL> DESC STUDENTS;
Practice:
Create a table Employee having attributes as id, name,
department, age.
Inserting Data into Table
CREATE TABLE STUDENTS ( ID number(10)
NOT NULL, NAME varchar2(20) NOT NULL, AGE
number(5) NOT NULL, ADDRESS varchar2 (25) );
•It will get that no null value is taken;
Truncate command
• truncate command removes all records from a
table. But this command will not destroy the table's
structure.
truncate table table-name
Example:
truncate table Student;
• The above query will delete all the records
of Student table.
• truncate command is different
from delete command.
Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.
• SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
SELECT * FROM STUDENTS
WHERE name=‘Rohan’;
SQL AND, OR and NOT Operators
1. The AND and OR operators are used to filter records based
on more than one condition.
2. The AND operator displays a record if all the conditions
separated by AND are TRUE.
3. The OR operator displays a record if any of the conditions
separated by OR is TRUE.
4. The NOT operator displays a record if the condition(s) is
NOT TRUE.
SQL AND, OR and NOT Operators
AND Syntax
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
SQL AND, OR and NOT Operators
AND Example
select all fields from "Customers" where country
is “India" AND city is “City2":
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
SQL UNIQUE Constraint