How Do I Create A Database in SQL
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:
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":
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.
MatricNumber VARCHAR(20),
FullName VARCHAR(100),
Department VARCHAR(50),
Level INT
);
VALUES
matric_number VARCHAR(20),
full_name VARCHAR(100),
department VARCHAR(50),
level INT
);
Explanation:
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:
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.
Replace 'table_name' with the name of the table from which you want to retrieve records.
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:
Let's identify the primary key and the attributes in the 'student_info' table:
1. Primary Key:
• In the 'student_info' table creation statement:
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.