Creating a database and then adding to it a foreign key

Assignment Help Database Management System
Reference no: EM13842215

Following is an example of creating a database and then adding to it a foreign key constraint

mysql> create database dbdemo;

Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE vendors(

-> vdr_id int not null auto_increment primary key,

-> vdr_name varchar(255)

-> )ENGINE=InnoDB;

Query OK, 0 rows affected (0.01 sec)

CREATE TABLE categories(

-> cat_id int not null auto_increment primary key,

-> cat_name varchar(255) not null,

-> cat_description text

-> ) ENGINE=InnoDB;

Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE products(

-> prd_id int not null auto_increment primary key,

-> prd_name varchar(355) not null,

-> prd_price decimal,

-> cat_id int not null,

-> FOREIGN KEY fk_cat(cat_id)

-> REFERENCES categories(cat_id)

-> ON UPDATE CASCADE

-> ON DELETE RESTRICT

-> )ENGINE=InnoDB;

Query OK, 0 rows affected (0.01 sec)

mysql> ALTER TABLE products

-> ADD COLUMN vdr_id int not null AFTER cat_id;

Query OK, 0 rows affected (0.02 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE products

-> ADD FOREIGN KEY fk_vendor(vdr_id)

-> REFERENCES vendors(vdr_id)

-> ON DELETE NO ACTION

-> ON UPDATE CASCADE;

Query OK, 0 rows affected (0.02 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> describe City;

+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| CityName | varchar(30) | YES | | NULL | |
| From_There | varchar(20) | NO | PRI | | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from City;
+--------------------+-------------------+
| CityName | From_There |
+--------------------+-------------------+
| Madrid , Spain | Mike JR |
| Sydney, Australia | Jill Engelstein |
| San Francisco, USA | John Smith |
| Hyderabad, India | Sudhir Srinivasan |
+--------------------+-------------------+

4 rows in set (0.00 sec)

mysql> describe people;

+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| email | varchar(20) | NO | PRI | | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from people;
+-------------------+-------------------+
| name | email |
+-------------------+-------------------+
| Jill Engelstein | [email protected] |
| John Smith | [email protected] |
| Mike JR | [email protected] |
| Sudhir Srinivasan | [email protected] |
+-------------------+-------------------+

4 rows in set (0.00 sec)

I am trying to create a foreign key:

mysql> alter table people

-> add foreign key fk_name(name)

-> references City(From_There)

-> on delete no action

-> on update cascade;

ERROR 1215 (HY000): Cannot add foreign key constraint

What is wrong?

Reference no: EM13842215

Questions Cloud

Complete the statement of owners equity : Important complete the Income Statement first, complete the Statement of Owners' Equity second, complete the Balance Sheet third, and complete the Statement of Cash Flows last.
Writing a request for proposal on sales promotion : Writing a Request for Proposal on Sales Promotion. Describe the extent of products and services your organization is looking for, as well as, the overall objectives of the contract
Where in the organism is the cell found? : Where in the organism is the cell found?
Describe the purposes of coding diseases and operations : List and explain five characteristics of useful clinical terminologies. Describe the purposes of coding diseases and operations.
Creating a database and then adding to it a foreign key : creating a database and then adding to it a foreign key constraint
Differences between economic and accounting concepts of cost : What are the differences between economic and accounting concepts of cost. How are prices determined under perfect competition
What is the wavelength of neutrons traveling : What is the wavelength of neutrons traveling
Lisp programming and prolog : You have to do all the assignment need its lisp programming and prolog also, the question 2 do it from Microsoft word please i want perfect assignment also, please no plagiarism and no website its very important to me
Business to start with little capital : Could you tap a skill you already possess. For example, could you be a foreign language tutor Compile a list of all the business opportunities you have discovered and share them with your classmates. What are they willing to pay for your products ..

Reviews

Write a Review

Database Management System Questions & Answers

  Write sql statements to create the corresponding relations

Write SQL statements to create the corresponding relations to the ER diagram you designed for Exercise 2.8. If your translation cannot capture any constraints in the ER diagram, explain why.

  Calculate confidence and support of rule

Assume that 30% of science students are majoring in biology. Would you consider to be novel with respect to rule? Describe. Calculate confidence and support of rule.

  Dml stands for

DML stands for

  Create a data dictionary

Create a data dictionary that includes the following: A description of the content for each field, The data type of each field, The format the data will be stored as in the field

  Create database to produce reports using surrogate keys

Complete following task. In each exercise, represent answer in both DBDL and with diagram. You may use any of styles for diagram. Create database to produce following reports. Do not use any surrogate keys in design.

  Relational database modelling techniques and e-r diagram

Practical experience in using Entity-Relationship and Relational Database modelling techniques - design using an E-R diagram an entity-relationship model of the problem, convert the model into a relational model, and assess the normal form of each ..

  Benefits of using databases and dbmss

Assignment: Research organizational benefits of using databases and DBMS's. Supplement your research with a review of your lessons. Post a response to the discussion board: Respond to the following statements and, if appropriate, include personal e..

  Create an erd from a business problem

List all the business rules for the ABS ( Business rules are important because they define the conditions that the database must meet. For example, every Order must be associated with a valid Product.This prevents invalid Orders being entered into..

  How does data become usable information

How does data become usable information and Find articles to support your argument in both situations. Is the article based on quantitative or qualitative research? Explain

  What happens as a database gets bigger

What happens as a database gets bigger? What if you have more data than the database program was designed to handle?

  Write the vbscript code

Write the VBScript code that initializes all the variables in the numArray(100) to the value numArray(0)=1, numArray(1)=2,&#-123;,numArray(100)=101

  Part 1 - reportwrite a 2000-word report that describes a

part 1 - reportwrite a 2000-word report that describes a suitable methodology from the literature for the purpose of

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd