kopia lustrzana https://github.com/animator/learn-python
Add_stringinterpolation
rodzic
62d94493b1
commit
65bac1c26b
|
@ -1,238 +0,0 @@
|
|||
# Introduction to SQL
|
||||
|
||||
SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. Developed initially in the 1970s, SQL has become the standard language for interacting with databases across various platforms and environments. It provides a structured approach to defining, querying, updating, and managing data stored in relational database management systems (RDBMS) such as MySQL, PostgreSQL, Oracle, SQL Server, and SQLite.
|
||||
|
||||
SQL operates through a set of declarative commands that enable users to perform essential operations such as retrieving data with SELECT statements, inserting new records with INSERT INTO, updating existing records with UPDATE, and deleting records with DELETE FROM. These commands form the foundation for creating, modifying, and maintaining database schemas and data integrity.
|
||||
|
||||
Beyond basic CRUD (Create, Read, Update, Delete) operations, SQL supports advanced capabilities including:
|
||||
|
||||
Aggregation functions (SUM, AVG, COUNT, etc.) for data analysis
|
||||
Joins to combine data from multiple tables
|
||||
Transaction management for ensuring data consistency and reliability
|
||||
Indexing for optimizing query performance
|
||||
Views, stored procedures, and triggers for encapsulating complex logic within the database
|
||||
SQL’s versatility and standardized syntax make it indispensable in various domains such as software development, data analysis, business intelligence, and system administration. Its ability to handle both simple and complex queries efficiently makes SQL a cornerstone of modern data management practices.
|
||||
|
||||
## Wide Operations in SQL
|
||||
Data Retrieval: Retrieve specific data from databases using SELECT statements.
|
||||
Data Manipulation: Insert, update, and delete records with INSERT INTO, UPDATE, and DELETE statements.
|
||||
Data Definition: Define and modify database schemas, tables, indexes, and constraints.
|
||||
SQL also supports advanced capabilities such as:
|
||||
Joins: Combine data from multiple tables using INNER JOIN, LEFT JOIN, etc.
|
||||
Aggregation: Perform calculations on grouped data using functions like SUM, AVG, COUNT, etc.
|
||||
Transactions: Ensure data consistency and integrity by grouping operations into atomic units.
|
||||
Stored Procedures and Functions: Store and execute reusable procedural logic directly in the database.
|
||||
|
||||
## SQL Commands
|
||||
1.**Extract and Transform Data**
|
||||
- **SELECT**: Extracts data from a database
|
||||
|
||||
Syntax:
|
||||
> SELECT column1, column2, ...FROM table_name;
|
||||
Example:
|
||||
> SELECT * FROM Customers;
|
||||
|
||||
|
||||
2.**Modify Existing Data**
|
||||
- **UPDATE**: Updates data in a database
|
||||
|
||||
Syntax:
|
||||
>UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
|
||||
|
||||
Example:
|
||||
>UPDATE Customers SET ContactName = 'Alfred Schmidt' WHERE CustomerID = 1;
|
||||
|
||||
3.**Remove Unnecessary Data**
|
||||
- **DELETE**: Deletes data from a database
|
||||
|
||||
Syntax:
|
||||
> DELETE FROM table_name WHERE condition;
|
||||
|
||||
Example:
|
||||
> DELETE FROM Customers WHERE CustomerID = 1;
|
||||
|
||||
4.**Add New Entries**
|
||||
- **INSERT INTO**: Inserts new data into a database
|
||||
|
||||
Syntax:
|
||||
> INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
|
||||
|
||||
Example:
|
||||
> INSERT INTO Customers (CustomerName, ContactName) VALUES ('Cardinal', 'Tom B. Erichsen');
|
||||
|
||||
5.**Database Management**
|
||||
- **CREATE DATABASE**: Creates a new database
|
||||
|
||||
Syntax:
|
||||
> CREATE DATABASE databasename;
|
||||
|
||||
Example:
|
||||
> CREATE DATABASE myDatabase;
|
||||
|
||||
- **ALTER DATABASE**: Modifies a database
|
||||
|
||||
Syntax:
|
||||
> ALTER DATABASE database_name [MODIFY option ...]
|
||||
|
||||
Example:
|
||||
>ALTER DATABASE myDatabase MODIFY NAME = newDatabaseName;
|
||||
|
||||
6.**Table Operations**
|
||||
- **CREATE TABLE**: Creates a new table
|
||||
|
||||
Syntax:
|
||||
> CREATE TABLE table_name (
|
||||
column1 datatype,
|
||||
column2 datatype,.... );
|
||||
|
||||
Example:
|
||||
>CREATE TABLE Customers (CustomerID int, CustomerName varchar(255));
|
||||
- **ALTER TABLE**: Modifies a table
|
||||
|
||||
Syntax:
|
||||
> ALTER TABLE table_name ADD column_name datatype;
|
||||
|
||||
Example:
|
||||
>ALTER TABLE Customers ADD Email varchar(255);
|
||||
|
||||
- **DROP TABLE**: Deletes a table
|
||||
|
||||
Syntax:
|
||||
>DROP TABLE table_name;
|
||||
|
||||
Example:
|
||||
> DROP TABLE Customers;
|
||||
|
||||
7.**Index Management**
|
||||
|
||||
- **CREATE INDEX**: Creates an index (search key)
|
||||
|
||||
Syntax:
|
||||
> CREATE INDEX index_name ON table_name (column1 column2, ...);\
|
||||
|
||||
Example:
|
||||
>CREATE INDEX idx_lastname ON Customers (LastName);
|
||||
|
||||
- **DROP INDEX** : Deletes an index
|
||||
|
||||
Syntax:
|
||||
> DROP INDEX index_name ON table_name;
|
||||
|
||||
Example:
|
||||
> DROP INDEX idx_lastname
|
||||
|
||||
## Diving Deeper into SQL: Beyond the Basics
|
||||
|
||||
#### ***Advanced Data Retrieval***
|
||||
1.**SELECT DISTINCT**: Retrieves unique values from a column<br/>
|
||||
Example:
|
||||
> SELECT DISTINCT Country FROM Customers;
|
||||
|
||||
2.**SELECT COUNT(**): Counts the number of rows that match a specified condition<br/>
|
||||
Example:
|
||||
> SELECT COUNT(CustomerID) FROM Customers;
|
||||
|
||||
3.**SELECT AVG()**: Calculates the average value of a numeric column<br/>
|
||||
Example:
|
||||
> SELECT AVG(OrderAmount) FROM Orders;
|
||||
|
||||
4.**SELECT SUM()**: Calculates the total sum of a numeric column<br/>
|
||||
Example:
|
||||
> SELECT SUM(OrderAmount) FROM Orders;
|
||||
|
||||
#### ***Data Filtering and Sorting***
|
||||
1.**WHERE**: Filters records<br/>
|
||||
Example:
|
||||
> SELECT * FROM Customers WHERE Country='Germany';
|
||||
|
||||
2.**AND/OR**: Combines multiple conditions<br>
|
||||
Example:
|
||||
> SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
|
||||
|
||||
3.**ORDER BY**: Sorts the result set<br>
|
||||
Example:
|
||||
>SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;
|
||||
|
||||
#### ***Joins and Subqueries***
|
||||
1.**INNER JOIN**: Returns records that have matching values in both tables<br/>
|
||||
Example:
|
||||
>SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
|
||||
|
||||
2.**LEFT JOIN**: Returns all records from the left table, and the matched records from the right table<br/>
|
||||
Example:
|
||||
>SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
|
||||
|
||||
3.**RIGHT JOIN**: Returns all records from the right table, and the matched records from the left table<br/>
|
||||
Example:
|
||||
> SELECT Orders.OrderID, Customers.CustomerName FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
|
||||
|
||||
4.**FULL JOIN**: Returns all records when there is a match in either left or right table<br/>
|
||||
Example:
|
||||
>SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
|
||||
|
||||
5.**Subquery**: A query nested inside another query<br/>
|
||||
Example:
|
||||
> SELECT CustomerName FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderAmount > 500);
|
||||
|
||||
### Data Grouping and Aggregation
|
||||
1.**GROUP BY**: Groups rows that have the same values into summary rows<br/>
|
||||
Example:
|
||||
> SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;<br>
|
||||
|
||||
2.**HAVING** : Filters records after the GROUP BY statement<br/>
|
||||
Example:
|
||||
> SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
|
||||
|
||||
### Data Constraints
|
||||
|
||||
1.**NOT NULL**: Ensures that a column cannot have a NULL value<br/>
|
||||
Example:
|
||||
> CREATE TABLE Orders (OrderID int NOT NULL, OrderNumber int NOT NULL);
|
||||
|
||||
2.**UNIQUE**: Ensures all values in a column are unique<br/>
|
||||
Example:
|
||||
> CREATE TABLE Customers (CustomerID int UNIQUE, CustomerName varchar(255));
|
||||
|
||||
3.**PRIMARY KEY**: Uniquely identifies each record in a table<br/>
|
||||
Example:
|
||||
>CREATE TABLE Customers (CustomerID int PRIMARY KEY, CustomerName varchar(255));
|
||||
|
||||
4.**FOREIGN KEY**: Uniquely identifies a record in another table<br/>
|
||||
Example:
|
||||
> CREATE TABLE Orders (OrderID int, CustomerID int, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID));
|
||||
|
||||
5.**CHECK**: Ensures that the values in a column satisfy a specific condition<br/>
|
||||
Example:
|
||||
> CREATE TABLE Orders (OrderID int, OrderAmount int CHECK (OrderAmount>0));
|
||||
|
||||
6.**DEFAULT**: Sets a default value for a column if no value is specified.<br/>
|
||||
Example:
|
||||
> CREATE TABLE Orders (OrderID int, OrderStatus varchar(255) DEFAULT 'Pending');
|
||||
|
||||
## RDBMS
|
||||
A Relational Database Management System (RDBMS) is a software system that facilitates the creation, management, and use of relational databases. RDBMSes are built on the principles of relational algebra and structured query language (SQL), providing a structured approach to storing and retrieving data.
|
||||
|
||||
## Key Components of RDBMS
|
||||
Tables: Data in RDBMSes is organized into tables, which consist of rows and columns. Each row represents a record, and each column represents a specific attribute or field of the data.
|
||||
|
||||
Relationships: RDBMSes support relationships between tables through keys. Primary keys uniquely identify each row in a table, while foreign keys establish relationships between tables.
|
||||
|
||||
SQL: SQL is the standardized language used to interact with RDBMSes. It allows users to define, manipulate, query, and control data within the database.
|
||||
|
||||
Data Integrity: RDBMSes enforce data integrity constraints such as entity integrity (ensuring each row is unique with a primary key), referential integrity (maintaining relationships between tables), and domain integrity (ensuring valid data types and values).
|
||||
|
||||
Transactions: RDBMSes support transactions, which are atomic units of work that ensure all operations within a transaction either succeed completely or fail completely, preserving data consistency.
|
||||
|
||||
Indexing: Indexes are used to optimize data retrieval by providing fast access paths to data based on indexed columns.
|
||||
|
||||
## Applications of RDBMS
|
||||
RDBMSes are widely used across various industries and applications:
|
||||
1. Web Applications: Storing user data, session management, and content management systems.
|
||||
2. Enterprise Applications: Managing business data, transactions, and customer relationships.
|
||||
3. Data Warehousing: Storing and analyzing large volumes of data for business intelligence and decision-making.
|
||||
4. E-commerce: Handling product catalogs, order processing, and customer transactions.
|
||||
|
||||
## Conclusion
|
||||
SQL plays a crucial role in managing structured data and is essential for anyone involved in database-driven applications or systems.
|
||||
This introduction provides a concise overview of SQL, highlighting its importance, capabilities, and widespread use in database management. Adjustments can be made based on specific audience or detailed requirements as needed.
|
||||
In summary, RDBMSes play a crucial role in modern data management by providing a structured and efficient method for storing, retrieving, and managing relational data. Their robust features, standardized query language, and support for transactions make them indispensable for applications requiring organized and reliable data storage.
|
|
@ -0,0 +1,45 @@
|
|||
# Python String Interpolation
|
||||
String interpolation in Python is a method of embedding variables or expressions within string literals to create dynamic and formatted strings. It allows for the seamless integration of variable values and expressions into a string, enhancing readability and efficiency in code.
|
||||
|
||||
### 4 Methods for String Interpolation in Python
|
||||
|
||||
1. .format()
|
||||
2. modulo %
|
||||
3. f-string
|
||||
4. Template class
|
||||
|
||||
## Using String Format Indicators
|
||||
String formatting with the modulo operator includes the % character and a format indicator for each of the entries in the string template. A format indicator converts a provided value into the type indicated by the format indicator. The conversion is done before the value is inserted into the string. Python provides the following format indicators:
|
||||
|
||||
- %s: String (performed using the str() function)
|
||||
- %d: Integer
|
||||
- %f: Floating point
|
||||
- %e: Lowercase exponent
|
||||
- %E: Uppercase exponent
|
||||
- %x: Lowercase hexadecimal
|
||||
- %X: Uppercase hexadecimal
|
||||
- %o: Octal
|
||||
- %r: Raw (performed using the repr() function)
|
||||
- %g: Floating point for smaller numbers, lowercase exponent for larger numbers
|
||||
- %G: Floating point for smaller numbers, uppercase exponent for larger numbers
|
||||
- %a: ASCII (performed using the ascii() function)
|
||||
- %c: Converts an int or a char to a character, such as 65 to the letter A<br/>
|
||||
|
||||
**Example 1**
|
||||
``` python
|
||||
x = 'looked'
|
||||
print("Misha %s and %s around"%('walked',x))
|
||||
```
|
||||
**Output**
|
||||
> Misha walked and looked around
|
||||
|
||||
**Example 2**
|
||||
```python
|
||||
print('The value of pi is: %5.4f' %(3.141592))
|
||||
```
|
||||
**Output**
|
||||
>The value of pi is: 3.1416
|
||||
|
||||
|
||||
### Conclusion
|
||||
String interpolation in Python is a powerful method for creating dynamic and formatted strings by embedding variables or expressions within string literals. It significantly enhances code readability and efficiency. Using the modulo operator for string formatting involves the % character along with specific format indicators, which convert and insert values into the string according to their types. This method offers various format indicators such as %s for strings, %d for integers, %f for floating-point numbers, and many others for different data types and representations. Understanding and utilizing these format indicators allows for precise control over string formatting, making the code more flexible and expressive.
|
Ładowanie…
Reference in New Issue