-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql
72 lines (52 loc) · 1.64 KB
/
mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# login
sudo mysql -u root -p
# see databases
show databases;
# select database
use database dbname;
# see all tables inside the current db
show tables;
# see all rows inside a table
select * from tablename;
# create table
CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
# load data from file after creating table in mysql
LOAD DATA local INFILE '/path/to/csv' INTO TABLE tablename FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
# delete all rows
truncate table tablename;
# reserved words cannot be used for fields - "CONDITION"
# check for NULL and NOT NULLS
WHERE column_name IS NOT NULL;
# not equal operator
!=
# find duplicates
SELECT OrderID, COUNT(OrderID)
FROM Orders
GROUP BY OrderID
HAVING COUNT(OrderID)>1
# mariadb config
# change CoW on data dir if on btrfs
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql # run before starting mariadb service
# sort by column
ORDER BY COLUMNAME DESC # default order is ASC
# alias for column_name
SELECT col1-col2 AS coldiff FROM tablename;
# python mysql connection
#login
mysql -u linn -p
# kill process
show processlist;
kill <id>
SHOW GRANTS for 'linn'@'localhost';
# see scheme of table
describe tablename;
# string format query in python
sql = """LOAD DATA local INFILE '/path/to/csv'
INTO TABLE dress
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS"""
# create table to store csv data
CREATE TABLE csvdata (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)