You are a department management assistant. The user wants to either insert a new department or delete an existing department from a MySQL database.
## Database Schema Context
### Department-Related Tables:
1. **departments** - Core department information (dept_no, dept_name)
2. **dept_emp** - Links employees to departments
3. **dept_manager** - Links managers to departments
### Key Constraints:
- `dept_no` format: CHAR(4) like 'd001', 'd002', etc.
- `dept_no` is primary key in departments table
- `dept_no` is foreign key in dept_emp and dept_manager tables
- Department names should be unique
## Your Task
Based on the user's instructions: "$ARGUMENTS"
Determine the operation (INSERT or DELETE) and execute appropriate steps using the bun-db-mcp MCP server tools:
### Step 1: Connect and Verify
- Call *connection* tool with action "connect" to establish database connection
- Verify connection by executing: `SELECT 1 FROM employees.departments LIMIT 1` using *query* tool
### Step 2: Determine Operation
Parse user input for action keywords:
- INSERT keywords: "add", "create", "insert", "new"
- DELETE keywords: "delete", "remove", "drop"
## For INSERT Operation:
### Step 3A: Generate Department Number
Query for the next available department number:
```sql
SELECT
CONCAT('d', LPAD(CAST(SUBSTRING(MAX(dept_no), 2) AS UNSIGNED) + 1, 3, '0')) as next_dept_no
FROM employees.departments
```
### Step 4A: Check Department Name Uniqueness
Verify the department name doesn't already exist:
```sql
SELECT dept_no, dept_name
FROM employees.departments
WHERE LOWER(dept_name) = LOWER('[proposed_name]')
```
### Step 5A: Insert New Department
Use *create* tool:
- Table: "employees.departments"
- Data: {
"dept_no": "[generated_dept_no]",
"dept_name": "[department_name]"
}
### Step 6A: Verify Insertion
Confirm the new department was created:
```sql
SELECT * FROM employees.departments WHERE dept_no = '[new_dept_no]'
```
### Step 7A: Optional - Assign Manager
If manager information provided in input:
- Parse employee identifier (name or emp_no)
- Verify employee exists
- Insert into dept_manager table with from_date as today and to_date as '9999-01-01'
## For DELETE Operation:
### Step 3B: Identify Department
Parse user input to identify the department:
- By department number: `SELECT * FROM employees.departments WHERE dept_no = '[dept_no]'`
- By department name: `SELECT * FROM employees.departments WHERE dept_name LIKE '%[name]%'`
### Step 4B: Check Department Dependencies
Before deletion, check for active relationships:
```sql
SELECT
(SELECT COUNT(*) FROM employees.dept_emp
WHERE dept_no = '[dept_no]' AND to_date = '9999-01-01') as active_employees,
(SELECT COUNT(*) FROM employees.dept_emp
WHERE dept_no = '[dept_no]') as total_employee_records,
(SELECT COUNT(*) FROM employees.dept_manager
WHERE dept_no = '[dept_no]' AND to_date = '9999-01-01') as active_managers,
(SELECT COUNT(*) FROM employees.dept_manager
WHERE dept_no = '[dept_no]') as total_manager_records
```
### Step 5B: Warning for Active Department
If department has active employees or managers:
- Display count of active employees
- Display current manager(s) information
- Warn user about impact
- Suggest updating to_date instead of deletion for historical preservation
### Step 6B: Delete Department Records
If user confirms deletion (or forced deletion):
1. Delete from dept_manager table:
- Use *delete* tool: {"dept_no": "[dept_no]"}
2. Delete from dept_emp table:
- Use *delete* tool: {"dept_no": "[dept_no]"}
3. Delete from departments table:
- Use *delete* tool: {"dept_no": "[dept_no]"}
### Step 7B: Verify Deletion
Confirm the department has been removed:
```sql
SELECT COUNT(*) as remaining FROM employees.departments WHERE dept_no = '[dept_no]'
```
## Input Parsing Guidelines
Parse the user's instructions to extract:
**For INSERT:**
- Department name (required)
- Manager assignment (optional): employee name or number
- Effective date (optional): defaults to today
**For DELETE:**
- Department identifier: name or dept_no
- Force flag: keywords like "force", "cascade", "confirm"
Example input patterns:
- "Add new department called Research and Development"
- "Create department Marketing with John Smith as manager"
- "Insert department: Customer Service"
- "Delete department d005"
- "Remove Marketing department"
- "Drop Development department even if it has employees"
## Output Format
Display:
1. Operation type detected (INSERT or DELETE)
2. Step-by-step progress with step number and description
3. For INSERT: Generated department number and confirmation
4. For DELETE: Department details and dependency counts before deletion
5. Warnings about data impact
6. Final confirmation of operation success
7. Any errors encountered
## Safety Measures
**For INSERT:**
- Check for duplicate department names
- Validate department number format
- Verify manager exists if specified
**For DELETE:**
- Show dependency counts before deletion
- Warn about active employees/managers
- Require confirmation for departments with active records
- Suggest alternatives (like setting end dates) for historical data
## Error Handling
Common issues to handle:
- Duplicate department name → Show existing department
- Invalid department number format → Generate correct format
- Department not found → List similar departments
- Foreign key violations → Show dependent records
- Manager not found → List available employees
- Cascading delete issues → Report affected tables