Can anyone please help me with writing a query to add constraint on BestFigure column with "%/%" i.e. it should be 3/10 format.
Please refer the image.
create table Player(
Player_No int Identity(1,1) Primary Key, Player_Name Varchar(20) Not null,
Category Varchar(20) check (Category='batsman' or Category='bowler' or Category='Allrounder'),
BestFigure Varchar(10) check (Bestfigure like'%/%'))
I think a better solution is to store the two figures separately and then combine them:
BestFigure_left int,
BestFigure_right int,
BestFigure varchar(10) generated always as (concat(BestFigure_Left, '/', BestFigure_Right))
MySQL does not actually enforce check constraints (which is why a trigger is needed). If it did, you would do:
BestFigure Varchar(10) check (Bestfigure regexp '^[0-9]{1,3}/[0-9]{1,6}$')
Or something like that. I am unclear what the "3" and "10" mean in your description.
Related
I need advice in creating tables where there would be different fields based on a condition. I'm pretty new to psql, so I don't really know if I'm going the right way and would appreciate any tips / advice!
Currently I have a table to represent a meeting_note, which can either be a voice recording OR a text.
When the meeting note is of type text, it must have a meeting_content, and can have an optional meeting_summary. audio_source should be null.
When the meeting note is of type audio, it must have an audio_source and the fields meeting_content and meeting_summary should be null.
I was also thinking of creating two tables - one for type audio and another for text, but there is a unique constraint on created_at which represents a date like May 11th. I wasn't sure how to add this constraint between two tables.
Here are the fields for the table meeting_note
id serial PRIMARY KEY,
meeting_id integer REFERENCES meeting(id),
meeting_note_type enum('audio', 'text') NOT NULL,
meeting_content text,
summary varchar(255),
created_at varchar(10) NOT NULL,
recording_source varchar(255)
and the constraints:
UNIQUE (to_char(created_at, 'YYYY-MM-DD')),
CHECK (NOT (meeting_note_type = 'text' AND meeting_content IS NULL)),
CHECK (NOT (meeting_note_type = 'audio' AND audio_source IS NULL)),
CHECK (NOT (meeting_content IS NULL AND audio_source IS NULL),
CHECK (NOT (meeting_content IS NOT NULL AND audio_source IS NOT NULL),
CHECK (NOT (audio_source IS NOT NULL AND summary IS NOT NULL))
Appreciate any help on this. Thank you so much in advance!
There are two common approaches to this problem - using a table-per-type and using one table for everything. The approach you describe in the question is one table for everything; your definition is pretty accurate.
Here is how to do a table-per-type solution: make a "master" table for all notes, and then a table for each note sub-type, like this:
create table note_master(
id serial PRIMARY KEY,
meeting_id integer REFERENCES meeting(id),
created_at varchar(10) NOT NULL
)
create table note_text (
id serial REFERENCES note_master(id),
meeting_content text,
summary varchar(255),
)
create table note_audio (
id serial REFERENCES note_master(id),
recording_source varchar(255)
)
To query for everything you do left-outer joins to note_text and note_audio. This approach lets you skip the enum because you can always figure out what kind of note it is by examining the results of the join.
I have the following table
create table consultas(
id_consultas int(3) primary key auto_increment,
id_med int(3) not null,
num_cli int(3) not null,
id_tipo int(3) not null,
id_sala int(3) not null,
data_inicio datetime not null,
date_fim datetime,
id_promocao
)
The idea for this table is to save the clinic records of appointments ("consulta")
And i wanted to make a Check Constraint.
So that every time i try to add a new entry it checks if there is already a "consulta" for that client and at that time (data_inicio).
My problem is how do I represent those values in the Check Constraint.
I think it should look something like this
CONSTRAINT CHK_constraint1 CHECK ((SELECT COUNT(*) FROM consultas WHERE data_inicio = ??? AND num_cli = ?) = 0)
Is this possible to achieve or does it have to be the software using the database that makes this check.
If you have any tips please tell me since i don't have much experience with databases.
What you need is a unique index or unique constraint on num_cli and data_inicio. A check constraint is used to impose restrictions in cell values (e.g., ensure a number is multiple of 5 or force a string to be uppercase).
Beware, though, that while the index itself is not a bad idea I won't necessarily solve your problems because you can't possibly assign appointments at 12:00:00 and 12:00:01.
I have the next code of an sql query, but i need to adapt it to mysql query, but it gives me an error the the value "number", and i don't know how to change it correctly, if someone knows how can this code work i will appreciate you to answer
Create table SalarioBase(
IdSalario number constraint pk_salariobase primary key,
Salario number)
Change your code to this
Create table SalarioBase(
IdSalario int constraint pk_salariobase primary key,
Salario int)
You can change int to float ... etc. if you need.
Follows T-SQL92,number is not a basic data type
As far as I know, also your code does not work on ms-sql
you may try:
CREATE TABLE `SalarioBase` (
`IdSalario` INT NOT NULL COMMENT '',
`Salario` INT NULL COMMENT '',
PRIMARY KEY (`IdSalario`) COMMENT '');
I have got the following JSON file:
"vehicle_number" : 91,
"pit_stops" : [ {
"pit_in_elapsed_time" : 1874.0926,
"pit_out_elapsed_time" : 0.0
}, {
"pit_in_elapsed_time" : 1992.9723,
"pit_out_elapsed_time" : 0.0
}, {
"pit_in_elapsed_time" : 2862.2129,
"pit_out_elapsed_time" : 0.0
} ],
My table has to keep the following value:
vehicle
pit_int_elapsed_time
pit_out_elapse_time
How do I create a table based on this??
create table pitstop (
vehicle varchar(50) not null,
inTime varchar(50) not null,
outTime varchar(50) not null,
constraint pk_id primary key(inTime, outTime))
I am not sure if this would be the ideal way of create the table?
Regards
EDIT
I have been thinking of creating 2 main tables. One for the vehicles (vehicleID as pk, pitstopFK as foreign key).
create table vehicles (
vehicle varchar(50) primary key not null
pitstops_fk int not null );
Also the pitstops table:
create table pitstops (
id int primary key autoincrement not null,
inTime varchar(50) not null,
outTime varchar(50) not null,
constraint u_time UNIQUE (inTime, outTime))
vehicles ----- pitstops ( 1 to many)
It's hard to say much without a better understanding of the problem you're trying to solve, but I would say:
if vehicle ID is always an integer, an unsigned int will generally perform better than treating it as text
using a SQL date type for the inTime and outTime columns will allow you to use SQL time/date operations. If the timestamps represent real-world times consider DATETIME or TIMESTAMP, if they are elapsed seconds since the start of a race or something like that, a floating point numeric type would probably be a better choice.
the combination of inTime and outTime doesn't make sense as a primary key, since as you note in the comment it is not necessarily unique. In thinking about designating a primary key, don't (just) think about uniqueness, but about how you'd want to refer to a pitstop event from other tables. In the case I would probably suggest a synthetic auto-incremented unsigned int as a primary key.
First of all, if it is a time you want to store in your database i wouldn't use varchar. Instead try to use Float or Real.
The next thing is, that you should take your vehicle in your primary key as well.
I also would recommend to introduce a ID to your Database.
Something like this:
CREATE TABLE pitstop (
ID int NOT NULL AUTO_INCREMENT,
vehicle varchar(50) NOT NULL,
inTime float NOT NULL,
outTime float NOT NULL,
constraint pk_id primary key(ID, vehicle))
I have a table in SQL Server 2008 like this:
Column DataType
-----------------------
FID numeric(18, 0)
Name varchar(50)
DOB datetime
MobileNo numeric(18, 0)
EmailId varchar(50)
add1 varchar(50)
add2 varchar(50)
add3 varchar(50)
Pincode numeric(18, 0)
UpdateDate datetime
In this table FID should be changed to varchar(50) without dropping (deleting) the table..
How to do this and also FID is set as primary key to the above table and also as a foreign key to other tables
As I said - I would leave the column FID as it is - otherwise, you'll have a ripple effect throughout your entire data model, and this is really really not what you want to do.
What you can do is this: create a new, separate column that contains that new prefix that the "hgiher ups" are so desperate for, and concatenates this with the FID value - but it's a separate column, and you don't need to change any of the references at all.
ALTER TABLE dbo.YourTable
ADD FID2 AS 'MU' + CAST(FID AS VARCHAR(20)) PERSISTED
With this, you'll get a VARCHAR typed new column called FID2 which will have values like MU1, MU2 ..... and so on - automatically, no changes necessary, no messy table re-creations and data copying.... it just works!
Tools->Options->designers->(Unmark)Prevent saving changes that require table recreation
Right click table -> Design -> Change the numeric(18, 0) to varchar(50) -> Save
It will ask for confirmation since changing from one data type to another removes certain precision values.
The best practice is to keep Primary and foriegn keys unique numbers. But you may opt for unique code values too which are varchar values
And one more thing, changing the primary key field datatype automatically changes the datatype of all relational foreign key fields in other tables too.