Professional Documents
Culture Documents
sql
show tables;
+------------------------+
| Tables_in_sql_learning |
+------------------------+
| student |
+------------------------+
1 row in set (0.01 sec)
--------------------------------------------------------
create a database . next create a table with primary key.
mysql> create database Sql_Classes; ---> create after we write the database after
we write the database name its important
--to remember when we write a database or table and droping the database and
droping the table. we need to write
--DATABASE in any case drop is their we need to write it..
Page 1 of 4
Document2.sql
+--------------------+
6 rows in set (0.00 sec)
--- when you write a foreigh key ... the table that is present already in the
databases if not its working ..
here battery is the foreigh key in the mobile .. so when we write the
mobile as a foreigh key the is already present
inside the table..
foreigh key : when we declare the foreigh key..
we need to write the column name of the another table id ..here you see the .
b_id INT, ..
second write the foreigh key (its the reference of the column ) next references
and use the anothertablename add the column name.
Page 2 of 4
Document2.sql
FOREIGN KEY (b_id) REFERENCES battery(b_id) here its the column name that are
write above ..
--
create table switchboard(
s_id int AUTO_INCREMENT,
s_name varchar(12),
s_color varchar(12),
s_desiner varchar(12),
s_address varchar(12),
w_id int,
primary key(s_id),
foreign key (w_id) references wires(w_id)
);
create table wires(
w_id int AUTO_INCREMENT,
w_name varchar(12),
w_color varchar(12),
primary key (w_id)
);
so lets try it wihtout the next table add the siwtchboard that are declared
to the foreign key ...its show error.now
when you try to add the table with foreign key we need the reference
table should be present in the database if not its shows errors.
WHEN TABLE IS REFERENCED BY FOREIGH KEY WE CAN'T' DELETE THAT TABLE UNTILL YOU
DELETE THE REFERENCE TABLE..
---- here when we declare any key with foreign key that tables are not deleted
untill the parent table delete. lets try.
here parent table its the foreign key table...
child table .... the table name is refereence in the parent table..
here we can't' delete the child table here..if you try it its show error..
Page 3 of 4
Document2.sql
Page 4 of 4