I am pretty new to MYSQL, and have been struggling on getting Concat to work properly. I have tried several different variations found here, but without any luck. Can someone point me in the right direction on getting this to work. I am creating a very basic table and then a view to Concat the first and last name along with displaying the rest of the customer's information.
Create Table Customer(
CustomerId INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR (50),
Address VARCHAR (75),
City VARCHAR (50),
State VARCHAR (50),
Zip NUMERIC,
OrderID INT
);
CREATE VIEW CustomerInformation AS
SELECT FirstName, LastName, CONCAT(FirstName, ‘ ‘, lastname),
Address VARCHAR (75),
City VARCHAR (50),
State VARCHAR (50),
Zip NUMERIC,
FROM Customer
try with this:
SELECT FirstName, LastName, CONCAT(FirstName, ‘ ‘, lastname), Address, City, State, Zip
FROM Customer
Do not add the column data type in the SELECT statement. That's only needed in the CREATE TABLE or ALTER TABLE statements
Related
I create a table in MS Access using the following script:
CREATE TABLE POWERSQL (
ProposalNumber INTEGER PRIMARY KEY,
FirstName CHAR (15),
LastName CHAR (20),
Address CHAR (30),
City CHAR (25),
StateProvince CHAR (2),
PostalCode CHAR (10),
Country CHAR (30),
Phone CHAR (14),
HowKnown CHAR (30),
Proposal CHAR (50),
BusinessOrCharity CHAR (1) );
When insert some value to a CHAR filed which is shorter than the set length, e.g., 'John' in [FirstName], it fills the remaining characters with empty space which messes up with other queries and joins. Trim does not help. Any advice? Many thanks.
CHAR fields are half-supported in MS Access and are fixed-length. This means you can't insert less characters than the full field length, and if you do, Access will fill the unused positions with spaces.
Instead, use VARCHAR for variable-length character fields:
CREATE TABLE POWERSQL (
ProposalNumber INTEGER PRIMARY KEY,
FirstName VARCHAR (15),
etc...
Instead of the fixed length CHAR(x) data type, use TEXT, like:
FirstName TEXT,
.................
The TEXT data type is variable length and can store up to 255 characters
I need to make column 'full_name' as composite column of columns 'first_name' and 'last_name' in MySQL. I can do the same in MS SQL using following syntax :
create table customer
(
first_name varchar(100)
, last_name varchar(100)
, full_name AS first_name + last_name
)
How can I do the same in MySQL. Any suggestions?
MySQL 5.7.5+ supports computed/generated columns (here is a introduction to the subject). So you can do this as:
create table customer (
first_name varchar(100),
last_name varchar(100),
full_name varchar(200) AS (concat_ws(' ', first_name, last_name))
);
Notes: First, I assume you want a space between the names. Second, string concatenation does not use + in MySQL.
In earlier versions of MySQL, you would need to use a view for this.
You can try to update record once it will get inserted into the table:
UPDATE customer SET fullname=CONCAT(first_name,last_name)
I want to create a table in the database and below is the query that I wrote; does anyone have any idea why it doesn't work?
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 'sname varchar(255), city varchar(255), avg int(20), clg# int(20) )' at line 4`
CREATE TABLE stud
(
s# int,
sname varchar(255),
city varchar(255),
avg int(20),
clg# int(20)
);
The # is not a valid character for an identifier. Just remove it:
CREATE TABLE stud (
s int,
sname varchar(255),
city varchar(255),
avg int(20),
clg int(20)
);
You can review the rules for identifiers here. Note you could also put the name in backticks:
`s#` int,
However, I strongly discourage you from using names that need to be escaped.
You cannot use the '#' character in column names.
you cant have # in your table names, change your script to this one and try again.
CREATE TABLE stud ( s int,
sname varchar(255),
city varchar(255),
avg int(20),
clg int(20) );
It's not recommended to use '#' to name your identifiers. This is ambiguous for you and other developers on your team. Use this instead
CREATE TABLE stud (
sNo int,
sname varchar(255),
city varchar(255),
avg int(20),
clgNo int(20)
);
By naming your identifiers descriptively it easier for everyone to understand. However mysql qouted identifier naming rules allow ASCII: U+0001 .. U+007F
see here.
MySQL Schema object naming Rules
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.
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