How to create a calculated attribute in PostgreSQL - mysql

i have a table in sql "empleado", from which, one of the attributes I want it be calculated.
This is the code:
create table empleado (
dni char(9) not null,
contrasena varchar (20) not null,
nombre varchar (30) not null,
apellidos varchar (60) not null,
direccion varchar (80) not null,
telefono char (9) not null,
tipo varchar(30) not null,
fechaIngreso date not null,
antiguedad Integer as getdate()-fechaIngreso,
salario real not null check (salario >0),
primary key (dni)
);
The attribute antiguedad must be the actual date - "fechaIngreso" in days. How could I solve this problem?

Your syntax suggests that you are using SQL Server, not MySQL. You can do what you want with a computed column in SQL Server:
create table empleado (
dni char(9) not null,
contrasena varchar (20) not null,
nombre varchar (30) not null,
apellidos varchar (60) not null,
direccion varchar (80) not null,
telefono char (9) not null,
tipo varchar(30) not null,
fechaIngreso date not null,
antiguedad as (cast(getdate() - fechaIngreso as int)),
salario real not null check (salario > 0),
primary key (dni)
);
In MySQL, you would use a view to do the same thing.

create table empleado (
dni char(9) not null,
contrasena varchar (20) not null,
nombre varchar (30) not null,
apellidos varchar (60) not null,
direccion varchar (80) not null,
telefono char (9) not null,
tipo varchar(30) not null,
fechaIngreso date not null,
antiguedad Integer as DATEDIFF(dd,fechaIngreso, getdate())
salario real not null check (salario >0),
primary key (dni)
);

Related

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

Getting unknown errors in MySQL

