Issuu on Google+

DATA DEFINITON LANGUAGE SQL QUERIES Contents

1 Introduction 1.1 CREATE 1.1.1 CREATE DATABASE 1.1.2 CREATE TABLE 1.1.3 CREATE INDEX 1.1.4 CREATE VIEW 1.2 ALTER 1.2.1 ALTER TABLE COMMAND 1.2.2 ALTER TABLE COLUMN ADD 1.2.3 ALTER TABLE COLUMN MODIFY 1.2.4 ALTER TABLE RENAME COLUMN 1.2.5 ALTER TABLE DROP COLUMN 1.2.6 ALTER TABLE / RENAME TABLE 1.3 DROP 1.3.1 DROP TABLE 1.3.2 DROP VIEW 1.3.3 DROP INDEX 1.3.4 DROP DATABASE 1.4 TRUNCATE 2 Data Types 3 SQL Constraints 3.1 Constraint NOT NULL 3.2 Constraint DEFAULT 3.3 Constraint UNIQUE 3.4 CONSTRAINT CHECK 3.5 CONSTRAINT PRIMARY KEY 3.6 CONSTRAINT FOREIGN KEY

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES 1.Introduction Data Definition Language (DDL) statements are used to manage the database structure or schema. Some DDL commands are as follows.

1.1 CREATE CREATE command is used to create a new database, table,index, views and stored procedure in RDMS.

1.1.1 CREATE DATABASE CREATE DATABASE is a DDL command to create a database object in RDMS engine. Syntax:

CREATE DATABASE <DB_NAME>; Example:

CREATE DATABASE DB_CONTACTS; This command will create DB_CONTACTS database object in RDMS engine.

1.1.2 CREATE TABLE CREATE TABLE command is used to create table object in a database.

Syntax:

CREATE TABLE <TABLE_NAME>( COL_1 column_type, COL_2 column_type, COL_3 column_type);

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Example:

CREATE TABLE customer( First_Name varchar(50), Last_Name varchar(50), Address varchar(70), City varchar(10), Country varchar(15), Birth_Date date); This command will create a customer table in a database.

1.1.3 CREATE INDEX CREATE INDEX is used to create an index in a table. Index is an ordered list of the contents of column, (or group of columns) of a table which improves the speed to locate records when records are fetched from a table. Syntax:

CREATE INDEX <INDEX_NAME> ON <TABLE_NAME>(COLUMN_NAME); Example:

CREATE INDEX idx_cust_id ON CUSTOMER(cus_id); Here Index is created on CUSTOMER table on column cus_id with index name idx_cust_id

1.1.4 CREATE VIEW CREATE VIEW is used to create a VIEW object in a database. View is a virtual table based on SELECT query that is used to restrict other users to view certain columns of whole table from which it is created.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Syntax:

CREATE VIEW <VIEW_NAME> AS SELECT COL1,COL2,COLN FROM <TABLE_NAME>; Example:

CREATE VIEW EMP_INFO_VIEW AS SELECT EMP_ID,EMP_NAME,EMP_DESIG FROM EMP_TBL; Here EMP_INFO_VIEW View is created with EMP_ID,EMP_NAME,EMP_DESIG columns from EMP_TBL

1.2 ALTER ALTER command is used to modify existing structure of database objects.

1.2.1 ALTER TABLE COMMAND The SQL ALTER TABLE statement is used to add, modify or drop/delete columns in a table.

1.2.2 ALTER TABLE COLUMN ADD Syntax:

ALTER TABLE <TABLE_NAME> ADD(col_name col_type); Example:

ALTER TABLE CUSTOMER ADD(cust_id int); Here cust_id column is added to existing table CUSTOMER.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES

1.2.3 ALTER TABLE COLUMN MODIFY Syntax:

ALTER TABLE <TABLE_NAME> MODIFY(col_name col_type); Example:

ALTER TABLE CUSTOMER MODIFY(FIRST_NAME VARCHAR(50)); Here FIRST_NAME column type modified in existing table CUSTOMER.

1.2.4 ALTER TABLE RENAME COLUMN Syntax:

ALTER TABLE <TABLE_NAME> RENAME COLUMN COL_NAME TO ANOTHER_COL_NAME; Example:

ALTER TABLE CUSTOMER RENAME COLUMN CITY TO CUS_CITY; Here CITY column name is renamed to CUS_CITY in existing table CUSTOMER.

