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)
Related
I am a beginner in SQL, I created a table called Product and in one of the columns I placed SURNAME instead of DESCRIPTION, I am trying to follow the code below but this syntax error is appearing:
alter table palmsdatabase.product
CHANGE COLUMN SURNAME DESCRIPTION NVARCHAR(500) NULL
Just try this:
ALTER TABLE Product CHANGE `DESCRIPTION` `SURNAME` NVARCHAR(500) NULL;
and check for more information: Alter Table
I have a sql query,
$sql = "select
tbl_user.name,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd
from chat,tbl_user where (chat.to =
'".mysql_real_escape_string($_SESSION['chatuser'])."' AND recd = 0) and
chat.from=tbl_user.user_id order by id ASC";
But i dont know how to create a table from this information. The above is
a select table syntax, but can anyone help me to make the above sql query
to a proper syntax, so that i can create the table.
Example, i need the ab0ve t0 like this
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
You can use the CREATE TABLE new_tbl AS SELECT ...
Read the documentation for MySql:
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
In Below Table, I want full_name column to take Default value as concatenation of values in first_name, middle_name & last_name columns.
SQL Table
CREATE TABLE ms_employee
(
emp_no int unique,
first_name varchar(100) not null,
middle_name varchar(100) not null,
last_name varchar(100) not null,
full_name varchar(100)
)
Code
code below gives error
ALTER TABLE ms_employee ADD CONSTRAINT [cons_1] DEFAULT ([first_name]+' '+[middle_name]+' '+[last_name]) FOR [emp_name]
Error Message
The name "first_name" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
So, it's not working using Constraint!
How to achieve it, using Constraint? OR any other way will also be appreciated !
You can change it to a Computed column.
ALTER TABLE ms_employee DROP COLUMN [full_name];
GO
ALTER TABLE ms_employee ADD [full_name]
AS ([first_name] + ' ' + [middle_name] + ' ' + [last_name])
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.
Is there any gui functionality within mysql workbench where you can view whether a field is set to NULL or NOT NULL?
For example this:
CREATE TABLE Peoples (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR (200) NULL,
last_name VARCHAR (100) NOT NULL,
PRIMARY KEY (id)
);
Is displayed as
Table Peoples
=============
id, first_name, last_name
-------------
id int(11) PK
first_name varchar(200)
last_name varchar(100)
In the Object Information tab. The object information tab does not specify that the first_name is NULL and the last name is NOT NULL.
It seems unfortunate that one would need to create an entire EER model just to see if a column is nullable.. another workaround i found is to right-click on the table and choose 'Alter Table' and the editor that comes up shows the NN column, checked or not (version 5.2.47).
Under "Data Modelling" use "Create EER Model From Existing Database" or open up an existing model of the desired database if you have one. Then select the desired table and the bottom window will show the table's properties. The "Columns" tab will show what columns are NULL by default.