This question already has answers here:
Prevent duplicate values in database - mysql
(2 answers)
Closed 4 months ago.
Long story short,
I'm trying to create a table, where you can enter a company name. However you cannot enter a company name which contains a string value of a previous entered company name. Is this possible to do with a check constraint?
For example:
My company "CrazyJello" is inserted. Now the following companies that are being entered to MySQL cannot have the string "Crazy" in them.
CREATE table company(
name VARCHAR (100)
CHECK (name != ?????)
No, in sql there is the unique index, but it compares the whole content of the field https://www.mysqltutorial.org/mysql-unique/
Related
This question already has answers here:
MYSQL - add new column with default string concat with data from existing column
(2 answers)
String concatenation in MySQL
(5 answers)
Closed 11 months ago.
For Example
I have the following Columns in a DB-table: Name, Surname, Adress, Contact.
In one row the value for the column name is Smith , the surname Bob and the adress Street1.
How can i combine the mentioned values to the column Contact?
The Value in this row should be like this then:
Bob_Smith_Street1
I want this to be automatically created in the table. so as soon as I right click on the Table -->select 1000 rows, the value is displayed. Without having me to enter the mentioned select command. Is this possible? Can anything be done when creating the table for this. So for example create table employees (Contact(name""surname""address)).
thanks
ps. sorry for my bad english:)
Use CONCAT(). For example:
select concat(Name, '_', Surname, '_', Adress) as contact from t
This question already has answers here:
Delete sql rows where IDs do not have a match from another table
(4 answers)
Closed 1 year ago.
I have a MySQL database where i want to clean up a table but i do not know how to compare against other table entrys and hope someone can help.
One table in the database is called "stats". in the table "Stats" there is a row called "Page_title" which holds an ID number. The same ID number should exist in the table "url,id" otherwise that entry from "stats" should be removed, how can this be done?
Please see this image for reference.
You can do this with a sub select:
delete from stats where page_title not in (select id from shorturl)
This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 5 years ago.
Table1
Id, name, addresses
101,raja ,chennai
Table2
Id,group,name
101,a,siva
102,b,selva
I want retrieve data from two tables like
Table2.group=a and two table Id must equal then take address from table1 display
Id, name, Address, group
What have you tried so far? Have you taken a look at some of the resources here?
Since this is the type of pretty straightforward SQL query you're likely to do many times over, it's worth you reading the tutorials and learning it yourself. I'll give you a few pointers to begin:
Your two tables are connected by the Id field (most likely, this is the primary key for one table and the foreign key for another). You'll need to JOIN these two tables using that field.
You'll need to specify a WHERE clause to filter for your condition that Table2.group = a.
The fields you want to display can all be specified using SELECT. If you are selecting data FROM Table 1 and joining to Table 2, you won't need to specify the table name before the field name.
This question already has answers here:
How to store phone numbers on MySQL databases? [duplicate]
(11 answers)
Closed 5 years ago.
I want to add a column in Customers table. The name of the column which I want to add, is Phone_no. Now if I want to add the country code along with the Phone_no field , then what would be the data type of Phone_no ? e.g. the Phone_no is +918884560909 , Then what would be the data type of Phone_no field?
If you want to add the number with special symbol like +, then use varchar for it like:
varchar(20)
If you prefer one field name it as varchar(expected_size). If you prefer two fields you can name one field as country code (varchar) and other as phone_no(number). Go through Oracle documentation for all available datatypes.
ALTER TABLE Customers ADD COLUMN Phone-no varchar(13);
You could just store number without +. In that case you could store number as bigint
This question already has answers here:
Generate next Id as per the max id in database using Java
(2 answers)
Closed 8 years ago.
I am working on a web application. It will have multiple users. I am using mysql as database.
In my application i am fetching the latest id (from the database, using max(id)) , and then generating the next id for the new registration. This approach is incorrect, as the id might change between the time i update an id
How should i proceed?
LAST_INSERT_ID() or AUTO_INCREMENT will be your friends
LAST_INSERT_ID() :
INSERT INTO table (id, field) VALUES (LAST_INSERT_ID() + 1, "Hello")
AUTO_INCREMENT :
INSERT INTO table (field) VALUES ("Hello")
And the id is auto incrementing
Some documentation here :
https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
https://dev.mysql.com/doc/refman/5.0/fr/example-auto-increment.html