sql manual

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

todo:sql edible manual

------> From Albert

Note: different databases, keywords and sentences will be different, if it can not run, you can separate Baidu'xxx database xxx keywords'

import time
import sqlite3

db_path = r’C:\Users\86131\Desktop\ndhd_cw_001.db’
conn = sqlite3.connect(db_path)
c = conn.cursor()

1.select: query

(1) Query a column

c.execute('select field from table')

(2) Query all

c.execute(‘select * from 表’)

2.distinct: de-duplication

(1) De-duplication of a column

c.execute('select distinct field form table')

(2) Multiple columns to remove duplicates (merge multiple columns that are exactly the same)

c.execute('select dintinct field 01 field 02… from table')

(3) All deduplication (merge the entire line exactly the same)

c.execute('select distinct * from 表 ')

3.where: condition

select field from table where field operator value

'''
Operator:
equal to: =
not equal to: <> (in some versions of SQL, written as: !=)
greater than:>
less than: <
greater than greater than: >=
less than or equal to: <=
in a certain range: between
Search for a certain pattern: like
'''

4. AND and OR operators: used to filter records based on more than one condition

c.execute('select field from table where (condition 01 AND condition 02) OR condition 03')

5.order: sort (default ascending, asc ascending, desc ascending)

c.execute('select field 01, field 02, field 03 from table order by field 01, field 02 asc, field 03 desc')

6.insert: insert

(1) Insert directly

c.execute('insert into table values ​​(value 01, value 02,)')

(2) Insert by column

c.execute('insert into table (column 01, column 02, column 03) values ​​(value 01, value 02, value 03)')

7.update: update

c.execute('update table set field = value where condition')

8.delete: delete

(1): Delete table data

c.execute(‘delete from 表’)

(2): Delete row data

c.execute('delete from table where condition')

9.limit: limit the number of query rows

c.execute('select field from table limit number')

10.like + wildcard

c.execute('select * from table where field like wildcard formula')
'''
Wildcard
Replace one or more characters:%
Replace one character: -Any character in the
list (without comma): [charlist]
Not a list Any character in (without comma): [^charlist] or [!charlist]
'''

11.between/in: range

c.execute('select * from table where field between value 01 and value 02')
c.execute('select * from table where field in (value 01, value 02, value 03, …)')

12.Alias:

(1): The table name specifies the alias

c.execute(‘select a.id,a.name,b.money from sql_notebook as a,sql_notebook001 as b where a.id = b.id’)

(1): The field name specifies the alias

c.execute(‘SELECT LastName AS Family, FirstName AS Name FROM Persons’)

13.join:

c.execute(‘select a.id,a.name,b.money from sql_notebook as a,sql_notebook001 as b from a inner join b on a.id = b.id’)

Equivalent to