1.2.5 ALTER TABLE DROP COLUMN Syntax:

ALTER TABLE <TABLE_NAME> DROP COLUMN COL_NAME;

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Example:

ALTER TABLE CUSTOMER DROP COLUMN CITY; Here CITY column is dropped from existing table CUSTOMER.

1.2.6 ALTER TABLE / RENAME TABLE Syntax:

ALTER TABLE <table_name> RENAME TO <new_table_name>; Example:

ALTER TABLE CUSTOMER RENAME TO EAST_CUSTOMER; Here CUSTOMER table is renamed to EAST_CUSTOMER.

1.3 DROP DROP command is used to delete objects from a database.

1.3.1 DROP TABLE DROP TABLE command is used to delete a table object from the database. Syntax:

DROP TABLE <table_name>; Example:

DROP TABLE CUSTOMER; Here CUSTOMER table is deleted from database.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES 1.3.2 DROP VIEW DROP VIEW command is used to delete a view object from a database. Syntax:

DROP VIEW <view_name>; Example:

DROP VIEW EMP_INFO_VIEW; Here EMP_INFO_VIEW view is deleted from database.

1.3.3 DROP INDEX DROP INDEX command is used to delete an index from a table. Syntax:

DROP INDEX index_name ON <table_name>; Example:

DROP INDEX idx_cust_id ON CUSTOMER; Here idx_cust_id index is deleted from table CUSTOMER.

1.3.4 DROP DATABASE DROP DATABASE command is used to delete an database from a DBMS. Syntax:

DROP DATABASE <database_name>;

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Example:

DROP DATABASE DB_CONTACTS; Here DB_CONTACTS database is deleted from DBMS.

1.4 TRUNCATE Truncate command is used to delete all records from an existing table. Important Point: we can delete all the rows by using following SQL DELETE command.

DELETE FROM TABLE_NAME; By using the DELETE to delete all records each row is deleted individually and it takes more time and this operation will cause all DELETE triggers on the table to fire if DBMS supports triggers whereas TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as a DELETE. Note: The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. Syntax:

TRUNCATE TABLE <table_name>; Example:

TRUNCATE TABLE CUSTOMER; Here CUSTOMER table records are deleted from DBMS.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES 2 Data Types A data type defines what kind of value a column can contain in a table. Column in database contains name and its data type. The developer of the SQL has to decide what type of data is to be stored inside each table column when creating SQL table. General Data Types in SQL are as follows: Data Type

Description

CHARACTER(n) VARCHAR(n) or CHARACTER VARYING(n) BINARY(n)

Character string. Fixed-length n. Character string. Variable length. Maximum length n.

BOOLEAN VARBINARY(n) or BINARY VARYING(n) INTEGER(p)

Stores TRUE or FALSE values. Binary string. Variable length. Maximum length n.

SMALLINT INTEGER BIGINT DECIMAL(p,s) NUMERIC(p,s)

Integer numerical (no decimal). Precision 5. Integer numerical (no decimal). Precision 10. Integer numerical (no decimal). Precision 19. Exact numerical, precision p, scale s. Exact numerical, precision p, scale s. (Same as DECIMAL). Approximate numerical, mantissa precision p. A floating number in base 10 exponential notation. Approximate numerical, mantissa precision 7.

FLOAT(p) REAL FLOAT DOUBLE PRECISION DATE TIME TIMESTAMP INTERVAL ARRAY MULTISET XML

Binary string. Fixed-length n.

Integer numerical (no decimal). Precision p.

Approximate numerical, mantissa precision 16. Approximate numerical, mantissa precision 16. Stores year, month, and day values. Stores hour, minute, and second values. Stores year, month, day, hour, minute, and second values. Composed of a number of integer fields, representing a period, depending on the type of interval. A set-length and ordered collection of elements. A variable-length and unordered collection of elements. Stores XML data.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES The different database have different choices of data type definition like.

Data Type

SQL Server

Oracle

MySQL

integer float

int float real

number number

int / integer Float

currency string (fixed)

money char

N/A char

N/A Char

string (variable)

varchar

varchar2

Varchar

binary object

binary (fixed up to 8K) varbinary (<8K) image (<2GB)

long raw

blob text

boolean

bit

byte

N/A

3 SQL Constraints Constraints are the rules for the data in a table. In SQL, constraints can be specified when the table is created (in CREATE TABLE STATEMENT) or after the table is created. SQL constraints are used to specify rules for the data in a table. Following are the SQL Constraints

