0% found this document useful (0 votes)
24 views9 pages

SPa

Uploaded by

percy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

SPa

Uploaded by

percy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Design a transactional database schema for an online banking system.

Include
tables/entities for users, accounts, transactions, and any other relevant entities. Ensure
that your schema supports ACID properties.

-- Users table to store user information


CREATE TABLE Users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
full_name VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Accounts table to store account information


CREATE TABLE Accounts (
account_id SERIAL PRIMARY KEY,
user_id INT REFERENCES Users(user_id),
account_number VARCHAR(20) UNIQUE NOT NULL,
balance DECIMAL(15, 2) NOT NULL CHECK (balance >= 0),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Transactions table to store transaction history


CREATE TABLE Transactions (
transaction_id SERIAL PRIMARY KEY,
account_id INT REFERENCES Accounts(account_id),
amount DECIMAL(15, 2) NOT NULL,
transaction_type VARCHAR(10) NOT NULL CHECK (transaction_type IN ('deposit', 'withdrawal',
'transfer')),
reference VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Index for faster querying of transactions by account_id and created_at


CREATE INDEX idx_transactions_account_created ON Transactions(account_id, created_at);

-- Inserting values into the Users table


INSERT INTO Users (username, password, email, full_name, date_of_birth)
VALUES
('user1', 'password1', '[email protected]', 'John Doe', '1990-01-01'),
('user2', 'password2', '[email protected]', 'Jane Smith', '1985-05-15'),
('user3', 'password3', '[email protected]', 'Alice Johnson', '1998-12-10');

-- Inserting values into the Accounts table


INSERT INTO Accounts (user_id, account_number, balance)
VALUES
(1, 'ACC123456789', 1000.00),
(2, 'ACC987654321', 2500.00),
(3, 'ACC543216789', 500.00);

-- Inserting values into the Transactions table


INSERT INTO Transactions (account_id, amount, transaction_type, reference)
VALUES
(1, 500.00, 'deposit', 'Initial deposit'),
(2, 100.00, 'deposit', 'Initial deposit'),
(3, 200.00, 'deposit', 'Initial deposit');
SELECT * FROM Users;

SELECT * FROM Accounts;

SELECT * FROM Transactions;


Applications of stream processing in different domains, such as gaming

Stream processing is a powerful technique for handling continuous flows of data, and it finds
applications in many surprising domains, including gaming! Here are some examples of how stream
processing is used in gaming:
 Real-time analytics and fraud detection: Stream processing can analyze in-game activity
data to identify suspicious patterns that might indicate cheating or hacks. This allows game
developers to take action quickly and maintain a fair playing environment.
 Personalization and recommendations: By processing player behavior data in real-time,
games can offer personalized recommendations for in-game items, challenges, or even
content. This can enhance player engagement and satisfaction.
 Live game optimization: Stream processing can analyze game server performance metrics to
detect issues like lag or glitches. This allows for dynamic adjustments and optimizations to
ensure a smooth gaming experience.
 Real-time game mechanics: Some games use stream processing to incorporate real-time
events or data feeds into the gameplay itself. For instance, a racing game might use live
weather data to influence in-game weather conditions.
 In-game analytics and reporting: Stream processing can be used to generate real-time
reports on player activity and in-game events. This data can be valuable for game developers
to understand player behavior and make informed decisions about game design and balance.
These are just a few examples, and the potential applications of stream processing in gaming are
constantly evolving as technology advances. As games become more complex and data-driven, stream
processing will play an increasingly important role in creating a dynamic and engaging gaming
experience.

-- Create a table to store player events


CREATE TABLE PlayerEvents (
event_id SERIAL PRIMARY KEY,
player_id INT,
event_type VARCHAR(50),
event_data JSONB,
event_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
-- Example of inserting player events into the PlayerEvents table
INSERT INTO PlayerEvents (player_id, event_type, event_data)
VALUES
(1, 'login', '{"timestamp": "2024-03-14T12:00:00", "location": "New York"}'),
(2, 'logout', '{"timestamp": "2024-03-14T12:05:00", "duration": 300}'),
(3, 'purchase', '{"timestamp": "2024-03-14T12:10:00", "item_id": 123, "amount": 5.99}');

SELECT * FROM PlayerEvents


-- Perform stream processing on the PlayerEvents table
-- Example: Real-time analytics to calculate the number of logins per minute
SELECT
DATE_TRUNC('minute', event_timestamp) AS minute,
COUNT(*) AS login_count
FROM
PlayerEvents
WHERE
event_type = 'login'
GROUP BY
minute
ORDER BY
minute DESC;
Choose a specific application domain (e.g., e-commerce, social networking, IoT) and
design a schema for a document-based NoSQL database like MongoDB.

// Schema for an e-commerce application in MongoDB

// Users collection to store user information


{
"_id": ObjectId(),
"username": "user123",
"password": "hashed_password",
"email": "[email protected]",
"full_name": "John Doe",
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip_code": "12345",
"country": "USA"
},
"created_at": ISODate("2024-03-14T08:00:00Z")
}

// Products collection to store product information


{
"_id": ObjectId(),
"name": "Product ABC",
"description": "Description of Product ABC",
"price": 49.99,
"category": "Electronics",
"stock_quantity": 100,
"created_at": ISODate("2024-03-14T08:00:00Z")
}
// Orders collection to store order information
{
"_id": ObjectId(),
"user_id": ObjectId(),
"order_date": ISODate("2024-03-14T08:00:00Z"),
"items": [
{
"product_id": ObjectId(),
"quantity": 2,
"price": 49.99
},
{
"product_id": ObjectId(),
"quantity": 1,
"price": 29.99
}
],
"total_price": 129.97,
"status": "pending",
"created_at": ISODate("2024-03-14T08:00:00Z")
}

// Reviews collection to store product reviews


{
"_id": ObjectId(),
"product_id": ObjectId(),
"user_id": ObjectId(),
"rating": 4,
"comment": "This product is great!",
"created_at": ISODate("2024-03-14T08:00:00Z")
}
Output:

// Output for Users collection


[
{
"_id": ObjectId("61eb64c5e0dfc2cf53cf787a"),
"username": "user123",
"password": "hashed_password",
"email": "[email protected]",
"full_name": "John Doe",
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip_code": "12345",
"country": "USA"
},
"created_at": ISODate("2024-03-14T08:00:00Z")
}
]

// Output for Products collection


[
{
"_id": ObjectId("61eb64c5e0dfc2cf53cf787b"),
"name": "Product ABC",
"description": "Description of Product ABC",
"price": 49.99,
"category": "Electronics",
"stock_quantity": 100,
"created_at": ISODate("2024-03-14T08:00:00Z")
}
]

// Output for Orders collection


[
{
"_id": ObjectId("61eb64c5e0dfc2cf53cf787c"),
"user_id": ObjectId("61eb64c5e0dfc2cf53cf787a"),
"order_date": ISODate("2024-03-14T08:00:00Z"),
"items": [
{
"product_id": ObjectId("61eb64c5e0dfc2cf53cf787b"),
"quantity": 2,
"price": 49.99
}
],
"total_price": 99.98,
"status": "pending",
"created_at": ISODate("2024-03-14T08:00:00Z")
}
]

// Output for Reviews collection


[
{
"_id": ObjectId("61eb64c5e0dfc2cf53cf787d"),
"product_id": ObjectId("61eb64c5e0dfc2cf53cf787b"),
"user_id": ObjectId("61eb64c5e0dfc2cf53cf787a"),
"rating": 4,
"comment": "This product is great!",
"created_at": ISODate("2024-03-14T08:00:00Z")
}
]

You might also like