column with value as Concatenate other columns' value - sql-server-2008

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])

Related

How to use an ''if '' statement to make an condition while creating a table in plsql or is it possible to do the same in mysql?

I want that the column could accept only Latin characters, how should I code? What do you suggest?
For example( in MySQL):
Create table employees_info(
employee_id int not null auto_increment,
first_name varchar(30) not null,
primary key(employee_id));
-- here I tried to use a 'check' clause but I guess I could not use it correctly
I want that the column first_name could able to accept only Latin char;

creating composite field in mysql

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)

Copy/duplicate SQL row with blob/text, How do that?

I would like to copy a SQL's row into the same table.
But in my table, I've a 'text' column.
With this SQL:
CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT * FROM produit WHERE pdt_ID = 'IPSUMS';
UPDATE produit2 SET pdt_ID='ID_TEMP';
INSERT INTO produit SELECT * FROM produit2;
DROP TABLE produit2;
I get this error :
#1163 - The used table type doesn't support BLOB/TEXT columns
Here is my table :
pdt_ID varchar(6)
pdt_nom varchar(130)
pdt_stitre varchar(255)
pdt_accroche varchar(255)
pdt_desc text
pdt_img varchar(25)
pdt_pdf varchar(10)
pdt_garantie varchar(80)
edit_ID varchar(7)
scat_ID int(11)
pdt_asso1 char(3)
pdt_asso2 char(3)
pdt_online tinyint(4)
It's possible to help me to duplicate row ? How?
You can't store TEXT-columns (which really are blobs) in memory tables. See here
Depending on your ultimate goal, you may insert a md5-hash of the TEXT-column instead to preserve entity identity. Otherwise you need to put pdt_desc and such into another table and refer to it's primary key - that will save you some storage/memory too.

How to give the long name as a field of the table?

How can I give the long text as a table field name in mysql?
Here is what I tried:
CREATE TABLE IF NOT EXISTS surveyForm_8(
surveyForm_8_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(surveyForm_8_id),
survey_form_id VARCHAR(255),
submitted_by VARCHAR(15),
submitted_on TIMESTAMP,
'How_to_change_the_way_of_road?' VARCHAR(255)
)
But I got this error:
#1059 error
Try this one, you should use the ` symbol for column names
CREATE TABLE IF NOT EXISTS surveyForm_8(surveyForm_8_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(surveyForm_8_id), survey_form_id varchar(255) ,submitted_by varchar(15),
submitted_on timestamp, `How_to_change_the_way_of_road?` varchar(255));
Please see http://dev.mysql.com/doc/refman/5.5/en/identifiers.html for valid table and field names.
Basically, double quotes only work in ANSI_QUOTES mode. The default is to use `backticks` to quote. Also, the maximum length of table / field names is 64 characters.

Alterning a table that has spaces in its name

I want to add 3 columns. There are two things that I dont know, one is how to specify a default value for each column, and then next is how to alter a table that has got spaces:
ALTER TABLE app name and url
ADD COLUMN price VARCHAR(200)
ADD COLUMN type_of_membership VARCHAR(200)
ADD COLUMN special_deal VARCHAR(200)
I get this error:
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 'name and url ADD COLUMN price VARCHAR(200) ADD COLUMN type_of_membership VARCH' at line 1
I guess it is because I have a tables name with spaces.
Is this how you insert default values:
ALTER TABLE app name and url
ADD COLUMN price VARCHAR(200) DEFAULT 'None'
ADD COLUMN type_of_membership VARCHAR(200) DEFAULT 'None'
ADD COLUMN special_deal VARCHAR(200) DEFAULT 'None'
UPDATE
This is what I executed:
ALTER TABLE `app name and url`
ADD COLUMN price VARCHAR(200) DEFAULT 'None',
ADD COLUMN type_of_membership VARCHAR(200) DEFAULT 'None',
ADD COLUMN special_deal VARCHAR(200) DEFAULT 'None';
and this is what I got:
'mydb.app name and url' is not BASE TABLE
The error is because it is a view..I get it now. I will have to modify the view
In Mysql, you need to escape it using backtick (grave accent)
ALTER TABLE `app name and url`
ADD COLUMN price VARCHAR(200)
ADD COLUMN type_of_membership VARCHAR(200)
ADD COLUMN special_deal VARCHAR(200)
What is the meaning of grave accent (AKA backtick) quoted characters in MySQL?
PS: Next time use only alphanumeric in your table and column name.
Use ``
You have to replace the table name with
ALTER TABLE `app name and url`
SQL Fiddle demo