-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriting.html
174 lines (162 loc) · 5.94 KB
/
writing.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rauf: AI and ML</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
#comment {
color: lightgreen;
}
body {
background-color: white;
color: black;
}
.main-heading {
color: blue;
}
.navbar {
background-color: rgb(199, 193, 193);
}
.navbar-brand {
font-weight: bold;
color: blue;
}
.navbar-nav .nav-link {
color: blue;
}
.content img {
max-width: 100%;
height: auto;
display: block;
margin: 20px auto;
}
footer {
background-color: white;
color: black;
padding: 15px 0;
}
pre {
background-color: white;
color: blue;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg">
<div class="container">
<a class="navbar-brand" href="https://rauf-psi.vercel.app/">Rauf</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="tutorialDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Notes
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="tutorialDropdown">
<li><a class="dropdown-item" href="index.html">Querying</a></li>
<li><a class="dropdown-item" href="relating.html">Relating</a></li>
<li><a class="dropdown-item" href="design.html">Designing</a></li>
<li><a class="dropdown-item" href="#">Writing</a></li>
<li><a class="dropdown-item" href="viewing.html">Viewing</a></li>
<li><a class="dropdown-item" href="optimizing.html">Optimizing</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<!-- Header -->
<header class="container text-center my-4">
<h1 class="main-heading">Rauf</h1>
<h5>AI and ML</h5>
<h2 class="main-heading">Writing in a Database</h2>
<p>Writing in a database means adding or inserting data to the database. In this Web Note, I will talk about Create, Read, Update, Delete (CRUD), INSERT INTO, CSVs, .import, Triggers, and Soft Deletions using SQLite. Let’s start 😊</p>
</header>
<!-- Content -->
<div class="container content">
<section id="tutorial1" class="mb-5">
<h2 class="main-heading">Create a Database</h2>
<p>To create a database for Nawabshah, the city I live in (located in Sindh, Pakistan), we will add a table for popular places.</p>
<pre><code>
<p id="comment">-- Create a table to store Nawabshah details</p>
CREATE TABLE nawabshah (
id INTEGER PRIMARY KEY, -- Unique ID for each entry
name TEXT, -- Name of the place
popular_places TEXT -- Description of popular places
);
</code></pre>
</section>
<section id="tutorial2" class="mb-5">
<h2 class="main-heading">Read</h2>
<p>Reading data means retrieving it from the database. Here’s how to read data from the table:</p>
<pre><code>
<p id="comment">-- Simple SELECT query to read data</p>
SELECT * FROM nawabshah; -- Fetch all rows from the Nawabshah table
</code></pre>
</section>
<section id="tutorial3" class="mb-5">
<h2 class="main-heading">Update and Delete</h2>
<p>Updating data allows modifying existing entries, and deleting removes entries.</p>
<pre><code>
<p id="comment">-- Update an entry</p>
UPDATE nawabshah
SET name = 'Updated Name'
WHERE id = 1; -- Change name for the entry with ID 1
<p id="comment">-- Delete an entry</p>
DELETE FROM nawabshah
WHERE id = 2; -- Remove the entry with ID 2
</code></pre>
</section>
<section id="tutorial4" class="mb-5">
<h2 class="main-heading">INSERT INTO and CSV</h2>
<p>INSERT INTO is used to add data to a table. Data can also be imported from a CSV file.</p>
<pre><code>
<p id="comment">-- Insert data into the table</p>
INSERT INTO nawabshah (id, name, popular_places)
VALUES (1, 'Quest University', 'The University i study in.');
<p id="comment">-- Import data from a CSV file</p>
.mode csv -- Set mode to CSV
.import file.csv nawabshah -- Import data from file.csv into the Nawabshah table
</code></pre>
</section>
<section id="tutorial5" class="mb-5">
<h2 class="main-heading">Triggers</h2>
<p>Triggers automate actions based on database events.</p>
<pre><code>
<p id="comment">-- Create a trigger to log deletions</p>
CREATE TRIGGER log_deletion
AFTER DELETE ON nawabshah
BEGIN
INSERT INTO deletion_log (deleted_id) VALUES (OLD.id); -- Log the deleted ID
END;
</code></pre>
</section>
<section id="tutorial6" class="mb-5">
<h2 class="main-heading">Soft Deletes</h2>
<p>Soft deletions mark entries as inactive instead of removing them.</p>
<pre><code>
<p id="comment">-- Add an "is_active" column for soft deletions</p>
ALTER TABLE nawabshah ADD COLUMN is_active BOOLEAN DEFAULT 1; -- 1 for active, 0 for inactive
<p id="comment">-- Soft delete an entry</p>
UPDATE nawabshah
SET is_active = 0
WHERE id = 3; -- Mark the entry with ID 3 as inactive
</code></pre>
</section>
</div>
<!-- Footer -->
<footer class="text-center">
<p>24 Dec 2024 Rauf AI and ML</p>
</footer>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>