-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcreate_database.py
More file actions
38 lines (32 loc) · 1.02 KB
/
create_database.py
File metadata and controls
38 lines (32 loc) · 1.02 KB
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
from sqlalchemy import create_engine
def main():
engine = create_engine(
"mysql+pymysql://root:qwerty@localhost:33061/"
)
sql_statements = [
"CREATE SCHEMA IF NOT EXISTS company;",
"""
CREATE TABLE IF NOT EXISTS company.users(
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(40) NOT NULL
);
""",
"ALTER TABLE company.users AUTO_INCREMENT = 1;",
"""
CREATE TABLE IF NOT EXISTS company.archived_users(
id INT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(40) NOT NULL,
archival_date DATETIME NOT NULL DEFAULT NOW()
);
""",
"ALTER TABLE company.archived_users AUTO_INCREMENT = 1;"
]
with engine.connect() as conn:
for sql in sql_statements:
conn.execute(sql)
if __name__ == '__main__':
main()