Architecture
create table course (course_id varchar(8), title varchar(50), dept_name varchar(20), credits numeric(2,0) check (credits > 0), primary key (course_id), foreign key (dept_name) references department (dept_name) on delete set null );
I want to add this data to the table but cannot add 0 points.
"CS-001", titled "Weekly Seminar", 0 Credits
Insert query
INSERT INTO `course`(`course_id`, `title`, `credits`) VALUES ('CS-001','Weekly Seminar','0');
search result:
Is there any other way to insert the same data without changing the table structure?
foreign_key_checks
option affects foreign key enforcement, not check constraint enforcement.You must use
ALTER TABLE
, but you do not have to drop the constraint.But once the row is inserted, the check constraint cannot be re-enabled because the row will be re-checked when the constraint is enabled.
You can subsequently delete or update the row that violates the check constraint and re-enable the constraint.
If you need to be able to insert a zero value into the
credits
column, thencheck (credits > 0)
doesn't seem to be the right choice for that column. Maybecheck(credits >= 0)
required.