You are on page 1of 4

Document2.

sql

------------------ DROP KEY -----


USE DROP TABLE TABLENAME
IN CASE DATABASE
DROP DATABASE DATABASENAME

drop the sql table


--- drop table table_name.---> delete for tables in the sql
ex:

show tables;
+------------------------+
| Tables_in_sql_learning |
+------------------------+
| student |
+------------------------+
1 row in set (0.01 sec)

mysql> drop table student;

Query OK, 0 rows affected (0.02 sec)

---- drop database;


--> it required to write the database and name of the database.

ex: drop database databasename.


mysql> drop database sql_learning;
Query OK, 0 rows affected (0.04 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..

Query OK, 1 row affected (0.01 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| joins_subqueries |
| mysql |
| performance_schema |
| sql_classes |
| sys |

Page 1 of 4
Document2.sql

+--------------------+
6 rows in set (0.00 sec)

-------CREATE A TABLE WITH PRIMARY KEY.

create table mobile(


m_id int AUTO_INCREMENT,
m_name varchar(12) not null,
m_color varchar(12),
m_model varchar(12),
m_address varchar(12) default "Hyderabad",
FOREIGN KEY (b_id) REFERENCES battery(b_id),
primary key (id)
);
CREATE TABLE mobile (
m_id INT AUTO_INCREMENT,
m_name VARCHAR(12) NOT NULL,
m_color VARCHAR(12),
m_model VARCHAR(12),
m_address VARCHAR(12) DEFAULT 'Hyderabad',
mobile_b_id INT, -- Assuming each mobile device is associated with a specific
battery ...it is important when we write the foreigh key.

PRIMARY KEY (m_id),


FOREIGN KEY (mobile_b_id) REFERENCES battery(b_id) -- Establishing foreign key
constraint
);

create table battery(


b_id int AUTO_INCREMENT,
b_name varchar(12),
b_color varchar(12),
b_desighModel varchar(14),
b_address varchar(12),
primary key (b_id)
);

--- 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

mysql> 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)
-> );
ERROR 1824 (HY000): Failed to open the referenced table 'wires' out put like these ..

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

drop table wires;


ERROR 3730 (HY000): Cannot drop table 'wires' referenced by a foreign key constraint
'switchboard_ibfk_1' on table 'switchboard'.
so we can't 'delete the foreign key table .

-- Insert values into the battery table


INSERT INTO battery (b_name, b_color, b_designModel, b_address) VALUES
('Li-Ion 3000mAh', 'Black', 'Model X1', '123 Main St'),
('Li-Poly 4000mAh', 'White', 'Model Y2', '456 Elm St');
-- Add more values as needed;

-- Now, insert values into the mobile table


INSERT INTO mobile (m_name, m_color, m_model, m_address, b_id) VALUES
('Samsung Galaxy S21', 'Phantom Black', 'S21', 'New York', 1), -- Assuming the
corresponding b_id exists in the battery table
('iPhone 13 Pro', 'Silver', '13 Pro', 'San Francisco', 2);
-- Add more values as needed;

ADD THE VALUES INTO THE TABLE.

insert into battery (b_name, b_color, b_designModel, b_address)


values('Lithum','Blue','model-x1','123 main street');

SELECT c.child_name, c.child_age, p.parent_name


FROM children c
INNER JOIN parents p ON c.child_parent_id = p.p_id;

Page 4 of 4

You might also like