create table Member_ID(
Member_ID not null,
Title varchar(4) not null,
Forename varchar(30) not null,
Surname varchar(30) not null,
DOB date not null,
Address 1 varchar (30), not null,
Address 2 varchar (30), not null,
Postcode varchar(8), not null,
MobileNo char (11), not null,
Email varchar (30), not null,
Gender char (1), not null,
Medical varchar (30), not null,
Joining_Date date not null,
Paid varchar (4), not null,
Membership_Type char(1), not null,
Staff_Initials char (2), not null,
Primary key (Member_ID) );
create table class(
Class_Name varchar (30) not null,
ClassDayofWeek date not null,
Class_Time select get date not null,
Class_Duration char(4) not null,
Studio_ID char (4) not null,
Instructor_ID int not null,
create table class_list(
Class_ID varchar (30) not null,
Member_ID not null,
Date_Booked date not null,
create table Instructor(
Instructor_ID not null,
InsFirstName varchar (30) not null,
InsSurname varchar (30) not null,
InsContactNo char (11) not null,
create table Equipment(
Equip_ID not null,
Supplier_ID not null,
Studio_ID not null,
Equip_Name varchar (30),
create table supplier(
Supplier_ID not null,
Supplier_Name varchar (30) not null,
SupplierContactNo char (11) not null,
Supplier_Email varchar (30) not null,
create table Equipment_Maintanence(
Maintenence_ID not null,
EquipID not null,
Main_date date not null
Maint_ID not null,
Eng_Name varchar (30)
Fault_Desc varchar (200)
Maint_Type varchar (7)
Hi,
I am attempting to create a table in MySQL and am a beginner so please be patient. I am getting syntax error on lines 2, 7, 26, 32. Everything else seems to be error free and unsure. I've spent quite some time figuring out. Any advice thanks.
There are plenty of errors in this sql, for example let's get first table "create table Member_ID(" and its
2nd string:
Member_ID not null - you should add type, for example
int
Member_ID int not null - normal definition for field
7th string
Address 1 varchar (30), not null, - you should use another column name, f.e Address 1 and remove extra comma after varchar (30) and before not null
Address_1 varchar(30) not null - normal definition for field
So I have fixed whole first table and get this code:
create table Member_ID(
Member_ID int not null,
Title varchar(4) not null,
Forename varchar (30) not null,
Surname varchar (30) not null,
DOB date not null,
Address_1 varchar(30) not null,
Address_2 varchar(30) not null,
Postcode varchar(8) not null,
MobileNo char(11) not null,
Email varchar(30) not null,
Gender char(1) not null,
Medical varchar(30) not null,
Joining_Date date not null,
Paid varchar(4) not null,
Membership_Type char(1) not null,
Staff_Initials char(2) not null,
Primary key (Member_ID)
);
you forget to add the datatype for alot of columns like Member_ID not null it should be Member_ID int not null
also if the column name have space you should be like that
`Address 1` varchar(30) not null
not
Address 1 varchar(30) not null
also Postcode varchar(8), not null, here there are extra , it should be Postcode varchar(8) not null,
and there few , and ); you forget to write
this seem work to me
create table Member_ID(
Member_ID int not null,
Title varchar(4) not null,
Forename varchar(30) not null,
Surname varchar(30) not null,
DOB date not null,
`Address 1` varchar(30) not null,
`Address 2` varchar(30) not null,
Postcode varchar(8) not null,
MobileNo char (11) not null,
Email varchar (30) not null,
Gender char (1) not null,
Medical varchar (30) not null,
Joining_Date date not null,
Paid varchar(4) not null,
Membership_Type char(1) not null,
Staff_Initials char (2) not null,
Primary key (Member_ID)
);
create table class(
Class_Name varchar (30) not null,
ClassDayofWeek date not null,
`Class_Time select get` date not null,
Class_Duration char(4) not null,
Studio_ID char (4) not null,
Instructor_ID int not null
);
create table class_list(
Class_ID varchar(30) not null,
Member_ID int not null,
Date_Booked date not null
);
create table Instructor(
Instructor_ID int not null,
InsFirstName varchar (30) not null,
InsSurname varchar (30) not null,
InsContactNo char (11) not null
);
create table Equipment(
Equip_ID int not null,
Supplier_ID int not null,
Studio_ID int not null,
Equip_Name varchar (30)
);
create table supplier(
Supplier_ID int not null,
Supplier_Name varchar (30) not null,
SupplierContactNo char (11) not null,
Supplier_Email varchar (30) not null
);
create table Equipment_Maintanence(
Maintenence_ID int not null,
EquipID int not null,
Main_date date not null,
Maint_ID int not null,
Eng_Name varchar(30),
Fault_Desc varchar (200),
Maint_Type varchar (7)
);

MySQL Fiddle syntax error when table name contains a space

I'm trying to build a schema for class but I get this:
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 'NULL, Apartment INTEGER NULL, City ' at line 4.
Here is my code:
CREATE TABLE CUSTOMER PIZZA ORDER NUMBER(
Last_Name VARCHAR(25) PRIMARY KEY NOT NULL,
First_Name VARCHAR(25) NOT NULL,
Street VARCHAR NULL,
Apartment INTEGER NULL,
City VARCHAR NULL,
State VARCHAR(2) NOT NULL,
Zip_Code INTEGER(5) NOT NULL,
Home_Phone INTEGER(10) NOT NULL,
Mobile_Phone INTEGER(10) NOT NULL,
Other_Phone INTEGER(10) NOT NULL,
QTY_of_Pizzas INTEGER NOT NULL,
Special_Handling_Notes VARCHAR NOT NULL,
Pizza_ID INTEGER NOT NULL,
Pizza_Name VARCHAR NOT NULL,
Pizza_Description VARCHAR NOT NULL,
Pizza_Size VARCHAR NOT NULL,
Pizza_Price INTEGER NOT NULL,
Pizza_Photo_URL VARCHAR NOT NULL,
Pizza_Order_Timestamp INTEGER NOT NULL,
Pizza_Order_ID INTEGER NOT NULL,
);
CREATE TABLE PIZZA TABLE(
Pizza_ID INTEGER NOT NULL PRIMARY KEY,
Pizza_Name VARCHAR NOT NULL,
Pizza_Description VARCHAR NOT NULL,
Pizza_Size VARCHAR NOT NULL,
Pizza_Price INTEGER NOT NULL,
Pizza_Photo_URL VARCHAR NOT NULL,
);
CREATE TABLE CUSTOMER PIZZA TABLE(
Pizza_Order_ID INTEGER PRIMARY KEY NOT NULL,
Pizza_Order_Timestamp INTEGER NOT NULL,
QTY_of_Pizzas INTEGER NOT NULL,
Special_Notes VARCHAR NOT NULL,
Last_Name VARCHAR(25) NOT NULL,
First_Name VARCHAR(25) NOT NULL,
Street VARCHAR NOT NULL,
Apartment VARCHAR NOT NULL,
City VARCHAR NOT NULL,
State VARCHAR(2) NOT NULL,
ZIP INTEGER(5) NOT NULL,
Home_Phone INTEGER(10) NOT NULL,
Mobile_Phone INTEGER(10) NOT NULL,
Other_Phone INTEGER(10) NOT NULL,
Pizza_ID INTEGER NOT NULL,
);
CREATE TABLE PIZZA TABLE(
Pizza_ID INTEGER PRIMARY KEY NOT NULL,
Pizza_Name VARCHAR NOT NULL,
Pizza_Description VARCHAR NOT NULL,
Pizza_Size VARCHAR NOT NULL,
Pizza_Price INTEGER NOT NULL,
Pizza_Photo_URL VARCHAR NOT NULL,
);
CREATE TABLE CUSTOMER TABLE(
Customer_ID INTEGER PRIMARY KEY NOT NULL.
Last_Name VARCHAR NOT NULL,
First_Name VARCHAR NOT NULL,
Street VARCHAR NULL
Apartment INTEGER NULL,
City VARCHAR NULL,
State VARCHAR(2) NOT NULL,
ZIP INTEGER(5) NOT NULL,
Home_Phone INTEGER(10) NOT NULL,
Mobile_Phone INTEGER(10) NOT NULL,
Other_Phone INTEGER(10) NOT NULL,
);
CREATE TABLE PIZZA ORDER TABLE(
Pizza_Order_ID INTEGER PRIMARY KEY NOT NULL,
Pizza_ID INTEGER NOT NULL,
Customer_ID INTEGER NOT NULL,
QTY_of_Pizzas INTEGER NOT NULL,
Special_Notes VARCHAR NOT NULL,
Pizza_Order_Timestamp INTEGER NOT NULL,
);
There are two tables have the same name.
Some "," using "." to spilt.
Missing "," to spilt
Table name contain space?
I think if you fix the above problem, it would be fine.

Cannot Create Table

I am using this SQL query to create a table but does gives error in my REFRENCES AT line 13. This is my query
CREATE TABLE ITEM (
ID INT NOT NULL,
TYPE VARCHAR (32) NOT NULL,
DESCRIPTION VARCHAR (64) NOT NULL,
SIZE FLOAT NOT NULL,
SIZE_TYPE VARCHAR (4) NOT NULL,
MANUFACTURE VARCHAR (16) NOT NULL,
SECTION VARCHAR (16) NOT NULL,
PRICE FLOAT NOT NULL,
LEVEL INT(1) NOT NULL,
AISLE_ID INT(11) NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (AISLE_ID),
REFERENCES aisle(AISLE_ID)
);
CREATE TABLE ITEM (
ID INT NOT NULL,
TYPE VARCHAR (32) NOT NULL,
DESCRIPTION VARCHAR (64) NOT NULL,
SIZE FLOAT NOT NULL,
SIZE_TYPE VARCHAR (4) NOT NULL,
MANUFACTURE VARCHAR (16) NOT NULL,
SECTION VARCHAR (16) NOT NULL,
PRICE FLOAT NOT NULL,
LEVEL INT NOT NULL,
AISLE_ID INT NOT NULL
PRIMARY KEY (ID) ,
FOREIGN KEY (AISLE_ID) REFERENCES aisle(AISLE_ID)
)
FOREIGN KEY (AISLE_ID),
REFERENCES aisle(AISLE_ID)
Is wrong and should be:
FOREIGN KEY (AISLE_ID) REFERENCES aisle(AISLE_ID)

Having trouble in Online Movie ticketing database design

#J2D8T i have done few changes in my code please tell if its correct also is it properly normalized? thanks in advance.
use [fadi1]
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
Actors varchar (100) not null,
);
create table Venue(
V_id int primary key not null,
V_Name varchar(25) not null,
Maxcapacity int not null,
);
create table Shows (
showid int primary key IDENTITY(1,1) not null,
V_id int not null,
M_id int not null ,
date_time datetime not null unique,
remaining_tickets int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
);
create table Tickets(
showid int not null,
Seat_no int not null, -- (maxcap-remaining tickets)+1 from capacity table of given pk of that table
T_id int primary key IDENTITY(1,1) not null, --(p.k)
price int not null,
foreign key (showid) references Shows(showid)
);
create table Booking (
showid int not null,
T_id int not null unique,
Cnic varchar(17) not null,
booking_time datetime not null,
foreign key (showid) references Shows(showid),
foreign key (T_id) references Tickets(T_id),
foreign key (Cnic) references Customer(Cnic),
constraint pk_IDBooking primary key(T_id,showid),
);
please inform me as soon as possible because i am not quite far from the deadline :D
At a minimum, there is no password data type in SQL Server. Since you have provided no details about how you're storing data, I converted it to varchar(50).
SQLFiddle
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar(50) not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30) ,
)
create table Movie (
M_id int not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null,
primary key (M_id)
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25)not null
);
create table Capacity(
M_id int not null,
v_id int not null,
date_time varchar (15) not null,
remaining_tickets int not null,
max_capacity int not null,
primary key (M_id, v_id ,date_time )
);
create table Booking (
v_id int not null, --key
T_id int not null,
M_id int not null,--key
Cnic varchar(30) primary key not null,--(p.k))
date_time varchar (15) not null, -- (f.k )
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
);
I edited the answer to include only the code with some comments hope this solves your problem.
Edit from your original post:
create table Customer (
Cnic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword password not null,
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Adimn (
Anic varchar (17) primary key not null,
name varchar (30) not null,
Cpassword varchar (30) not null, // changed to varchar
email varchar (40) ,
contactno varchar (15) ,
city varchar (30)
);
create table Movie (
M_id int primary key not null,
name varchar(25) not null,
Mdescription varchar(500) not null,
imagename varchar (100) not null
);
create table Actors(
actor_id varchar(25) primary key not null ,
actor varchar(30)not null,
M_id int not null,
Act_role varchar (10)not null,
amountspent int ,
foreign key (M_id) REFERENCES Movie(M_id)
);
create table Venue(
V_id int primary key not null,
M_id int not null,--F.k
V_Name varchar(25) not null,
foreign key (M_id) references Movie(M_id)
);
create table Capacity(
v_id int not null, // FK
M_id int not null unique, //FK
date_time varchar (15) not null unique,
remaining_tickets int not null,
max_capacity int not null,
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
constraint pk_IDCapacity PRIMARY KEY (M_id,v_id,date_time) // this will create composite key
);
create table Tickets(
Seat_no int primary key not null, -- (p.k))
T_id int not null,
M_id int not null, --(f.k)
price int not null,
foreign key (M_id) references Capacity(M_id)
);
create table Booking (
v_id int not null, //FK
T_id int not null, //FK
M_id int not null,//FK
Cnic varchar(30) primary key not null // Will this stil be neccessary
date_time varchar (15) not null, //FK
foreign key (v_id) references Venue(v_id),
foreign key (M_id) references Movie(M_id),
foreign key (date_time) references Capacity(date_time),
foreign key (T_id) references Tickets(T_id),
constraint pk_IDBooking primary key(v_id,M_id,T_id,date_time) //Can be in any order the PK's
);
Could probably be more compacted with normalization but it should work based on the code you have provided.