0% found this document useful (0 votes)
15 views

SQL Notes ?

Uploaded by

l1s22bssy0063
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL Notes ?

Uploaded by

l1s22bssy0063
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
SQL COMMANDS » What is SQL? + Structured Query Language and it helps to make practice on SQL commands which provides immediate results, + SOL is Structured Query Language, which és a. computer language for storing, manipulating and reltieving data stored in relational database, + SQL is the standard language for Relation Database System, + All eclationat database management systems like MySQL, MS Access, and Oracle, Sybase, Informis, ane! SQL Server use SQL as standard database Inngwage, > Why SQL? + Allows users to create and drop databases and tables, * Allows users to describe the data, * Allows users to define the data in database and manipulate that data. + Allows users to access data in relational database management systems * Allows embedding within other languages using SQL modules, libraries & pre-compilers. O Allows users to set permissions on tables, procedures, and views > SQL Arehitecture: + When you are executing an SQL command for any RDBMS, the system os the best way to carry out your request and SQL engine figures ow how to imerpret the task: + There dre various components included in the process, + ‘These components are: © Query Dispatcher o Optimization Engines o Classic Query Engine o SQL Query Engine, ete, + Classic query engine handles all mon-SQL queries but SQL quoty eigine won't handle logical files, + Simple diagram showing SQL Architecture: > SOL Commands: The standard SQL commands to intersect with relational databases are CREATE, SELECT, INSERT, UPDATE, DEI 'E and DROP. + ‘These commands ean be classified into groups based on their nature: > DDL - Data Definition Language: + DDL defines the conceptual schema providing a link between the Tegicdl aind the pligsieal structure of the database. + ‘The functions of the Data Definition Language (DDL) are: 1, DDL defines the physical charactes fields length, fields logical name and also specify relationship among those records. 2, DDL describes the schema and subschsma, 3. DDL indicate the keys of records. 4, DDL provides data security measures, 5, DDL provides for the logical and physical dala independence, Few of the basie commands for DDL tics of each record, filed in the record, field's data type, Command Description CREATE. | Creates new table, a view ofa table, or other object in database ALTER | Modifies on existing database object, such as u table, DROP | Deletes am entire table, a view ofa table or other object in the database. > DML - Data Manipulation Language: modification, replacement, retrieval, sorting aud display of data or records, DML facilitates use of relationship between the records, DML. provides for independence of programming languages by supporting several high-level programming languages like COBOL, PL/I and C++ Few of the basic commands for DML are: ‘Command Deseription SELECT | Reirieves certain records from one ar more tables INSERT | Creates a record UPDATE | Modifies records DELETE | Deletes records > DCL - Data Control Language: > TCL — Transaction Control Language These SQL commands are used for provid ‘The different DCL commands are: security to database objects. ‘Command Deseription| GRANT | Gives a privilege to user REVOKE | Takes back pri "ges granted from user It includes commands to control the transactions in a database system, The commonly used commands ave: ML provides the data manipulation wechaiques like selection, insertion, deletion, updation, Command Description COMMIT | Make all the changes made by the statements issued permanent, ROLLBACK | Undoes ail changes since the begi asave point. ing of transaction or since| ¥ Data Types in SQL: “The following are the most common data types of SQL: DATA TYPE DESCRIPTION ‘A variable-length column, Allowed values are zero, 1 | NUMBI positive and negative numbers 2 | CHAR A variable length field up to 255 char ter in length 3. | VARCHAR/VARCHAR2 | A variable length field up to 2000 character in length ‘A fixed length field, The time is stored as a part of the #: AVENE date. The defaul form is DD/MON/YY 5 | LONG A vasiable length filed up to 2 GB in length pia 'A waviable Teng led wsed Tor binary data wp to 2000 in length = A variable lengih fled used Tor binary dats wp to 20B 3: PERE in fength 1, NUMBER: © Used to store a numeri value in a field column, # tmay be decimal, integer or real value. * General syntax: NUMBER(n, €) Where m specifies the number of digi decimal point Example: marks NUMBER(3), average NUMBER, 3) ind d specifies the number of digi (o right of the 2, CHAR: © Used to store charaeter type dat © General syntax: CHAR(size) o Where size represents the maximum (255 Characters) number of charaeters in a column. © Example: name CHAR(IS) in a column, 3. VARCHAR/VARCHAR2: © Itis used to store variable length alphanumeric data. © General syntax: VARCHAR(size) / VARCHAR2%size) © Whete size represents the maximum (2000 Characters) number of characters ina coluntn, + Example: address VARCHAR2(50) 4. DATE: ® Itis used to store date in columns, + SQL supports the various date formats other than the standard D-MON-YY.. + Example: dob DATE 5. TIME: It is used to store time in columns. SQL supports the various time formats other than the standard hh-mmess Every DATE and TIME can be added, subtracted or compared as it can be done with other data types. 6 LONG: | Itis used to store variable length strings of up to 2GB size. 2. Example: description LONG = Structure of SQL command: Any SQL command is a combination of keywords, identifiers and clauses. Every SQL command begins with a keyword (CREATE, CT, DE meaning to the language. and so on) which as SELECT, FROM and WHERE are keywords, + The clauses are “FROM student” and "WHERE RegNo=109", + Here SELECT and FROM are manclatory, but WHERE is optional. + Name, Student, Rego, are identifier that refers t6 objects in the database, + Nameand RegNo are column names, while Student isa table name, ‘The equal sign is an operator and 109 is a numeric constant. >» What is an Operator in SQL? + Anoperator is a reserved word or a character used primarily in an SQL statement’s WHERE clause to perform operation(s}, such as comparisons and arithmetic operations. © Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a statement. o Arithmetic operators (+, =, *, / %) © Comparison operators (>,<, >=, 15, 15) ‘+ Logical operators (AND, OR, NOT, IN, BETWEEN, EXISTS, ALL, ANY, LIKE, UNIQUE) > SQL Logical Operator Here is a list of all the logical operators available in SQL. ‘Operator Deseripti ALL | The ALL operator is used to compare a value to all values in another value set ‘AND | The AND operator allows the existence of multiple conditions in an SQL statement WHERE clause. NY | The ANY operaior is used to compare a value to any applicable value in the list according to the condition, BETWEEN | The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value. EXISTS | The EXISTS operator is used to search for the presence of a row ina specified table that meets certain criteria, N The IN operator iS used 10 compare a value to & list of Tieral values that have been specified, LIKE | The LIKE operator fs used to comparea value to similar values using wildcard operators. NOT _| The NOT operator reverses the meaning of the logical operator with which it i used. By: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator. OR | The OR operator is used to combine multiple conditions in an SQL statemen’s WHERE clause. ISNULL | The NULL operator is used to compare a value with a NULL value. UNIQUE | The UNIQUE operator searchcs every row of a specificd table for uniqueness (no duplicates). > Implementation of SQL Commands ¥ CREATE TABLE + The SQL CREATE TABLE statement is used to ereate a new table, + Creating a basic table involves naming the table and defining it type. + Syntax: Basic syntax of CREATE TABLE statement is as follows: calumns and each column's data CREATE TABLE Table_name ( column! datatype, column? datatype, coluran3 datatype, columnN datatype, PRIMARY KEY( one or more columns ) ): + Here CREATE TABLE js the keyword followed by the Table_name, followed by an open parenthesis, followed by the column names and data types for that column, and followed by a closed parenthesis, + For each column, a name and a data type must be specified and the column name must be a unique within the table definition. Column definitions are separated by commas (,) Upperease and lowercase letters makes no difference in column names. + Each table must have at least one column, o SQL commands should end icolan (). Example: Create a table “STUDENT” that contains five columns: RegNo, Name, Combination, DOB and Fees, CREATE TABLE STUDENT c RegNo NUMBER (6), Name VARCHAR2 (15), Combination CHAR (4), DOB DATE, Fees NUMBER (9, 2), PRIMARY KEY ( RegNo ) y * Itereates an empty STUDENT table which looks like this RegNo | Name | Combination | DOB | F ip Viewing the table information; ‘o The DESCRIBE or DESC command displays name of the columns, their data type and size along with the constraints, QL? DESCRIBE STUDENT; Nane Null? Type REGHO NOT MULL NUMBER (6) MANE UARCHARZ( 15) COMBINATION CHARGA) 08 DATE FEES NUMBER (442) v ALTER Statement: * The table can be modified or changed by using the ALTER command. + Syntax: Basic syntax of ALTER TABLE statement is as follows: 1, ALTER TABLE Table_name ADD (column_pamel DaType, Clournn_name? DataType...): 2. ALTER TABLE Table_aame MODIFY (column_namel DataType, Cloumn_name2 Data'Type....); 3. ALTER TABLE Table_name DROP (columa_namel DataType. Cloumn_name2 DataType...); + Example: [SQL> RCRER TaOLE STWOCHT ABO. CRowress VaRCNAR? CaO)): tapte attered. [sQU> mLFER TaMRE STUDENT MmDETY (Hodress UaREHARE (49025 Tapte antered [S0Ly @LTER TABLE STUDENT ORD cADORESS); Tate aaterea. + Using the ALTER TABLE command the following tasks cannot be performed ° ° ° © Changing a table name, Changing the column name. Decreasing the size of a column if table data exists. Changing 8 column's data type. ~ DROP TABLE: + The SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers, constraints, sind permission specifications for that table, O Syntax: Basic syntax of - DRO! E> DROP TADLE STUDENT; able dro ¥ INSERT: + The SQL INSERT INTO Statement is used to add new rows of data to a table in the database, + Syntax: ‘There are two basic syntaxes of INSERT INTO statement as follows: + Here, columnl, column?,...calumnN are the names of the columns in the table into which you want to insert data, You may not need to specify the column{s) name in the SQL query if you are adding values for all the columns of the table. But make sure the order of the values is in the same order as the columns in the table, METHOD 1: The SQL INSERT INTO syntax would be as follows: _ Example; Following statements would create sis records in STUDENT abl yor Ranesit GHG o7-quc-99 —sNo0o 402 OHH ‘POHE 45-SEP-99 4403 GANESH AN HANES 4N05. SURESH ai0 anon 4 SHREYA 412 rous selected. ¥ UPDATE: SQL provides the ability to change data through UPDATE command. ‘The UPDATE command used to modify or update an already existing now or sows ofa table, The basic syntax of UPDATE command is given below. ¥ DELETE command: + In SOL, an already existing row or rows are removed from tables through the use of DELETE command. * ‘The basie syntax of DELETE command is given below. Example: ¥ SELECT: * SQL SELECT statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets. Syntax: The basic syntax of SELECT statement is as follows: SELECT column! column2, columaN | Compulsory FROM Table_name; Part [WHERE condition) Optional [GROUPBY columnelist] Part [HAVING condition(s)] [ORDER BY column-name(s)|; + Here, column1, column3...are the fields of'a table whose values you want to fetch. If you want to fetch all the fields available in the field, then you can use the following syntax: Example: Considers UDENT table Saeies, the following records: o7-nuc-99. kor JOH 15-SEP-99 ‘haa GaNe’sH 19-n00-9 ‘tho MAHESH MO SURESH 4440 ARUN sant SHREYA rows celected. + Following is an example, which would fetch REGNO, NAME and COMBINATION ficlds of the customers available in STUDENT table: Ly SELEET AEGHO, WANE, COWOIMATION FRON STUDENT; ECHO HANE cove. wot RAMESH Powe Powe POM Pee Powe Pee ceon rous selected. ¥ DISTINCT: ‘The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetehing only unique records, ‘There may be. situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records, ‘Syntax: The basic syntax of DISTINCT keyword to-eliminate duplicate records is as follows: DISTINCT keyword with the above SELECT query and see the result: This would produce the following result where we do not have any duplicate entry:

You might also like