CREATE TABLE table_name(
id INT NOT NULL PRIMARY KEY,
Name TEXT,
Post TEXT,
Age INT LEN<68
)
How do i create that Post attribute where the constraint must be either Manager or clerk.
MySQL does not enforce check constraints. This leaves you with three options:
You can create an enum with two values, 'manager' and 'clerk'.
You can create a reference table that has two rows, and use a foreign key reference.
You can create a trigger to check the values.
By the way, this line:
Age INT LEN<68
is not syntactically correct.
Related
So I tried to make these following two tables with phpmyadmin.
create table category (
catId int identity(1,1),
catName varchar(20),
Constraint pk_category
PRIMARY KEY(catId))
create table subcategory (
subCatId INT IDENTITY(1,1),
catId INT,
subCatName VARCHAR(20),
CONSTRAINT pk_subcategory
PRIMARY KEY(catId,subCatId),
CONSTRAINT fk_subcat_cat
FOREIGN KEY(catID)
REFERENCES category(catId))
When creating the subcategory it shows this query error:
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key .
There aren't two auto incremented columns in subcategory table, only the 'subCatId' is. What should be done?
It is only available for the MyISAM Storage Engine Only one numeric
auto_increment value is allowed. Each auto_increment must be have
associated column to define uniqueness from other auto_increment
values within the same table.
This ref link may hep you more about this.
Note:
Don't do that, don't have a second auto incremented column at all. Do you really need a second auto incremented value? What for? A
description of the actual problem you are trying to solve would help
others help you better. I think you have only told here how you tried
to solve a problem and not what the actual problem is.
This is what MySQL states
There can be only one AUTO_INCREMENT column per table, it must be
indexed, and it cannot have a DEFAULT value. An AUTO_INCREMENT column
works properly only if it contains only positive values. Inserting a
negative number is regarded as inserting a very large positive number.
This is done to avoid precision problems when numbers “wrap” over from
positive to negative and also to ensure that you do not accidentally
get an AUTO_INCREMENT column that contains 0.
So according to your requirement you've following choices.
Make subCatId as Primary Key
Or Make the column as Unique
You appear to be using SQL Server syntax, but run against MySQL. Use the correct syntax and it should work:
CREATE TABLE category (
catId INT NOT NULL AUTO_INCREMENT,
catName VARCHAR(20),
PRIMARY KEY (catId)
)
CREATE TABLE subcategory (
subCatId INT NOT NULL AUTO_INCREMENT,
catId INT,
subCatName VARCHAR(20),
FOREIGN KEY (catId) REFERENCES category (catId),
PRIMARY KEY (subCatId)
);
IDENTITY in SQL Server roughly corresponds to AUTO_INCREMENT in MySQL.
You have
Constraint pk_category
PRIMARY KEY(catId)
Instead, just say
PRIMARY KEY(catId)
I have this code and I want to implement it in MySQL but CHECK is not supported. How would I do it?
CREATE TABLE Fare (
type_of_passenger varchar(20),
price FLOAT,
PRIMARY KEY (type_of_passenger),
CHECK (lower(type_of_passenger)='child' OR lower(type_of_passenger)='student' OR lower(type_of_passenger)='senior')
);
Are you sure you need that check? As the primary key is type_of_passenger you will only ever be able to have three rows.. this seems like over-kill
You could just INSERT those 3 rows and move on. If you reference this field in a foreign key, you'll be restricted to the values in the table anyway
In fact as soon as you reference each value with a foreign key that uses ON UPDATE RESTRICT and ON DELETE RESTRICT you won't be able to change them anyway
The only valid concern I can see here is that you want to allow a DB user to change the price but not the type_of_passenger
If you INSERT the correct (or stub) data to start with, you can then control table and column access via permissions
N.B. I would use a surrogate unsigned integer primary here and unique the string description, thus if I do need to change the string I can do it without worry, and without the performance hit of updating all the tables that reference it
It looks like you're really trying to implement an ENUM:
type_of_passenger ENUM('child', 'student', 'senior')
By default MySQL doesn't validate ENUM values, so if you try to store something else it will store an empty string, but if strict SQL mode is enabled it will report an error.
Another alternative is to make this a foreign key to a table where you enter the valid values.
CREATE TABLE passenger_types (
type VARCHAR(20) NOT NULL PRIMARY KEY
);
INSERT INTO passenger_types (type) VALUES
('child'), ('student'), ('senior');
CREATE TABLE Fare (
type_of_passenger varchar(20),
price FLOAT,
PRIMARY KEY (type_of_passenger),
CONSTRAINT FOREIGN KEY (type_of_passenger) REFERENCES passenger_types (type)
);
I have some values like these:
PHP
CSS
HTML
JavaScript
I want to know is that possible to define some specific values for a column as valid values? (everything else would be invalid)
I know, I can implement a tagging-system the other way. Creating a separated table which contains valid tags and checks everything for validating.
But actually that tagging-system is just as an example. All I'm trying to do is defining some constant values for a column.
You could use an ENUM Type and define your constants when creating the table:
http://dev.mysql.com/doc/refman/5.7/en/enum.html
I would only use it if it is a fixed list of some values. Like "male", "female".
A typical way of doing this uses a foreign key relationship.
You can define a table of values like this:
create table skills (
skillId int auto_increment primary key,
skill varchar(255) unique
);
insert into table_of_values (value)
select 'PHP' union all
select 'CSS' union all
. . . ;
Then you can validate using a foreign key. Say:
create table PeopleSkills (
PeopleSkillsId int auto_increment primary key,
PeopleId int,
Skill varchar(255),
constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
constraint fk_PeopleSkills_Skills foreign key (Skill) references skills(skill)
);
Note: This example uses strings for the foreign key reference. I don't actually advocate that approach -- but that is the direct answer to your question. In practice, the constraint can use the id rather than the name:
create table PeopleSkills (
PeopleSkillsId int auto_increment primary key,
PeopleId int,
SkillId int,
constraint fk_PeopleSkills_PeopleId foreign key (PeopleId) references People(PeopleId),
constraint fk_PeopleSkills_SkillId foreign key (SkillId) references Skills(skillId)
);
I tried to find some solution but I couldn't.
Let's supose we have the table bellow and each row of this table needs to be assigned only to one FK (columnfk1 or columnfk2) (Doesn't make sense be assigned to both OR none):
CREATE TABLE example(
id INT UNIQUE AUTO_INCREMENT
,name VARCHAR(255)
,columnfk1 INT
,columnfk2 INT
,FOREIGN KEY (columnfk1) REFERENCES example1(columnfk1)
,FOREIGN KEY (columnfk2) REFERENCES example2(columnfk2)
);
Is there some rule to warrant that each row will have one FK assigned?
I'm using MYSQL 5
Unfortunately, MySQL doesn't support the check constraint, because this can easily be handled using such a constraint:
check (columnfk1 is null or columnfk2 is null);
So, the only way that you can implement this constraint in the database with this data structure is to use a trigger.
Let's say I have a table with the following fields:
primaryEmail | secondaryEmail
I know how to create a UNIQUE constraint on one of the fields, and I know how to create a UNIQUE constraint that combines both fields, but is there a way to ensure that no value exists twice, ACROSS both columns? For example, if there's a value of joe#example.com in the primaryEmail field, I don't want it to be able to appear again in either the primaryEmail field OR the secondaryEmail field.
You might consider revising your data model and pulling the email address to another table and then relating the new and old tables together. Off the top of my head, something like this should work
create table master (
id int not null primary key,
name varchar(64)
);
create table email (
id int not null primary key,
address varchar(128) not null unique,
parent_id int not null,
type enum('prim', 'secd'),
foreign key (parent_id) references master(id)
on delete cascade,
unique (parent_id, type)
);
I don't love this design - I'm not a fan of the enum, for example - but it would solve your uniqueness constraint.
In my opinion, you would want to put two separate constraints on that field if that is really what you are trying to accomplish. What you are actually trying to do are two different things (make sure that column is unique within the record, and make sure that column within that row is also unique for the whole table).