c.execute('select a.id,a.name,b.money from sql_notebook as a inner join sql_notebook00

'''
JOIN: If there is at least one match in the table, return rows.
LEFT JOIN: even if there is no match in the right table, return all rows from the left table
RIGHT JOIN: even if there is no match in the left table, return all rows from the right table line
FULL JOIN: As long as there is a match in one of the tables, the line returns
' ''

14.union: merge two or more

The result set of the SELECT statement (the SELECT statement must have the same number of columns. The columns must also have similar data types. At the same time, the order of the columns in each SELECT statement must be the same.)

If duplicate values ​​are allowed, use UNION ALL

c.execute('select a.id from sql_notebook as a UNION select b.id from sql_notebook001 as b ')

15.select into: create a backup copy of the table

c.execute(‘SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P’)

ps:sqlite does not apply this syntax, equivalent:

c.execute(‘CREATE TABLE newtable AS SELECT * FROM oldtable’)

16.creat database: create a database

c.execute(‘craeat database db_name’)

17.creat table: create a table

c.execute('CREATE TABLE table name (
field 01 data type,
field 02 data type,
field 03 data type,

)')

todo data type: (temporarily enumerate sqlite)

'''
sqlite:
1. NULL, the value is NULL
2. INTEGER, the value is signed integer, according to the size of the value stored in 1, 2, 3, 4, 6 or 8 bytes
3. REAL, the value is floating point The value is stored as an 8-byte IEEE floating point number
. 4. TEXT, the value is a text string, and the database encoding (UTF-8, UTF-16BE or UTF-16LE) is used to store
5. BLOB, which is just a data block and is stored exactly as input (I.e. no replacement)

Most database engines (every sql database engine except sqlite as far as we know it) use static and rigid types. With static types, the type of data is determined by its container. This container refers to The specific column to be stored.
Sqlite uses a more general dynamic type system. In SQLite, the data type of a value is related to the value itself, not to its container.
Sqlite's dynamic type system is compatible with the more general static type systems of other databases, but at the same time, the dynamic type in sqlite allows it to do things that are not possible with traditional rigid type databases.
'''

18.constraints: constraints

(1)not null: do not add a value to the field, cannot insert new records or update records

c.execute(’’‘CREATE TABLE Kzz
(Code TEXT NOT NULL,
Total_Investment_Amounts REAL NOT NULL);’’’)

(2) unique: the constraint uniquely identifies each record in the database table

Each table can have multiple UNIQUE constraints, but each table can only have one PRIMARY KEY constraint

(2.1) Create unique constraints when creating tables

c.execute(’’‘CREATE TABLE Persons(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)’’’)

(2.2) The table already exists to create a unique constraint

c.execute('ALTER TABLE table ADD UNIQUE (field)'

(2.3) Name the UNIQUE constraint, create a multi-column constraint

c.execute(``'ALTER TABLE table
ADD CONSTRAINT constraint name UNIQUE (field 01, field 02)''')

(2.3) Cancel the UNIQUE constraint

c.execute(’’‘ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID’’’)

(3): PRIMARY KEY: Primary key constraint

(3.1) Create

c.execute(’’‘CREATE TABLE Persons
(
Id_P int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
‘’’)

(3.2) Naming PRIMARY KEY constraints, defining PRIMARY KEY constraints for multiple columns

c.execute(’’‘CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
)
‘’’)

(3.3) The table already exists to create a unique constraint

c.execute(’’‘ALTER TABLE Persons
ADD PRIMARY KEY (Id_P)’’’)

(3.4) Revocation of constraints

c.execute(’’‘ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID’’’)

(4): FOREIGN KEY: foreign key constraint

Delete: The master table can be deleted only when the record in the slave table does not exist. Delete from the table, the main table unchanged

Update: The master table can be updated only when the record in the slave table does not exist. Update the slave table, the master table remains unchanged

(4.1) Create

c.execute(’’‘CREATE TABLE Orders
(
Id_O int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
Id_P int FOREIGN KEY REFERENCES Persons(Id_P)
)’’’)

(4.2) FOREIGN KEY constraints, and define FOREIGN KEY constraints for multiple columns

c.execute(’’‘CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (Id_O),
CONSTRAINT fk_PerOrders FOREIGN KEY (Id_P)
REFERENCES’’’)

(4.3) Create a FOREIGN KEY constraint for the "Id_P" column if the table already exists

c.execute(’’‘ALTER TABLE Orders
ADD FOREIGN KEY (Id_P)
REFERENCES Persons(Id_P)’’’)

(4.4) Cancel the FOREIGN KEY constraint

c.execute(’’‘ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders’’’)

and also:

‘’’
SQL Check
SQL Default
SQL Create Index
SQL Drop
SQL Alter
SQL Increment
SQL View
SQL Date
SQL Nulls
SQL isnull()

SQL 函数
SQL functions
SQL avg()
SQL count()
SQL first()
SQL last()
SQL max()
SQL min()
SQL sum()
SQL Group By
SQL Having
SQL ucase()
SQL lcase()
SQL mid()
SQL len()
SQL round()
SQL now()
SQL format()
‘’’

start_time = time.time()
look = c.execute(sql)
end_time = time.time()
continuous_time = end_time - start_time
print(‘运行该sql语句的时间为{}’.format(str(continuous_time)) + ‘s’)
for i in look:
print(i)

Guess you like

Origin blog.csdn.net/ppkqq/article/details/109923488