Documente Academic
Documente Profesional
Documente Cultură
Adatabase is an organized collection of facts. In other words, we can say that it is acollection of information arranged and presented to serve an assigned purpose. An example of a database is a dictionary. The data is typically organized to model relevant aspects of reality (for example, the availability of rooms in hotels), in a way that supports processes requiring this information (for example, finding a hotel with vacancies). The term database is correctly applied to the data and their supporting data structures, and not to the database management system (DBMS).
The SQL language is subdivided into several language elements, including: Clauses, which are constituent components of statements and queries. (In some cases, these are optional.) Expressions, which can produce either scalar values or tables consisting of columns and rows of data. Predicates, which specify conditions that can be evaluated to SQL threevalued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow. Queries, which retrieve the data based on specific criteria. This is the most important element of SQL. Statements, which may have a persistent effect on schemata and data, or which may control transactions, program flow, connections, sessions, or diagnostics. Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
CHAR: This data types is used to store character strings values of fixedlength. The size in brackets determines the number of characters the cell canhold. The maximum number of characters (i.e. the size) this data type canhold is 255 characters. The data held is right- padded with spaces to whatever length specified. VARCHAR:This data type is used to store variable lengthalphanumeric data. It is a more flexible form of the CHAR data type. Themaximum this data type can hold up to 4000 characters. One difference between this data type and char data type is oracle compares varchar valuesusing non padded comparison semantics i.e. the inserted values will not be padded with spaces. VARCHAR can hold 1 to 255 characters. Varchar isusually a wiser choice than char due to its variable length formatcharacteristics but keep in mind that char is much faster than varchar sometimes up to 50%. NUMBER: The number data type is used to store numbers (fixed or floating point).The precision (P) determines the length of the data while(s), the scale, determines the number of places after the decimal. The NUMBER data type that is used to store number data can be specified either to store integers or decimals with the addition of a parenthetical precision indicator. If we do not use then the default value is 0 and if we don`t use precision then by default value stored can be of 38 digits.
SQL COMPONENTS
1. DDL (Data DefinitionLanguage):It is a set of SQL commands used to create, modify and delete database structures but not data. They are normally used by the DBA not by user to a limited extent, a database designer or application developer. These statements are immediate i.e. they are not susceptible to rollback commands. It should also be noted that if several DML statements for example updates are executed then issuing any DDL command would commit all the updates as every DDLcommand implicitly issues a commit command to the database. Anybody using DDLmust have the create object privilege and a table space area in which to create objects.For example: - create, alter, drop, truncate, comment etc.
2. DML (Data Manipulation Language):It is the area of SQL that allowschanging data within the database.Examples:INSERT, UPDATE, DELETE etc.
3. DCL (Data Control Language):It is the component of SQL statement thatcontrol access to data and to the database. Occasionally DCL statements are grouped withDML statements.Examples: -COMMIT, SAVEPOINT, ROLLBACK etc.
4. DQL (Data Query Language):It is the component of SQL statement thatallows getting data from the database and imposing ordering upon it. It includes theSELECT statement. This command is the heart of SQL. It allows getting the data out of the database perform operations with it. When a SELECT is fired against a table or tablesthe results is compiled into a further temporary table, which is displayed or perhapsreceived by the program i.e. a front-end.Examples: - SELECT retrieve data from the database.
SQL QUERIES
A query is a concise memo submitted to an editor by a writer seeking publication. It is basicallyan in query to see whether the writer`s work is of interest to a particular publication. A query briefly details a writer`s experience and knowledge of the subject matter, and gives a summary or synopsis of the article the writer hopes to have published. An approximate word count for the proposed article or feature is also generally included. 1. CREATE:The CREATE TABLE command defineseachcolumn of the table uniquely. Each columnhas a minimum of three attributes, a name, data type and size (i.e. column width). Syntax: CREATE TABLE<tablename>(<column name 1><datatype>(size),<column name 2><datatype>(size)); Example:-
2. INSERT:Once a table is created, the mostnaturalthing to do is load this with data to be manipulated later i.e. to insert the rows in a table.
Example:-
3. SELECT:Once data has been inserted into atable,the next most logical operation would be to viewwhat has been inserted. The SELECT SQL verb is used to achieve this. TheSELECT command is used to retrieve rows selected from one or more tables. Syntax: SELECT*FROM<table name>; Example:-
4. ORDER BY:Oracle allows data from a table to be viewed insorted order. The rows retrieve from the table will be sorted either in ascending or descending order depending on the condition specified in the select sentence. Syntax: SELECT * FROM <tablename>ORDER BY<columnname>; Example:-
5. DISTINCT:A table could hold duplicate rows insuch a case, only unique rows the distinctclause can be used. Syntax: SELECT DISTINCT <column name 1>,<column name2> FROM <tablename> ; Example:-
6. ALTER:The structure of a table can bemodified by using the ALTER TABLE command. ALTER TABLE allows changing thestructure of an existing table. With ALTER TABLE it is possible to add or deletecolumns, create or destroy indexes, changes the data type of existing columns, or renamecolumns or the table itself. Syntax:Alter table table name *alter specification+; Such as Add Column, Modify Column ,Rename Column ,Drop Colum ,Add Index, Drop Index, Add Constraint, Drop Constraint. Example:-
7. RENAME:Oracle allows renaming of tables. The rename operationis done atomically, which means that no other thread can accessany of the tables while the rename process is running. Syntax:RENAME <table name> TO <new table name>; Example:-
8. DROP:By using the DROP TABLE statement with the table name we can destroy a specific table. Syntax:DROP TABLE <table name>; Example:-
9. DESCRIPTION:This command displays the columns names, the data types and the special attributes connected tothe table. ]Syntax:DESC<table name>; Example:-
10. UPDATE:The update statement updates columns in the existing table`srowswith new values .The SET clause indicates which column data should be modifying and thenew values that they should hold. The WHERE CLAUSE specifies which rows should be updated. Otherwise all table rows are updated. Syntax:UPDATE<table name>SET<column name>=<expression >WHERE<condition>; Example:-
11. ADDING A PRIMARY KEY:A primary key is used to uniquely identify each row in a table. It can either be part of the actual record itself, or it can be an artificial field (one that has nothing to do with the actual record). A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key. Primary keys can be specified either when the table is created (using CREATE TABLE) or by changing the existing table structure (using ALTER TABLE). (a) While Creating a Table:Syntax:CREATE TABLE<table name>(<column name 1><data type>(size) PRIMARY KEY,<column name 2><data type>(size)); Example:-
(b) After Creating a Table:Syntax:ALTER TABLE<table name> ADD PRIMARY KEY (column name); Example:-
12. ADDING A FOREIGN KEY:Foreign key representrelationships between tables. A foreign key is a column (or a group of columns) whose valuesare derived from the primary key or unique key of some other table. The table in which theforeign key is defined is called a FOREIGN TABLE or DETAIL TABLE.The table thatdefines the primary or unique key and is referenced by the foreign key is called thePRIMARY KEY or MASTER KEY. Syntax:ALTER TABLE <table name> ADD FOREIGN KEY (column name) REFERENCES <table name>(column name); Example:-
14. COUNT:This allows us toCount up the number of row in a certain table. Syntax:SELECT COUNT("column_name") FROM "table_name" Example:-
15.SUM:The SUM function is used to calculate the total for a column. Syntax:SELECT SUM (column name) FROM <table name>; Example:-
16. MAX:The Max function is used to find the maximum value in a column. Syntax:SELECT MAX(columnname)FROM <table name>; Example:-
17. MIN:The Min function is used to find the minimum value in a column. Syntax:SELECT MIN (columnname)FROM <table name>; Example:-
18.AVG:The Avg function is used to calculate the average of a column. Syntax:SELECT AVG (columnname)FROM <table name>; Example:-
19. Like:The LIKE predicate allows comparison of one string value withanother string value, which is not identical. This is achieved by using wildcard characters.Two wildcard characters that are available are: (%) allows to match any string of any length(including zero length) (_) allows to match on a single character Syntax:SELECT<columnname>FROM <tablename>WHERE <columnname> LIKE {PATTERN} Example:-
20. IN:In case a value needs to be compared to a list of values then the IN is used. Syntax:SELECT <column name>FROM <tablename>WHERE<columnname> IN ('value1', 'value2') Example:-
21. BETWEEN:The BETWEENkeyword allows for selecting a range. Syntax:SELECT <columnname>FROM <tablename> WHERE <columnname>BETWEEN 'value1'AND 'value2' Example:-
22. WHERE:Used to conditionally select the data from a table. Syntax:SELECT <columnname>FROM <tablename>WHERE <condition>; Example:-
23. VIEW:A view is a virtual table. A view consists of rows and columns just like a table. The difference between a view and a table is that views are definitions built on top of other tables (or views), and do not hold data themselves. If data is changing in the underlying table, the same change is reflected in the view. A view can be built on top of a single table or multiple tables. It can also be built on top of another view. Syntax:CREATE VIEW<view name> AS SELECT<column name 1>,<column name 2> FROM <table name>; Example:-
24. GROUP BY:The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Syntax:SELECT <columnname1>,SUM(<columnname2>) BY<columnname1>; Example:FROM <tablename>GROUP
25. HAVING:Having is used to filter data based on the group functions. This is similar to WHERE condition but is used with group functions. Group functions cannot be used in WHERE Clause but can be used in HAVING clause. Syntax:SELECT <column name 1>,SUM(column name 2) FROM <table name> GROUP BY<column name 1>HAVING (arithmetic function condition); Example:-
Query no. 26 Sub-Query:Subquery or Inner query or Nested query is a query in a query. A subquery is usually added in the WHERE Clause of the sql statement. Most of the time, a subquery is used when you know how to search for a value using a SELECT statement, but do not know the exact value. Example:-
Query no.27 How to find the duplicate rows in a table. Sytax:Select count (column-name), <column-name> from <table-name> Group by <column-name> Having count(column-name) > 1; Example:-
Query no. 28 How to delete duplicate rows in a table. Syntax:Delete from <table-name> Where (column-name1,column-name2..) NOT IN (selet min(column-name1), column-name2 from <table-name> Group by column-name); Example:-
Query no. 29 How to delete a particular record from table. Syntax:DELETE from table-name Where column-name= expresion Example:-
Query no.30 How to create a table from another table. Syntax:Create table <new table name> AS (Select * from BBA_CAM); Example:-
Query no. 31 Truncate Table:Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.
Example:-
Query no. 32 Column Level Defining Check constraint at Column Level while creating a table:Column-level constraints refer to a single column in the table and do not specify a column name (except check constraints). They refer to the column that they follow. Example:-
Query no.33 Defining Check constraint at Column Level using alter table Syntax:ALTER TABLE<table-name> ADD CHECK (Expression); Example:-
Query no.34 Drop VIEW:This SQL View (Drop statement) would drop/delete the SQL View called sup_orders Syntax:DROP VIEW <view-name>; Example:-
Query no.35 CREATING INDEX:Index in sql is created on existing tables to retrieve the rows quickly.When there are thousands of records in a table, retrieving information will take a long time. Indexes can be created on a single column or a group of columns. When a index is created, it first sorts the data and then it assigns a ROWID for each row. Syntax:CREATE INDEX <INDEX name> ON <table-name> (column-name); Example:-
Query no 36 DROP INDEX The SQL DROP INDEX statement is the SQL command that removes an entire SQL index. You may drop an index permanently when it is no longer useful or temporarily. If the index is harming or not helping performance it could be dropped. Syntax:DROP INDEX <INDEX name>; Example:-
Query no. 37 SQRT Function:SQL SQRT function is used to find out the square root of any number. You can Use SELECT statement to find out squre root of any number as follows: Syntax:Select SQRT (value) from <table-name>; Example:-
Query no.38 EXP Function:SQL SQRT function is used to find out the square root of any number. You can Use SELECT statement to find out squre root of any number as follows: Syntax:SELECT EXP(value) from <table-name>; Example:-
Query no.39 MOD Function:The mod function uses the floor function in its formula, whereas the remainder function uses the round function in its formula. The mod function returns m if n is 0. Syntax:SELECT MOD(m,n) from <tablename>; Example:-
Query no. 40 FLOOR Function:The SQL FLOOR() rounded up any positive or negative decimal value down to the next least integer value. SQL DISTINCT along with the SQL FLOOR() function is used to retrieve only unique value after rounded down to the next least integer value depending on the column specified. Syntax:Select floor (Decimal value) from <table-name>; Example:-
Query no. 41
CEIL Function:This SQL CEIL() will rounded up any positive or negative decimal value within the function upwards. Syntax:Select CEIL (Decimal value) from <table-name>; Example:-
Query no.42 Upper case Function(UPPER):Here we will use the Sql server upper() function to get the result in upper case from the string. Syntax:SELECT UPPER(column_name) FROM table_name Example:-
Query no. 43 Lower case Function (LOWER):Here we will use the Sql server lower() function to get the result in lower case from table. Syntax:SELECT LOWER(column_name) FROM table_name Example:-
Query no. 44 ASCII Function Return numeric value of left-most character Syntax:SELECT ASCII(CHARACTER); Example:-