-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_pkey_finder.html
53 lines (48 loc) · 2.01 KB
/
csv_pkey_finder.html
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
<!DOCTYPE html>
<html>
<head>
<title>CSV Primary Key Finder</title>
</head>
<body>
<h1>CSV Primary Key Finder</h1>
<textarea rows="20" cols="100" id="csv-input"></textarea><br>
<button onclick="findPrimaryKeys()">Find Primary Keys</button>
<h2>Possible Primary Keys</h2>
<p id="output"></p>
<script>
function findPrimaryKeys() {
// Get the CSV input from the textarea
var csv = document.getElementById("csv-input").value;
// Split the CSV into lines
var lines = csv.split("\n");
// Split the first line (header) into columns
var header = lines[0].split(",");
// Check which combinations of columns are unique
var uniqueColumns = [];
for (var numColumns = 1; numColumns <= header.length; numColumns++) {
for (var i = 0; i <= header.length - numColumns; i++) {
var columns = header.slice(i, i + numColumns);
var unique = true;
for (var k = 1; k < lines.length; k++) {
var values = lines[k].split(",");
var key = columns.map((col) => values[header.indexOf(col)]).join("|");
if (uniqueColumns.includes(key)) {
unique = false;
break;
} else {
uniqueColumns.push(key);
}
}
if (unique) {
document.getElementById("output").innerHTML += "Primary key: " + columns.join(", ") + "<br>";
}
}
}
// If no primary key is found, display an error message
if (document.getElementById("output").innerHTML === "") {
document.getElementById("output").innerHTML = "Error: no primary key found";
}
}
</script>
</body>
</html>