Path : /home/vishqocm/pcib.in/temp/
File Upload :
Current File : /home/vishqocm/pcib.in/temp/create_tables.sql

-- Create student_successes table if it doesn't exist
CREATE TABLE IF NOT EXISTS `student_successes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `company` varchar(255) NOT NULL,
  `position` varchar(255) NOT NULL,
  `success_story` text NOT NULL,
  `graduation_year` int(4) NOT NULL,
  `is_featured` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Update users table to add designation and bio fields if they don't exist
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `designation` varchar(255) DEFAULT NULL;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `bio` text DEFAULT NULL;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `social_links` text DEFAULT NULL;
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `profile_image` varchar(255) DEFAULT NULL;

-- Check if users table has a role column, if not add it
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `role` enum('admin', 'instructor', 'student', 'user') NOT NULL DEFAULT 'user';

-- Check if users table has a status column, if not add it
ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `status` enum('active', 'inactive', 'suspended') NOT NULL DEFAULT 'active';

-- Create or update institute_stats table
CREATE TABLE IF NOT EXISTS `institute_stats` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `total_students` int(11) NOT NULL DEFAULT '0',
  `total_courses` int(11) NOT NULL DEFAULT '0',
  `total_teachers` int(11) NOT NULL DEFAULT '0',
  `total_certifications` int(11) NOT NULL DEFAULT '0',
  `total_placements` int(11) NOT NULL DEFAULT '0',
  `placement_rate` decimal(5,2) NOT NULL DEFAULT '0.00',
  `satisfaction_rate` decimal(5,2) NOT NULL DEFAULT '0.00',
  `years_in_operation` int(11) NOT NULL DEFAULT '0',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Insert default stats if none exist
INSERT INTO `institute_stats` (`id`, `total_students`, `total_courses`, `total_teachers`, `total_certifications`, 
                              `total_placements`, `placement_rate`, `satisfaction_rate`, `years_in_operation`)
SELECT 1, 500, 25, 15, 450, 400, 85.00, 95.00, 15
FROM (SELECT 1) as tmp
WHERE NOT EXISTS (SELECT 1 FROM `institute_stats` WHERE id = 1);

-- Create triggers to automatically update stats when related records change
DELIMITER //

-- Create or replace trigger for incrementing student count when a new student user is added
DROP TRIGGER IF EXISTS after_user_insert //
CREATE TRIGGER after_user_insert AFTER INSERT ON users
FOR EACH ROW
BEGIN
    IF NEW.role = 'student' AND NEW.status = 'active' THEN
        UPDATE institute_stats SET total_students = total_students + 1 WHERE id = 1;
    ELSEIF NEW.role = 'instructor' AND NEW.status = 'active' THEN
        UPDATE institute_stats SET total_teachers = total_teachers + 1 WHERE id = 1;
    END IF;
END //

-- Create or replace trigger for updating instructor count when a user's role changes
DROP TRIGGER IF EXISTS after_user_update //
CREATE TRIGGER after_user_update AFTER UPDATE ON users
FOR EACH ROW
BEGIN
    -- Check if user was changed from a different role to instructor
    IF NEW.role = 'instructor' AND OLD.role != 'instructor' AND NEW.status = 'active' THEN
        UPDATE institute_stats SET total_teachers = total_teachers + 1 WHERE id = 1;
    -- Check if user was changed from instructor to a different role
    ELSEIF OLD.role = 'instructor' AND NEW.role != 'instructor' AND OLD.status = 'active' THEN
        UPDATE institute_stats SET total_teachers = total_teachers - 1 WHERE id = 1;
    -- Check if user was changed from a different role to student
    ELSEIF NEW.role = 'student' AND OLD.role != 'student' AND NEW.status = 'active' THEN
        UPDATE institute_stats SET total_students = total_students + 1 WHERE id = 1;
    -- Check if user was changed from student to a different role
    ELSEIF OLD.role = 'student' AND NEW.role != 'student' AND OLD.status = 'active' THEN
        UPDATE institute_stats SET total_students = total_students - 1 WHERE id = 1;
    END IF;
    
    -- Check if status was changed for instructors or students
    IF NEW.role = 'instructor' AND NEW.status = 'active' AND OLD.status != 'active' THEN
        UPDATE institute_stats SET total_teachers = total_teachers + 1 WHERE id = 1;
    ELSEIF NEW.role = 'instructor' AND NEW.status != 'active' AND OLD.status = 'active' THEN
        UPDATE institute_stats SET total_teachers = total_teachers - 1 WHERE id = 1;
    ELSEIF NEW.role = 'student' AND NEW.status = 'active' AND OLD.status != 'active' THEN
        UPDATE institute_stats SET total_students = total_students + 1 WHERE id = 1;
    ELSEIF NEW.role = 'student' AND NEW.status != 'active' AND OLD.status = 'active' THEN
        UPDATE institute_stats SET total_students = total_students - 1 WHERE id = 1;
    END IF;
END //

-- Create or replace trigger for updating course count when a new course is added
DROP TRIGGER IF EXISTS after_course_insert //
CREATE TRIGGER after_course_insert AFTER INSERT ON courses
FOR EACH ROW
BEGIN
    UPDATE institute_stats SET total_courses = total_courses + 1 WHERE id = 1;
END //

-- Create or replace trigger for updating course count when a course is deleted
DROP TRIGGER IF EXISTS after_course_delete //
CREATE TRIGGER after_course_delete AFTER DELETE ON courses
FOR EACH ROW
BEGIN
    UPDATE institute_stats SET total_courses = total_courses - 1 WHERE id = 1;
END //

DELIMITER ;

-- Insert sample data for instructors if not already in the users table
INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`, `role`, `status`, `designation`, `bio`, `profile_image`)
SELECT 'Rajesh', 'Kumar', '[email protected]', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'instructor', 'active', 'Web Development Expert', 'Over 8 years of industry experience in web development and software engineering.', 'https://randomuser.me/api/portraits/men/41.jpg'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `users` WHERE email = '[email protected]');

INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`, `role`, `status`, `designation`, `bio`, `profile_image`)
SELECT 'Priya', 'Sharma', '[email protected]', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'instructor', 'active', 'Graphic Design Specialist', 'Adobe certified expert with portfolio of work for major brands.', 'https://randomuser.me/api/portraits/women/44.jpg'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `users` WHERE email = '[email protected]');

-- Insert sample students if not already in users table
INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`, `role`, `status`, `profile_image`)
SELECT 'Rahul', 'Verma', '[email protected]', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'student', 'active', 'https://randomuser.me/api/portraits/men/44.jpg'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `users` WHERE email = '[email protected]');

INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`, `role`, `status`, `profile_image`)
SELECT 'Meera', 'Reddy', '[email protected]', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'student', 'active', 'https://randomuser.me/api/portraits/women/63.jpg'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `users` WHERE email = '[email protected]');

-- Insert sample success stories if not already in student_successes
INSERT INTO `student_successes` (`user_id`, `company`, `position`, `success_story`, `graduation_year`, `is_featured`)
SELECT (SELECT id FROM users WHERE email = '[email protected]'), 'TCS', 'Web Developer', 'After completing the web development course, I got placed at TCS as a junior web developer. The practical training helped me stand out during interviews.', 2022, 1
FROM dual
WHERE EXISTS (SELECT 1 FROM users WHERE email = '[email protected]') 
AND NOT EXISTS (SELECT 1 FROM student_successes WHERE user_id = (SELECT id FROM users WHERE email = '[email protected]'));

INSERT INTO `student_successes` (`user_id`, `company`, `position`, `success_story`, `graduation_year`, `is_featured`)
SELECT (SELECT id FROM users WHERE email = '[email protected]'), 'Infosys', 'Graphic Designer', 'The design skills I learned at Popular Computer Institute helped me secure a position at Infosys. Now I work on projects for international clients.', 2021, 1
FROM dual
WHERE EXISTS (SELECT 1 FROM users WHERE email = '[email protected]') 
AND NOT EXISTS (SELECT 1 FROM student_successes WHERE user_id = (SELECT id FROM users WHERE email = '[email protected]'));