-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad-KuLStudents.sql
51 lines (43 loc) · 1.95 KB
/
Load-KuLStudents.sql
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
-- Step 1: Use the specified database
CREATE DATABASE IF NOT EXISTS kuloldstudents
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE kuloldstudents;
-- Step 2: Create a temporary table with VARCHAR for the date column and utf8mb4 encoding
CREATE TABLE IF NOT EXISTS temp_students (
Voornaam TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Naam TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Herkomst TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Herkomst_actuele_Schrijfwijze TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Bisdom TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Datum_Inschrijving VARCHAR(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
);
-- Step 3: Load data from the CSV file into the temporary table with UTF-8 encoding
LOAD DATA INFILE 'D:/Downloads/KuLOldStudents-dateModified.csv'
INTO TABLE temp_students
CHARACTER SET utf8mb4
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
-- Step 4: Create the final table with a DATE column for proper storage and utf8mb4 encoding
CREATE TABLE IF NOT EXISTS students (
Voornaam TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Naam TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Herkomst TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Herkomst_actuele_Schrijfwijze TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Bisdom TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Datum_Inschrijving DATE
);
-- Step 5: Transfer data from temp_students to students, converting the date format
INSERT INTO students (Voornaam, Naam, Herkomst, Herkomst_actuele_Schrijfwijze, Bisdom, Datum_Inschrijving)
SELECT
Voornaam,
Naam,
Herkomst,
Herkomst_actuele_Schrijfwijze,
Bisdom,
STR_TO_DATE(Datum_Inschrijving, '%d/%m/%Y')
FROM temp_students;
-- Step 6: Drop the temporary table
DROP TABLE temp_students;
-- Step 7: Verify the import
SELECT * FROM students;