Some More Programming Examples and Coding Challenges Week-5
Some More Programming Examples and Coding Challenges Week-5
Some More Programming Examples and Coding Challenges Week-5
...
columnN datatype
);
In this syntax:
CREATE TABLE is the SQL keyword that tells the database system to create a new
table.
table_name is the name of the table you want to create.
column1, column2, ..., columnN are the names of the columns in the table.
datatype specifies the type of data that each column can hold. Examples of data
types include INTEGER, TEXT, REAL, BLOB, and others, depending on the database
system you are using.
You can also specify constraints such as PRIMARY KEY, NOT NULL, UNIQUE, and FOREIGN
KEY within the CREATE TABLE statement to enforce data integrity rules. Here's an example
illustrating the use of some constraints:
CREATE TABLE Students (
student_id INTEGER PRIMARY KEY,
student_name TEXT NOT NULL,
student_age INTEGER,
address TEXT,
grade TEXT
);
In this example, a table named Students is created with columns student_id, student_name,
student_age, address, and grade. The student_id column is defined as the primary key,
while the student_name column is marked as NOT NULL.
In this syntax:
Some More Programming Examples and Coding Challenges -week5
INSERT INTO is the SQL keyword that specifies the action of inserting data.
table_name is the name of the table where you want to insert data.
column1, column2, ..., columnN are the names of the columns in the table into
which you want to insert data.
value1, value2, ..., valueN are the values to be inserted into the corresponding
columns.
Here's an example illustrating the use of the INSERT statement:
DELETE FROM is the SQL keyword that indicates the action of deleting data from a
table.
table_name is the name of the table from which you want to delete data.
WHERE is an optional clause that allows you to specify a condition that must be met
for the deletion to occur. If you omit the WHERE clause, all records in the table will
be deleted.
Here's an example illustrating the use of the DELETE statement:
Some More Programming Examples and Coding Challenges -week5
You can also use more complex conditions in the WHERE clause, such as using logical
operators like AND, OR, and NOT to specify multiple conditions for the deletion.
Syntax
PDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
Assume we have a table named employees with columns employee_id, employee_name,
salary, and hire_date. Let's say we want to update the salary of an employee with ID 123 to
$60,000.
UPDATE employees
SET salary = 60000
WHERE employee_id = 123;
This command will update the salary column of the employees table to 60000 for the row
where the employee_id is 123.
The ORDER BY command in SQL is used to sort the result set by one or more columns. It
arranges the rows returned by a query in either ascending or descending order based on
the specified column(s). Here is the basic syntax for using the ORDER BY clause:
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
In this syntax:
column1, column2, ... are the columns you want to select from the table.
table_name is the name of the table from which you want to retrieve data.
ORDER BY specifies the column(s) you want to sort the result set by.
[ASC | DESC] indicates whether the sorting should be in ascending (ASC) or
descending (DESC) order. ASC is the default if not specified.
Example:
Let's say you have a table named students with columns student_id, student_name, and
age. To retrieve the data sorted by the age column in ascending order, the SQL query would
be:
SELECT student_id, student_name, age
FROM students
ORDER BY age ASC;
This query would return the student_id, student_name, and age for all records in the
students table, sorted by the age column in ascending order.
You can also use multiple columns in the ORDER BY clause to sort the data by multiple
criteria. For example:
SELECT student_id, student_name, age
FROM students
ORDER BY age ASC, student_name ASC;
This query would sort the records first by the age column in ascending order, and for
records with the same age, it would then sort by the student_name column in ascending
order.
____________________________________________________________________
Some More Programming Examples and Coding Challenges -week5
import sqlite3
# Create a connection to the database (or it will create a new one if it doesn't exist)
conn = sqlite3.connect('student.db')
Displaying the Table Contents: The program fetches and displays the contents of the
'Student_Information' table using the SELECT query and a loop that iterates over the
fetched data.
Closing the Connection: Finally, the program closes the connection to the database.
import sqlite3
# Create a connection to the database (or it will create a new one if it doesn't exist)
conn = sqlite3.connect('student.db')
# Create a table to store student information. (''' ''') allows you to write the CREATE TABLE
SQL query across multiple lines without encountering syntax errors or the need for escaping
special characters. It is a way to make the code more readable and manageable.
# When you use the "CREATE TABLE IF NOT EXISTS" statement, the database checks if a
table with the specified name already exists. If it does not, the database creates a new table
with the specified structure. If a table with the same name already exists, the database does
not create a new table and simply continues without any changes to the existing table.
cursor.execute('''
CREATE TABLE IF NOT EXISTS Student_Information (
student_id INTEGER PRIMARY KEY,
name TEXT,
address TEXT,
fname TEXT
)
Some More Programming Examples and Coding Challenges -week5
''')
# Insert some sample data into the table ( List of tuples or list of record ).
students_data = [
(1, 'John Doe', '123 Main St, City, State', 'Michael Doe'),
(2, 'Jane Smith', '456 Maple Ave, Town, State', 'David Smith'),
(3, 'Alex Johnson', '789 Oak Rd, Village, State', 'Chris Johnson'),
(4, 'Emily Williams', '567 Pine Lane, County, State', 'James Williams'),
(5, 'Daniel Brown', '432 Cedar Court, District, State', 'Andrew Brown')
]