Refer to the given schema. Write a query to create customer and branch tables with the constraints mentioned.
Question
Refer to the given schema. Write a query to create customer and branch tables with the constraints mentioned.
Solution
To create the customer and branch tables with the specified constraints, you can use the following SQL queries.
1. Create the Branch Table
CREATE TABLE branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(100) NOT NULL,
branch_location VARCHAR(100) NOT NULL,
CONSTRAINT unique_branch_name UNIQUE (branch_name)
);
2. Create the Customer Table
CREATE TABLE customer (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100) NOT NULL,
branch_id INT,
email VARCHAR(100) UNIQUE,
phone_number VARCHAR(15),
FOREIGN KEY (branch_id) REFERENCES branch(branch_id)
);
Explanation:
-
Branch Table:
branch_idis defined as anINTtype and is a primary key.branch_nameis aVARCHARtype with a constraint ensuring it cannot be null and is unique.branch_locationis anotherVARCHARfield that also cannot be null.
-
Customer Table:
customer_idis a primary key withINTtype.customer_nameis aVARCHARtype that cannot be null.branch_idestablishes a foreign key relationship with thebranchtable.emailis unique across customers, andphone_numberallows a maximum length of 15 characters.
Summary
These SQL queries create the branch and customer tables and enforce the required constraints such as primary keys, unique constraints, and foreign key relationships.
Similar Questions
write sql query for the following:1. create database - emp2.create table - employee with 4 columns3.To see table structure
Sketch a simple Entity Relational Diagram (ERD) of your table structure, including column names and data types.
Which constraints is used to establish parent-child relation between two or more tables ?
A number of constraints are discussed in the case narrative. Make a concise list of theconstraints. Try to express each constraint mathematically
What do you mean by constraint ? What are the different type of constraint in MySQL? Explain
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.