Hello stackoverflow people
I have a 2 tables that shares the same attributes, but in different categories. I am trying to list items ONLY with the attribute "Rending":
CREATE VIEW rending AS SELECT ranged_weapons.Name_, ranged_weapons.Dam, ranged_weapons.Dam_Type,
melee_weapons.Name_, melee_weapons.Dam, melee_weapons.Dam_Type
FROM ranged_weapons, melee_weapons
WHERE Dam_Type = 'Rending';
but when i run it, i get:
Error Code: 1052. Column 'Dam_Type' in where clause is ambiguous
What am i doing wrong?
table information
CREATE TABLE Ranged_weapons (
Name_ varchar (40) NOT NULL,
Class varchar (40),
Type_ varchar (40),
Range_ varchar (40),
RoF varchar (40),
Dam varchar (40),
Dam_Type varchar (10),
Pen integer ,
Clip integer ,
Rld varchar (10),
PRIMARY KEY (Name_),
FOREIGN KEY (Name_) REFERENCES Items(Name_) ON DELETE CASCADE );
CREATE TABLE Melee_weapons (
Name_ varchar (40) NOT NULL,
Type_ varchar (40),
Dam varchar (40),
Dam_Type varchar (40),
Pen integer ,
PRIMARY KEY (Name_),
FOREIGN KEY (Name_) REFERENCES Items(Name_) ON DELETE CASCADE );
Thats says it all meaning the column is present in both the tables and you need to explicitly mention which table column you are referring to something as
CREATE VIEW rending AS SELECT *
FROM ranged_weapons, melee_weapons
WHERE ranged_weapons.Dam_Type = 'Rending';
UPDATE : From the given table structures using the above code will show duplicate column name error since lot of column name is same across the tables.
The best thing is to select the columns explicitly for the view using JOIN or if needed to select the same column from the other table then using different alias names. Here is an example of how we can select and create the view using JOIN
CREATE VIEW rending AS
SELECT rw.Name_,rw.Class,rw.Type_,rw.Range_,
rw.RoF,rw.Dam,rw.Dam_Type,rw.Pen ,rw.Clip,rw.Rld
FROM ranged_weapons rw
JOIN melee_weapons mw on mw.Name_ = rw.Name_
WHERE rw.Dam_Type = 'Rending';
If you want to select same columns for the view from different tables this how you can use alias
CREATE VIEW rending AS
SELECT rw.Name_,rw.Class,rw.Type_,rw.Range_,
rw.RoF,rw.Dam,rw.Dam_Type,rw.Pen ,rw.Clip,rw.Rld,
mw.Name_ as mw_Name
FROM ranged_weapons rw
JOIN melee_weapons mw on mw.Name_ = rw.Name_
WHERE rw.Dam_Type = 'Rending';
Here mw.Name_ as mw_Name will refer to the column from melee_weapons and you can specify other column names this way if its needed for the view.
DAM_TYPE is present in both tables that's why where clause unable to recognize it...
You must write table_name with DAM_TYPE in where clause..
Thanks
Related
I have tried all the combinations of LIKE and NOT LIKE but unable to set a CONSTRAINT for mobile number field, as I want to create table using
CREATE TABLE patient (
pname VARCHAR(20) NOT NULL,
mobNo VARCHAR(15),
CONSTRAINT checkMob CHECK(mobNo NOT LIKE '^[^A-Z]%')
);
but getting error while inserting values, and i want numbers only in mobile number field.
i want numbers only in mobile number field
CREATE TABLE patient ( pname VARCHAR(20) NOT NULL,
mobNo VARCHAR(15),
CONSTRAINT checkMob CHECK(mobNo REGEXP '^[0-9]+$')
);
I have a event in which I am storing a string in a variable. Now I want to use that variable to create a new table. Everytime my event runs it creates table with the name of "mon". What is I am doing wrong ?
BEGIN
DECLARE onlyweek INT;
DECLARE mon VARCHAR(20);
SET #mon = "rehan";
CREATE TABLE mon(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
capacity INT NOT NULL
);
END
Because you use mon instead of #mon. And even then it wont work because you need dynamic SQL for that.
But what is even more important:
Don't do that!
Don't create a table on the fly. Table designs should be static. That smells like a big design flaw.
This is a design mistake. For example, you need to make report for the year. In your design you have to join 12 tables and where-s how to join. And this is very slow.
Better design is creating 2 tables - "months" and "reporting_periods" with foreign key to table 'months'. This way when you need year report - you join only 2 tables by ID with "where".
Table 'months' can be filled once a year using same mysql events.
Then use mysql "stored procedure" (and mysql event) for periodic insert into reporting_period with month id. Months` names can include year as "bad way" or have the field 'year' = 'better one'.
CREATE TABLE months(
id int auto_increment,
name varchar(10),
number int not null,
year int not null,
constraint monthes_pk
primary key (id)
);
and reporting_period
CREATE TABLE reporting_period(
id INT auto_increment,
period_id INT NOT NOT,
capacity INT NOT NULL,
constraint `reporting_period_pk`
primary key (id),
constraint `reporting_period__fk`
foreign key (period_id) references months (id)
);
More about DB design: normalization
Hi I have this code and I have been asked for this query: Surname of the actors in alphabetical order, with the titles of the films in which they participated, and their age at
time of participation in film production (conventionally dated 30 June);
I have tried in every way the datediff function but it keeps giving me error, from what I understand does not accept the production year written in that way. How do I set the month and day as a query and then use the datediff? thank you very much for those who will help me, I'm wasting my time for nonsense. Excuse me but I'm still in the beginning
create database cinema;
use cinema;
create table participation(film varchar (3) not null ,actor varchar(5) not null);
create table actor (id_actor varchar(5) not null,name varchar(30), surname varchar(30) not null);
create table film (id_film varchar(3) not null,title varchar(30) not null, kind varchar (30) not null, producer varchar (5) not null, production_year year(4));
create table producer( id_producer varchar (5) not null, name varchar (30) ,surname varchar(30) not null);
alter table producer add primary key (id_producer);
alter table film add primary key (id_film);
alter table participation add primary key (film,actor);
alter table actor add primary key (id_actor);
alter table participation add constraint fk_pfilm foreign key (film) references film(id_film);
alter table participation add constraint fk_pactor foreign key (actor) references actor(id_actor);
alter table film add constraint fk_fproducer foreign key (producer) references producer (id_producer);
insert into producer(id_producer,name,surname) values ("0000A","Steven","Spielberg"),("0000B","Stanley","Kubrick"),("0000C","Ridley","Scott");
insert into actor(id_actor,name,surname) values ("000AA","Sylvester","Stallone"),("000AB","Brad","Pitt"),
("000AC","George","Clooney"),("000AD","Demi","Moore"),("000AE","Bruce","Willis"),
("000AF","Monica","Bellucci");
insert into film(id_film,title,kind,production_year,producer) values ("00A","Jurassic Park","avventura",'2000',"0000A"),("00B","Matrix","fantascienza",'2001',"0000B"),
("00C","Star Wars","fantascienza",'2000',"0000A"),("00D","Indiana Jones","avventura",'2002',"0000B"),("00E","Rambo","avventura",'2002',"0000A"),
("00F","Rocky I","sportivo",'2001',"0000A"),("00G","Rocky II","sportivo",'2004',"0000B");
insert into participation (film,actor) values ("00A","000AA"),("00B","000AB"),("00C","000AC"),("00D","000AD"),("00E","000AA"),
("00F","000AA"),("00G","000AB"),("00A","000AC"),("00B","000AA"),("00C","000AB"),("00D","000AB");
select * from participation;
select * from actor;
select * from film;
select * from producer;
alter table actor add column datebirth date;
update actor set datebirth='1946-07-06' where id_actor="000AA";
update actor set datebirth='1963-12-18' where id_actor="000AB";
update actor set datebirth='1961-05-06' where id_actor="000AC";
update actor set datebirth='1962-11-11' where id_actor="000AD";
update actor set datebirth='1955-03-19' where id_actor="000AE";
update actor set datebirth='1964-09-30' where id_actor="000AF";
create view vista as select * from (actor inner join participation on id_actor=participation.actor);
create view vista2 as select * from (vista inner join film on vista.film=film.id_film);
select surname,title,datediff(datebirth,production_year-06-30) as età from vista2;
Try to replace anno_produzione-06-30 with:
concat(anno_produzione, '-06-30')
The engine can cast ISO date representations to date.
And you shouldn't use double quotes around string literals. Use singles quotes instead, that's their job in SQL.
I don't understand why I'm getting this error when trying to populate this table. There is nothing in the table at the moment so I don't understand why there would be a duplicate...
This is the code I'm using:
INSERT INTO Suppliers
(supp_id,company_name,town,phone)
Values
("ADT217","AdTec","Birmingham","0121-368-1597"),
("CPS533","CPS","Maidenhead","01382-893715"),
("FCL162","ForComp Ltd","Nottingham","01489-133722"),
("KBC355","KBC Computers","Glasgow","0141-321-1497");
suppliers table...
CREATE TABLE suppliers(
supp_id int NOT NULL,
company_name character(15) NOT NULL,
town character(15)
phone character(15)
primary key(supp_id)
);
This occurs when you have a primary key but do not give it an initialization value. The insert itself is causing the duplication.
In your case, two possibilities come to mind:
supp_id is the primary key and declared as a number. In older versions of MySQL, I think the string values get silently converted to numbers. Because the leading characters are letters, the value is 0.
You have another id field that is the primary key, but given no value and not declared auto_increment.
EDIT:
I suspect you want the following code:
CREATE TABLE suppliers (
supplierId int NOT NULL auto_increment primary key,
supp_name varchar(255) unique,
company_name varchar(15) NOT NULL,
town varchar(15),
phone varchar(15)
);
INSERT INTO Suppliers(supp_name, company_name, town, phone)
Values ('ADT217', 'AdTec', 'Birmingham', '0121-368-1597'),
('CPS533', 'CPS', 'Maidenhead', '01382-893715'),
('FCL162', 'ForComp Ltd', 'Nottingham', '01489-133722'),
('KBC355', 'KBC Computers', 'Glasgow', '0141-321-1497');
Some notes:
Usually you want varchar() rather than char(), unless you really like lots of spaces at the end of strings.
I added a unique supplier name to the table and declared the id to be a auto_increment.
Single quotes are ANSI standard for string constants. MySQL (and some other databases) allow double quotes, but there is no reason to not use the standard.
With your table you can get the error like "Incorrect Integer Value", but depending on MySQL server configuration it can do conversion(string->int) automatically for your query string must become "0" as result of this it makes 2 rows with 0 as supp_id and get error Duplicate entry '0' for key 'PRIMARY'. I guess you are using InnoDB as table type, in this case query will run as transaction and it will rollback after first error(for this example it will be second row).
DROP TABLE suppliers; -- Will drop your old table
CREATE TABLE suppliers(
supp_id varchar(30) NULL, -- You can set length as you wish
company_name character(15) NOT NULL,
town character(15),
phone character(15),
primary key(supp_id)
);
INSERT INTO Suppliers
(supp_id,company_name,town,phone)
Values
("ADT217","AdTec","Birmingham","0121-368-1597"),
("CPS533","CPS","Maidenhead","01382-893715"),
("FCL162","ForComp Ltd","Nottingham","01489-133722"),
("KBC355","KBC Computers","Glasgow","0141-321-1497");
After changing type insert will work without problems.
In Wordpress, when we clone the website, the media and user roles are not working. The error is as below:
WordPress database error Duplicate entry '0' for key 'PRIMARY' for query
INSERT INTO `wp_334_actionscheduler_logs`
(`action_id`, `message`, `log_date_gmt`, `log_date_local`)
VALUES (0, 'action complete via WP Cron', '2021-02-17 05:29:40',
'2021-02-17 05:29:40')
made by
do_action_ref_array('action_scheduler_run_queue'),
WP_Hook->do_action,
WP_Hook->apply_filters,
ActionScheduler_QueueRunner->run,
ActionScheduler_QueueRunner->do_batch,
ActionScheduler_Abstract_QueueRunner->process_action,
do_action('action_scheduler_after_execute'),
WP_Hook->do_action,
WP_Hook->apply_filters,
ActionScheduler_Logger->log_completed_action,
ActionScheduler_DBLogger->log
I have 3 columns: name, age, address.
I want name and age to be a composite key to refer to an address.
Create table usertable (
name varchar(100) not null,
age int not null,
address varchar(100) not null,
constraint addresskey PRIMARY KEY(name,age)
);
This works:
select * from usertable where (name,age)=('somename',someage);
But i would like to do something like:
select * from usertable where addresskey=('somename',someage);
I get an error when I do this.
In that way you want to say that you put the primary key for the addresskey.
1) It's not a good idea to put name and age as primary key because there should be some duplicates.
2) You get an error because the query is wrong because you don't have a field named addresskey and in the query you compare a varchar with 2 fields.
So if you want to do that you can add a field addresskey as an auto_increment field (but pay attention to manage the same address, decide how it could works) and then you can select using addresskey value.