I want to add the following check constraints to the table named "service", to the column "status"
CREATE TABLE Service(
Service_ID INT(5) PRIMARY KEY NOT NULL,
Invoice_ID INT(5),
Project_ID INT(5) NOT NULL,
Description CHAR(20) NOT NULL,
Start_Date VARCHAR (10) NOT NULL,
Due_Date VARCHAR (10) NOT NULL,
Planned_Price VARCHAR (10),
Actual_Price VARCHAR (10),
Status CHAR (10) NOT NULL,
Date_Completed VARCHAR (10)
);
The content/sample data of "status" column will be 1 from the following.
planned, active, completed
*if active or completed: DueDate, StartDate and PlannedPrice are all not null
* if completed: DateCompleted and ActualPrice are both not null
I didn't test it,
however this is the first one
ALTER TABLE Service WITH NOCHECK
ADD CONSTRAINT CK_serice_one
CHECK (
(Status IN ('active','completed')
AND DueDate IS NOT NULL
AND StartDate IS NOT NULL
AND PlannedPrice IS NOT NULL)
OR Status NOT IN ('active','completed')
);
and this is the second
ALTER TABLE Service WITH NOCHECK
ADD CONSTRAINT CK_serice_two
CHECK (
(Status LIKE 'completed'
AND DateCompleted IS NOT NULL
AND ActualPrice IS NOT NULL)
OR Status NOT LIKE 'completed'
);
This helps
Status CHAR(10) NOT NULL CHECK (Status IN ('planned', 'active', 'completed'))
(Status like 'active' and DueDate NOT NULL AND StartDate not NULL)
Related
I tried it with SQL Server and it was working by changing auto_increment into identity, but when it comes to MySQL, I keep getting an error:
create database Students
create table midTBSurname (
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
insert into midTBSurname
(**name**,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
1 errors were found during analysis.
Unrecognized statement type. (near "name" at position 305)
Here is the version in SQL Server
create database Students
create table midTBSurname (
id int not null primary key IDENTITY,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
Insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) values
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
This the table in SQL Server:
- id name Sex birthdate address contactno Course Yearlevel
- 1 Ruskin M 2002-08-10 Mandaluyong 422-5267 BSMATH 4
- 2 Tristan M 2001-07-23 Quezon 913-6791 BSN 1
- 3 Therese F 1998-06-19 Quezon 422-5267 BSIT 2
In mysql EVERY statement must be terminated with a semi-colon
drop table if exists midTBSurname;
create table midTBSurname(
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null);
insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1');
Select * from midTBSurname;
For example
why does MySQL give me #1063 - Wrong column specification for column 'Date' error if I want to import this query? What I need to have in that row is Date with format: YYYY/MM/DD. Can someone help me and explain whats wrong and why this is wrong so I can learn :) Thank you!
CREATE TABLE Employee (
Employee_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Lastname VARCHAR (255) NOT NULL,
Firstname VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL,
Skilllevel TINYINT NOT NULL,
Salary DECIMAL NOT NULL
);
CREATE TABLE Direct_hours (
Date DATE AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Project_number INT NOT NULL REFERENCES Project (Project_number)
);
CREATE TABLE Indirect_hours (
Date DATE NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Type VARCHAR (255) NOT NULL,
Number INT NOT NULL
);
CREATE TABLE Customer (
Customer_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Client_name VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL
);
CREATE TABLE Skilllevel (
Levels TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Description VARCHAR (255) NOT NULL,
Hourly_rate TINYINT NOT NULL
);
CREATE TABLE Project (
Project_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Customer_number INT NOT NULL REFERENCES Customer (Customer_number),
Hourly_rate TINYINT NOT NULL
);
> CREATE TABLE If NOT EXISTS HARDWARE
(
'RECORD_ID' int PRIMARY KEY NOT NULL,
'Asset_Number' int(10) unique not null index,
'Type' char(20) not null,
'Name' varchar(50) not null index,
'Device Description' varchar(50) not null,
'Building' char(40) not null index,
'Date Created' date DEFAULT CURDATE() not null,
'Serial Number' varchar(50) index not null,
'Owner' char(50) index,
'User Created' CHAR ON CREATE USER() INDEX,
'Date Modified' date ON UPDATE CURDATE(),
'Purchase Year' int(4),
'Model' char(50),
'User Modified' CHAR(50),
'PO Number' int(20),
'Location' VARCHAR(50) index,
'OS' VARCHAR(20),
'RAM' int(4),
'Notes' text,
'Verified_By_t' char(50) on update user(),
'Internal IP Address' ON UPDATE USER(),
'Verified_Date_d' date ON UPDATE CURDATE(),
'Ethernet MAC' VARCHAR(20) index,
'Wireless MAC' VARCHAR(20) index,
'Usage' VARCHAR(20),
'Funding Source' VARCHAR(20),
'Lock Combo' int(10),
'ServiceLog::Date Created' VARCHAR(20),
'ServiceLog::Issue' VARCHAR(20),
'Ext. Ethernet Adapter' VARCHAR(20),
'AD' bit,
'Updated' date on update CURDATE(),
'Asset LI Hardware::ID Link' VARCHAR(20),
'Hardware' VARCHAR(20),
'Hardware Asset LI::Description' VARCHAR(20),
'Hardware Asset LI::Location' VARCHAR(20),
'Hardware Asset LI::Type' VARCHAR(20),
'Main Menu::Company' VARCHAR(20)
)
and the error I am getting is
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''RECORD_ID' int PRIMARY KEY NOT NULL, 'Asset_Number' int unique not null index, ' at line 3
I have tried varying this with:
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
CREATE TABLE If NOT EXISTS HARDWARE
(
Asset_Number int unique not null index,
and other variations.
This is what I currently have and instead of one error it shows 5 errors.
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
Type char(20) not null,
Name varchar(50) not null index,
Device_Description varchar(50) not null,
Building char(40) not null index,
Date_Created date DEFAULT CURDATE() not null,
Serial_Number varchar(50) index not null,
Owner char(50) index,
User_Created CHAR ON CREATE USER() INDEX,
Date_Modified date ON UPDATE CURDATE(),
Purchase_Year int(4),
Model char(50),
User_Modified CHAR(50),
PO_Number int(20),
Location VARCHAR(50) index,
OS VARCHAR(20),
RAM int(4),
Notes text,
Verified_By_t char(50) on update user(),
Internal_IP_Address ON UPDATE USER(),
Verified_Date_d date ON UPDATE CURDATE(),
Ethernet_MAC VARCHAR(20) index,
Wireless_MAC VARCHAR(20) index,
Usage VARCHAR(20),
Funding_Source VARCHAR(20),
Lock_Combo int(10),
AD bit,
Updated_date on update CURDATE()
)
and the error I see for it:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index, Type char(20) not null, Name varchar(50) not null index, Device_Descripti' at line 4
You have numerous errors in your code. Here are some that I've spotted:
The index should be a separate line, not inline.
MySQL supports ON UPDATE DEFAULT CURRENT_TIMESTAMP . . . for one column and that column should be a datetime.
There is no ON UPDATE USER().
The IP Address does not have a type.
There is no ON CREATE.
(Note: Perhaps the most recent version of MySQL does support some of these options, but they don't look familiar to me.)
I would suggest that you create the table starting with the first column -- say by commenting out all the other columns. Then, add one column at a time and fix the errors as you encounter them.
The following recordset in Dreamweaver throws a 1052 ambiguous error every time I attempt to test it. I know it has something to do with the dateADDED, but don't know how to fix it.
SELECT commentID, commentTitle, commentContent, topicTable.topicTitle, DAYNAME(dateADDED) as day, MONTHNAME(dateADDED) as month,
DAY(dateADDED) as date, YEAR(dateADDED) as year
FROM commentTable, topicTable
WHERE commentID = colname AND topicTable.topidID = commentTable.topicID
Here is the layout of the tables,
CREATE TABLE userTable
(
userID VARCHAR(15) NOT NULL,
screenName VARCHAR(15) NOT NULL UNIQUE,
userPasswd CHAR(40) NOT NULL,
firstName VARCHAR(15) NOT NULL,
lastName VARCHAR(25) NOT NULL,
dateJoined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastlogin DATETIME,
PRIMARY KEY(userID)
)
;
CREATE TABLE categoryTable
(
categoryID MEDIUMINT AUTO_INCREMENT NOT NULL,
categoryName VARCHAR(30) NOT NULL,
categoryDescription VARCHAR(200) NOT NULL,
PRIMARY KEY (categoryID)
)
;
CREATE TABLE topicTable
(
topicID MEDIUMINT AUTO_INCREMENT NOT NULL,
topicTitle VARCHAR(30) NOT NULL,
userID VARCHAR(15) NOT NULL,
dateAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
categoryID MEDIUMINT NOT NULL,
PRIMARY KEY (topicID)
)
;
CREATE TABLE commentTable
(
commentID MEDIUMINT AUTO_INCREMENT NOT NULL,
commentTitle VARCHAR(30) NOT NULL,
commentContent TEXT NOT NULL,
userID VARCHAR(15) NOT NULL,
dateAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
topicID INT NOT NULL,
PRIMARY KEY (commentID)
)
;
dateADDED is represented in both tables, so you should choose from which one you want it to be in the result set :
SELECT ct.commentID
, ct.commentTitle
, ct.commentContent
, tt.topicTitle
, DAYNAME(ct.dateADDED) as `day`
, MONTHNAME(ct.dateADDED) as `month`
, DAY(ct.dateADDED) as `date`
, YEAR(ct.dateADDED) as `year`
FROM commentTable ct
JOIN topicTable tt ON ct.commentID = tt.colname AND tt.topidID = ct.topicID
By the way, still wonder, what colname stands for, maybe it should be ct.userID = tt.userID instead?
I use MySQL Server 5.5; MySQL Workbench 5.2 CE; WinXp SP3;
I created 2 tables:
CREATE TABLE IF NOT EXISTS `mydb`.`Address` (
`AddressID` INT NOT NULL AUTO_INCREMENT ,
`Country` VARCHAR(45) NOT NULL ,
`City` VARCHAR(45) NOT NULL ,
`Region` VARCHAR(45) NOT NULL ,
`PostalCode` VARCHAR(12) NOT NULL ,
`Address1` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`AddressID`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`Customers` (
`CustomerID` INT NOT NULL AUTO_INCREMENT ,
`FirstName` VARCHAR(45) NOT NULL ,
`LastName` VARCHAR(45) NOT NULL ,
`Email` VARCHAR(500) NOT NULL ,
`Password` VARCHAR(500) NOT NULL ,
`RegistrationDate` TIMESTAMP NULL ,
`CustomerCellPhone` VARCHAR(20) NULL ,
`AddressID` INT NULL ,
PRIMARY KEY (`CustomerID`) ,
INDEX `AddressID_idx` (`AddressID` ASC) ,
UNIQUE INDEX `Email_UNIQUE` (`Email` ASC) ,
CONSTRAINT `CustomerAddressID`
FOREIGN KEY (`AddressID` )
REFERENCES `mydb`.`Address` (`AddressID` )
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
Basically, the table ‘Customers’ contains foreign key constraint and it doesn't work.
Theoretically, if I insert the data into two tables, I won’t delete ‘Address’ because ‘Customers’ depends on it:
insert into Address SET Country = 'Zimbabwe',
City = 'Harare',
Region = 'Mashonaland East Province',
PostalCode = '777',
Address1 = 'square 777 - 777';
insert into Customers SET FirstName = 'John',
LastName ='Doe',
Email = 'john#doe.com',
Password = '12345',
RegistrationDate = now(),
CustomerCellPhone = 123456789,
AddressID = 1;
'Customers'
CustomerID FirstName AddressID
1 John 1
'Address'
AddressID Country City
1 Zimbabwe Harare
But, I can delete the address of my customer by:
DELETE FROM Address WHERE AddressID=1;
the table 'Customers' refer to nothing (dangling pointer)
CustomerID FirstName AddressID
1 John 1
the empty table 'Address'
AddressID Country City
--- --- ---
The problem is ‘Customers’ refer to NULL into ‘Address’, because ‘Address’.AddressID = 1 doesn’t exist. It doesn't give me any errors. What I can do to repair this bug?
Phew, restart the MySQL Server helps me.