3.1 Constraint NOT NULL Ensures that a column cannot have NULL value. Example: NOT NULL WHILE CREATING TABLE

CREATE TABLE STUDENT( Student_Id integer NOT NULL, First_Name char(15), Last_Name char(15) );

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Example: NOT NULL IN EXISTING TABLE

ALTER TABLE STUDENT MODIFY (FIRST_NAME char(15) NOT NULL);

3.2 Constraint DEFAULT Provides a default value for a column when none is specified. Example: DEFAULT CONSTRAINT WHILE CREATING TABLE

CREATE TABLE RESULT( std_id int, std_result char(4) DEFAULT 'PASS‘ ); Example: DEFAULT CONSTRAINT IN EXISTING TABLE

ALTER TABLE RESULT ADD(per float DEFAULT 35.00);

3.3 Constraint UNIQUE Ensures that all values in a column are different. Note: Difference between PK & Unique is that UNIQUE allows NULL Entries. Example: UNIQUE CONSTRAINT WHILE CREATING TABLE

CREATE TABLE ORDERS( order_id integer UNIQUE, invoice_id integer, order_item char(15) ); Example: UNIQUE CONSTRAINT IN EXISTING TABLE

ALTER TABLE orders MODIFY(invoice_id integer UNIQUE);

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES 3.4 CONSTRAINT CHECK Makes sure that all values in a column satisfy certain criteria. It supports boolean evaluation only. Example: CHECK CONSTRAINT (COLUMN LEVEL) WHILE CREATING TABLE

CREATE TABLE emp_reg( emp_id integer, emp_name char(15), emp_age integer CHECK(emp_age>=18) ); Example: CHECK CONSTRAINT (COLUMN LEVEL) IN EXSITING TABLE

ALTER TABLE emp_reg MODIFY(emp_name char(15) CHECK(emp_name=UPPER(emp_name)) Example: CHECK CONSTRAINT (TABLE LEVEL) IN EXSITING TABLE

ALTER TABLE emp_reg ADD( CHECK(emp_id>0));

3.5 CONSTRAINT PRIMARY KEY Used to uniquely identify a row in the table. NOTE: PRIMARY KEY COLUMN WILL NOT ALLOW NULL ENTRIES & ALLOWS UNIQUE COLUMN ENTRIES. Example: PRIMARY KEY (COLUMN LEVEL) WHILE CREATING TABLE

CREATE TABLE cust_detail( sid integer CONSTRAINT CUST_PK PRIMARY KEY, First_Name char(15), Last_Name char(15), Address char(15) ); Example: PRIMARY KEY (TABLE LEVEL) WHILE CREATING TABLE

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES CREATE TABLE cust_detail( sid integer, First_Name char(15), Last_Name char(15), Address char(15), CONSTRAINT CUST_PK PRIMARY KEY(sid) );

3.6 CONSTRAINT FOREIGN KEY A FOREIGN KEY in one table points to a PRIMARY KEY in another table. NOTE: FOREIGN KEY COLUMN WILL ALLOW NULL & DUPLICATE ENTRIES. Example: FOREIGN KEY (COLUMN LEVEL) WHILE CREATING TABLE

CREATE TABLE cust_orders( Order_Id integer CONSTRAINT ORDER_PK PRIMARY KEY, Order_Date date, Customer_sid integer CONSTRAINT FK_CUST REFERENCES cust_detail, Amount float ); Example: FOREIGN KEY (TABLE LEVEL) WHILE CREATING TABLE

CREATE TABLE cust_orders( Order_Id integer, Order_Date date, Customer_sid integer, Amount float, CONSTRAINT ORDER_PK PRIMARY KEY(Order_id), CONSTRAINT FK_CUST FOREIGN KEY(Customer_sid) REFERENCES cust_detail(sid) )

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


DATA DEFINITON LANGUAGE SQL QUERIES Get Free Computer Science Study Kit Android App From

OR

For more lecture notes and Computer Science Study Kit Android app visit us at http://computer-science-study-kit.blogspot.in/ We will be happy to have your comments and ideas about these notes and app also and if you find these notes helpful, please do share them with your friends and colleagues.

Get More Lecture notes and Computer Science Study Kit Android app from: http://computer-science-study-kit.blogspot.in/


Data Definition Language