Untitled
Untitled
Untitled
use trendytech;
show tables;
student_mname varchar(30),
student_alternate_phone varchar(15),
student_company varchar(30),
source_of_joining varchar(30),
);
desc students;
-- Alter table two columns for email to set it UNIQUE and for timestamp to reflect current timestamp
alter table students change student_email student_email varchar(30) NOT NULL unique key;
alter table students change enrollment_date enrollment_date timestamp not null default
current_timestamp;
-- For any column which is set as NULL means it can accept NULL value now when you entering the
data in table for this column some records may have values
-- and some records may not have values in that case mention that column name in insert command
and give the value where you have to mention and where you don't need to mention any value you
can give NULL
('Rohit','Sharma','rohit@gmail.com','+917838285358','5','Wipro','05-02-2022','Linkedin'),
('Kapil','Gupta','kapil@gmail.com','+917828596345','7','TCS','07-03-2022','Naukri.com'),
('Govinda','Kapoor','govinda.kapoor@yahoo.com','+919485452453','3','Coforge','05-02-2022','Friend
Reference'),
('Ram','Srivastava','ram.srivastava@gmail.com','+917838295328','5','Wipro','09-03-2022','Linkedin'),
('Mohit','Sharma','mohit@gmail.com','+917838285388','5','Wipro','05-03-2022','Naukri.com'),
('Rajnesh','Sharma','rajnesh123@gmail.com','+917838244358','5','Walmart','05-02-2021','Friend
Reference'),
('Rohit','Gupta','rohit12@gmail.com','+917118285358','5','Wipro','05-02-2022','Linkedin');
-- Now earlier our enrolled_course column was selected to varchar but we making this coloumn as
foreign key from parent table course column course_id so we need to change it to integer type
);
desc course;
insert into course (course_name, course_duration, course_fees) values
-- Here the catch is if there is already data in course_id in course table but as of now the
enrolled_course column in the student table is blank system will not accept this command and it will
throw error
-- Error Code:1452 cannot update or add a child row: a foreign key constraint fails
-- So to overcome this error first you will disable the foreign key checks by running the below
command
set foreign_key_checks = 0;
-- (0 means disabled 1 means enabled if want to enable again then you can run it again by giving 1)
-- Now after running this command run the above command 2 again it will work
-- Now you want to modify/enter the values in multiple rows for students for enrolled_course field
so to modify values in multiple rows you'll run the below command
update students
end)
set foreign_key_checks = 0;
-- Like this only when you will do insert for any student with enrolled_course id out of range in
course id it will still accept because this check is disable
-- so its advisable to enable it again you can try by inserting any column in student table with course
id out of range see it will accept that so enable this again
set foreign_key_checks = 1;
-- After enabling this delete the record you entered above with out of range course_id to verify this
check with false course_id to maintain the reference integrity of the table.
-- To know more about reference integrity of tables in relational DBMS refer this IBM block
-- https://www.ibm.com/docs/en/informix-servers/14.10?topic=integrity-referential
-- Now when you'll enter any record which is out of range of course_id in course table it will throw
error
-- Let's do some more entry in student table to understand the wildcard character %\
select student_fname from students where student_fname like '%hit' order by student_fname;
end)
-- Let's alter the course_fees coloumn in course table to decimal, I have mentioned (7,2) because
there are already records in my course fees coloumn which is of 5 decimal places so if i'll give
insert into course (course_id, course_name, course_duration, fees) values (12, 'DataBricks Engineer',
'5 months', 75000.23);
insert into course (course_id, course_name, course_duration, course_fees) values (13, 'GCP
Engineer', '5 months', 75000.23);
insert into course (course_id, course_name, course_duration, course_fees) values (14, 'AWS
Associate', '5 months', 75000.235); -- This will truncat the last 5 as only two decimal degits are allows
insert into course (course_id, course_name, course_duration, course_fees) values (15, 'Azure
Associate', '5 months', 75555.52);
insert into course (course_id, course_name, course_duration, course_fees) values (16, 'Azure
Associate', '5 months', 755558.52); -- This will throw error as there are more then 5 digits before
decimal
-- Let's alter table for two more coloumn one for entry time of record and one for update time of
record, let;s do this on seperate coloumns
update course set course_name = 'Full Stack Dev' where course_id = 17;
where (years_of_exp > 5 and years_of_exp < 12) order by years_of_exp desc;
-- To classify course as Diploma and Masters we first need to change course_duration coloumn from
varchar to int but before changing coloumns we have to change values
-- from 4 months to 4, 6 months to 6 like this so that when we modify coloumn to int type it should
not give error due to miss match values
END)
END as Degree_Type
from course;
END as Degree_Type
END as Company_Type
from students;
END as Company_Type
-- To create duplicate table with all the records without table schema/structure
desc latest_students;
-- To copy the exact table schema from original table, it will only copy the schema but no records
create table duplicate_students like students;
desc duplicate_students;
-- To copy records from original students table to this new duplicate students table for specific
coloumns
-- Please makesure we have to enter the records for all those coloumns which are set to not null
otherwise it will throw the error
-- To copy all the records in all the coloumns exactly as original table
--