SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

SQL for run aways

SQL for database requests

08.01.2020 Eike Hauptmann


© INNEO Solutions GmbH
Why should I learn SQL?

- We use SQL every day in our daily processes


- SQL would be used in:
- Our license tools, daily used programs, the intranet, nearly every website
- PTC Windchill
- PTC Thingworx
-
What SQL means?

- SQL Structured Query Language

- ANSI (since 1986), ISO - Standard (since 1987)


- current: ISO/IEC 9075-1:2016 - ISO/IEC 9075-15:2019
What do I need for it?

- A SQL database:
- File based (SQLite)
-

- A program / service to connect to it


- SQLite Browser (DB Browser for SQLite)
- MSSQL Workbench
- phpMySql
-
- SQL-database work with the ACID paradigm
- A Atomic
- C Consistent
- I Isolated
- D Durable
1983 Theo Härder, Andreas Reuter
Uniqueness

- Primary Key
- unique
- not NULL
- can be one or more columns

- Secondary / Foreign Key


- Connect 2 tables
- 1 : 1 , 1 : n , (n : m) Relations
Standard Commands
SELECT column1, column2, ... FROM table_name;

- SELECT - CREATE TABLE


- INSERT INTO - ALTER TABLE
- DROP TABLE
- UPDATE
- DELETE - CREATE INDEX
- DROP INDEX
- CREATE DATABASE
- ALTER DATABASE
SELECT and ?
SELECT

SELECT * FROM Customers;

SELECT Country FROM Customers;

SELECT DISTINCT Country FROM Customers;

SELECT COUNT(DISTINCT Country) FROM Customers;

SELECT COUNT (*) AS DistinctCountries FROM (SELECT DISTINCT Country


FROM Customers);
SELECT column1, column2, ... FROM table_name WHERE condition;

SELECT * FROM Customers WHERE CustomerID=1;


SELECT * FROM Customers WHERE Country= ;

=, >, <, >=, <=, <>, BETWEEN c1 AND c2, LIKE , IN (c1, c2,

AND, OR, NOT

COUNT (*) GROUP BY


ORDER BY ASC/DESC
SELECT * FROM Customers WHERE Country LIKE );
Symbol Description Example
% Represents zero or more characters bl% finds bl, black, blue, and blob
_ Represents a single character h_t finds hot, hat, and hit
[] Represents any single character within the brackets h[oa]t finds hot and hat, but not hit
^ Represents any character not in the brackets h[^oa]t finds hit, but not hot and hat

- Represents a range of characters c[a-b]t finds cat and cbt


Alias

- Return values and tables can get alias names

SELECT column_name AS alias_name1 FROM table_name AS alias_name2;


SELECT column_names FROM table_name WHERE column_name IS NULL;

SELECT column_names FROM table_name WHERE column_name IS NOT NULL;


INSERT INTO

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQLite
INSERT OR REPLACE

MySQL
INSERT OR UPDATE
UPDATE

UPDATE table_name SET column1 = value1, column2 = value2, ...


WHERE condition;

UPDATE table_name SET column1 = REPLACE(column1,'410','+1-410');


DELETE FROM

DELETE FROM table_name WHERE condition;


- SQL-
- Columns with a primary key are indexed every time
- Can be created explicit
- Would be updated automatically
- Slow the system when there are to much / not used ones

CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);


DROP INDEX index_name;
- Would be updated (mostly) automatically
- Can be used to select or preprepare
- Are an easy way to brake down complex requests and the amount of data
that need to be processed just in time
- Are (mostly) read only

CREATE VIEW view_name AS SELECT * FROM table_name ;


DROP VIEW index_name;
Joins

- INNER JOIN can also be created through WHERE clauses

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM


Orders, Customers WHERE Orders.CustomerID = Customers.CustomerID;

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM


Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
UNION

- UNION is like a DISTINCT FULL OUTER JOIN

SELECT Country FROM Customers UNION SELECT Country FROM Suppliers;

EXCEPT INTERSECT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy