Page 85 - Computer Software Application TP - Volume 1
P. 85
COMPUTER SOFTWARE APPLICATION - CITS
CHECK: Define custom conditions for valid data (e.g., age must be positive).
4 Create a Database: Use the following SQL command to create a new database:
CREATE DATABASE your database name;
Use the Database: Switch to the newly created database using the following command:
USE your database name;
5 Create Tables: Create the tables for which you want to apply data integrity rules. For example:
CREATE TABLE departments (
id INT AUTO INCREMENT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE employees (
id INT AUTO INCREMENT PRIMARY KEY,
name VARCHAR(100),
department id INT,
FOREIGN KEY (department id) REFERENCES departments(id)
);
In this example, the employees table has a foreign key constraint referencing the id column of the departments
table.
6 Insert Data: Insert some data into the tables you’ve created. Make sure to maintain referential integrity:
INSERT INTO departments (name) VALUES (‘HR’), (‘IT’), (‘Finance’);
INSERT INTO employees (name, department id) VALUES (‘John’, 1), (‘Jane’, 2), (‘Doe’, 3);
70
CITS : IT & ITES - Computer Software Application - Exercise 22