Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
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
Download now
Download
Save SQL Notes ? For Later
Download
Save
Save SQL Notes ? For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
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
Download now
Download
Save SQL Notes ? For Later
Carousel Previous
Carousel Next
Save
Save SQL Notes ? For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 10
Search
Fullscreen
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 ablyor 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
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6124)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brené Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (933)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8214)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2922)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
3.5/5 (2061)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2619)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2530)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Yes Please
From Everand
Yes Please
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel