0% found this document useful (0 votes)
7 views6 pages

How Do I Create A Database in SQL

The document provides a comprehensive guide on how to create and manage databases in SQL, including creating databases, using them, creating tables with specific columns and rows, inserting records, displaying records, and establishing foreign key relationships between tables. It includes SQL syntax examples for each operation, such as creating a database, creating tables, inserting data, and querying data. The document emphasizes the importance of primary keys and foreign keys in maintaining data integrity within relational databases.

Uploaded by

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

How Do I Create A Database in SQL

The document provides a comprehensive guide on how to create and manage databases in SQL, including creating databases, using them, creating tables with specific columns and rows, inserting records, displaying records, and establishing foreign key relationships between tables. It includes SQL syntax examples for each operation, such as creating a database, creating tables, inserting data, and querying data. The document emphasizes the importance of primary keys and foreign keys in maintaining data integrity within relational databases.

Uploaded by

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

HOW DO I CREATE A DATABASE IN SQL

To create a database in SQL, you typically use the CREATE DATABASE statement. The
exact syntax may vary slightly depending on the database management system (DBMS) you
are using. Here's a general example:

CREATE DATABASE database_name;

Replace database_name with the name you want to give to your new database. Here's a
breakdown of the statement:

• CREATE DATABASE: This is the SQL command used to create a new database.
• database_name: This is the name you choose for your new database. It should follow
the naming rules of the specific database system you're using.

For example, if you're using MySQL, you might execute the following command to create a
database named "my_database":

CREATE DATABASE my_database;

HOW DO I USE ANOTHER DATABASE

To use another database in SQL, you typically use the USE statement. The exact syntax may
vary depending on the database management system (DBMS) you are using. Here's a general
example:

USE database_name;

Replace database_name with the name of the database you want to switch to. Here's a
breakdown of the statement:

• USE: This is the SQL command used to select a database to work with.
• database_name: This is the name of the database you want to switch to.

For example, if you want to switch to a database named "my_database", you would execute:

USE my_database;

After executing this command, any subsequent SQL queries you execute will be performed
within the context of the "my_database" database until you explicitly switch to another
database or session ends.

HOW TO I CREATE A TABLE WITH 4 COLUMNS 20 ROWS, THE COLUMNS


CONTAINING, MATRIC NUMBER, FULL NAME, DEPARTMENT AND LEVEL
To create a table with 4 columns (Matric Number, Full name, Department, and Level) and 20
rows, you'll need to use SQL's CREATE TABLE statement to define the table structure and
then insert 20 rows of data into it. Here's how you can do it:

-- Create the table

CREATE TABLE Students (

MatricNumber VARCHAR(20),

FullName VARCHAR(100),

Department VARCHAR(50),

Level INT

);

-- Insert 20 rows of data

INSERT INTO Students (MatricNumber, FullName, Department, Level)

VALUES

('M001', 'John Doe', 'Computer Science', 100),

('M002', 'Jane Smith', 'Electrical Engineering', 200),

('M003', 'Alice Johnson', 'Mechanical Engineering', 300),

-- Add more rows as needed

('M020', 'Bob Brown', 'Chemistry', 400);

This SQL script does the following:

1. Creates a table named "Students" with four columns: "MatricNumber", "FullName",


"Department", and "Level".
2. The "MatricNumber" column is defined as a VARCHAR with a maximum length of 20
characters.
3. The "FullName" column is defined as a VARCHAR with a maximum length of 100
characters.
4. The "Department" column is defined as a VARCHAR with a maximum length of 50
characters.
5. The "Level" column is defined as an integer.
6. Inserts 20 rows of data into the "Students" table using the INSERT INTO statement.
You can add more rows by extending the VALUES list in the INSERT INTO statement.
Adjust the values accordingly to match your data.

HOW TO I CREATE A TABLE WITH 4 COLUMNS UNLIMITED ROWS, THE


COLUMNS CONTAINING, MATRIC NUMBER, FULL NAME, DEPARTMENT AND
LEVEL
To create a table with four columns named "Matric Number," "Full Name," "Department," and
"Level," where the number of rows can grow as needed, you can use the following SQL
statement:

CREATE TABLE student_info (

id INT AUTO_INCREMENT PRIMARY KEY,

matric_number VARCHAR(20),

full_name VARCHAR(100),

department VARCHAR(50),

level INT

);

Explanation:

• id INT AUTO_INCREMENT PRIMARY KEY: This creates a primary key column


named 'id' that auto-increments with each new row. It ensures each row has a unique
identifier.
• matric_number VARCHAR(20): This column stores the matriculation number of the
students as strings up to 20 characters long.
• full_name VARCHAR(100): This column stores the full name of the students as
strings up to 100 characters long.
• department VARCHAR(50): This column stores the department of the students as
strings up to 50 characters long.
• level INT: This column stores the level of the students as integers.

You can adjust the data types and lengths according to your specific requirements. For
example, if you need to accommodate longer names or matriculation numbers, you can
increase the length of the VARCHAR fields. Similarly, if you expect a large number of
departments, you may need to adjust the length of the 'department' field accordingly.

After creating the table, you can start inserting data into it using INSERT statements, like this:

INSERT INTO VALUES ('2023001', 'John Doe', 'Computer Science', 200);


This would insert a row with the specified values into the 'student_info' table. You can
continue to insert more rows as needed.
HOW DO I INSERT MORE RECORD IN THIS TABLE
To insert more records into the 'student_info' table, you can use the INSERT INTO statement.
Here's the general syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO student_info (matric_number, full_name, department, level)
VALUES ('2023002', 'Jane Smith', 'Mathematics', 300);

This command would insert a new row into the 'student_info' table with the specified
matriculation number, full name, department, and level values.

You can continue to execute similar INSERT INTO statements to add more records to the
table, each with its unique set of values.

HOW TO DISPLAY THE DATABASE TABLE RECORD


SELECT * FROM table_name;

Replace 'table_name' with the name of the table from which you want to retrieve records.

For example, to display all records from the 'student_info' table:

SELECT * FROM student_info;


This query will retrieve all columns () and all rows from the 'student_info' table. If you only
want to display specific columns, you can specify them instead of using '':
SELECT column1, column2, ... FROM table_name;
SELECT matric_number, full_name FROM student_info;
Executing any of these SELECT statements will display the records (rows) from the specified
table, allowing you to view the data stored within it.
HOW TO DISPLAY ALL THE RECORDS
SELECT * FROM table_name;

Replace 'table_name' with the name of the table from which you want to retrieve all records.

For example, if you want to display all records from the 'student_info' table:

SELECT * FROM student_info;

SELECT * FROM student_info;


This query will retrieve all columns and all rows from the 'student_info' table, displaying the
entire contents of the table.

INSERT INTO student_info (matric_number, full_name, department, level) VALUES


('2023002', 'Jane Smith', 'Mathematics', 300); I DENTIFY
PRIMARY KEY IN THE ABOVE TABLE, ALSON THE ATTRIBUTE
In the provided SQL statement for inserting data into the 'student_info' table, you have not
explicitly defined a primary key constraint within the table schema. However, in the SQL
statement used to create the 'student_info' table, an 'id' column was defined as the primary key.

Let's identify the primary key and the attributes in the 'student_info' table:

1. Primary Key:
• In the 'student_info' table creation statement:

id INT AUTO_INCREMENT PRIMARY KEY,


1.
• Here, the 'id' column is specified as the primary key using the PRIMARY KEY
constraint. It is defined as an integer (INT) and will auto-increment for each new record
added to the table.
2. Attributes:
• The attributes in the 'student_info' table include:
• matric_number
• full_name
• department
• level

Therefore, in the provided INSERT statement, the 'id' column is the primary key of the
'student_info' table, while the other columns ('matric_number', 'full_name', 'department',
'level') are attributes containing data specific to each student.
USING TWO DIFFERENT TABLE, HOW DOES THE FOREIGN KEY WORK
Let's create two tables and establish a foreign key relationship between them, explaining each
step along the way.

Consider two tables: 'students' and 'courses'. Each student may enroll in multiple courses, so
we'll establish a foreign key relationship between them.

1. Create the 'students' table:


CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
student_email VARCHAR(100)
);
1.
• In this table, 'student_id' is the primary key column that uniquely identifies each
student.
2. Create the 'courses' table:
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
• Here, 'course_id' is the primary key column that uniquely identifies each course.
2. Add a foreign key constraint to 'courses' referencing 'students':
ALTER TABLE courses
ADD COLUMN student_id INT,
ADD CONSTRAINT fk_student_id
FOREIGN KEY (student_id) REFERENCES students(student_id);
1.
• In the 'courses' table, we add a new column 'student_id' that will store the foreign key.
• The foreign key constraint 'fk_student_id' references the 'student_id' column in the
'students' table.
2. Explanation:
• By adding a foreign key constraint on the 'student_id' column in the 'courses' table, we
establish a relationship between the 'students' and 'courses' tables.
• This constraint ensures that any value entered into the 'student_id' column in the
'courses' table must already exist in the 'student_id' column of the 'students' table. It
maintains referential integrity between the two tables.
• For example, if we try to insert a course with a 'student_id' that does not exist in the
'students' table, the database system will reject the operation or take other specified
actions based on the constraint settings.
3. Querying with Foreign Keys:
• With the foreign key relationship established, you can now use SQL JOINs to retrieve
data from both tables based on their relationship. For example:
SELECT students.student_name, courses.course_name
FROM students
INNER JOIN courses ON students.student_id = courses.student_id;
By following these steps, you've successfully created two tables and enforced a foreign key
relationship between them, ensuring data integrity and facilitating queries that involve both
tables.

You might also like