MySQL Not Null commands aren't doing anything - mysql

I'm just starting SQL and I'm trying to change a tables values from NULL to NOT NULL, except the command prompt is showing that it isn't being changed.
Here's the code I'm putting in:
CREATE TABLE my_contacts
(
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
gender CHAR(1) NOT NULL,
birthday DATE NOT NULL,
profession VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
interests VARCHAR(100) NOT NULL,
seeking VARCHAR(100) NOT NULL
);
I can't post images but when I use DESC my_contacts; the table prints out and shows "Yes" under the null column for all rows.
This is MySQL 5.6

So as you mentioned in your comments. Do as follow:
But remeber one thing. If you create the table and have inserted some data on it and left any of the columns that you want to change to not null you will have to delete the data or put some data on the field that is null.
alter table my_contacts modify last_name VARCHAR(30) NOT NULL;
alter table my_contacts modify first_name VARCHAR(20) NOT NULL;
alter table my_contacts modify email VARCHAR(50) NOT NULL;
alter table my_contacts modify gender CHAR(1) NOT NULL;
alter table my_contacts modify birthday DATE NOT NULL;
alter table my_contacts modify profession VARCHAR(50) NOT NULL;
alter table my_contacts modify location VARCHAR(50) NOT NULL;
alter table my_contacts modify status VARCHAR(20) NOT NULL;
alter table my_contacts modify interests VARCHAR(100) NOT NULL;
alter table my_contacts modify seeking VARCHAR(100) NOT NULl;
Or if it suits better:
drop table my_contacts;
CREATE TABLE my_contacts
(
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
gender CHAR(1) NOT NULL,
birthday DATE NOT NULL,
profession VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
interests VARCHAR(100) NOT NULL,
seeking VARCHAR(100) NOT NULL
);

Related

Creating a column in MySql with id such as CHV18000002

i could do this in Microsoft sql to create a column incrementing such as CHV180000001, CHV180000002 but trying to do that in MySql. I have tried but getting error: incorrect syntax. Any guide to achieve this: This is my code:
CREATE TABLE Candidates (ID INT AUTO_INCREMENT NOT NULL Primary Key,
[ApplicationID] AS ('CHV18'+right('000000'+CONVERT([varchar](6),[ID]),(6))),
[FirstName] [varchar](100) NOT NULL,
[MiddleName] [varchar](100) NOT NULL,
[LastName] [varchar](100) NOT NULL,
[DateOfBirth] [date] NOT NULL,
[Gender] [nchar](1) NOT NULL
Try this to create the table
CREATE TABLE Candidates (ID INT(11) AUTO_INCREMENT NOT NULL Primary Key,
ApplicationID varchar(6),
FirstName varchar(100) NOT NULL,
MiddleName varchar(100) NOT NULL,
LastName varchar(100) NOT NULL,
DateOfBirth date NOT NULL,
Gender varchar(1) NOT NULL);

MySQL Error code 1452 occurs but values and tables do exist

I cannot for the life of me figure out where did I mess up. I Have the following tables, made using this SQL code:
CREATE TABLE IF NOT EXISTS Users(
username VARCHAR(50) NOT NULL PRIMARY KEY,
pwd TEXT NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
user_type VARCHAR(25) NOT NULL,
user_status VARCHAR(10) NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE UNIQUE INDEX Users_Index
ON Users (username);
CREATE TABLE IF NOT EXISTS Clients(
client_id VARCHAR(10) PRIMARY KEY,
client_name VARCHAR(255) NOT NULL UNIQUE,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
middle_name VARCHAR(25),
related_to VARCHAR(10),
relation_type VARCHAR(25),
ssn_ein VARCHAR(11) NOT NULL UNIQUE,
date_of_birth DATE NOT NULL,
manager VARCHAR(50) NOT NULL,
entity_type VARCHAR(50) NOT NULL,
client_status VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
state VARCHAR(2) NOT NULL,
zip INT NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
notes TEXT,
FOREIGN KEY(manager) REFERENCES Users(username)
);
CREATE UNIQUE INDEX username_index
ON Clients(manager);
I tried to insert data into the Clients table but it gave me error 1452, which is for when a constraint check has failed. Thing is, I already have data in the Users table, I TRIPLE checked for typos, I added the indexes to both tables to no avail, I checked that the reference was pointing to the right table, I checked to see that the engines were both InnoDB as well as the collations being exact, and both username and manager columns are the same exact datatype. I have checked the MySQL documentation and followed all requisites to the best of my knowledge. Does anyone know what could be the problem?
Here is the sample data:
INSERT INTO Clients VALUES (
client_id = "123456",
client_name = "John Doe",
first_name = "John",
last_name = "Doe",
middle_name = "Michael",
related_to = NULL,
relation_type = NULL,
ssn_ein = "123-456-7890",
date_of_birth = "1990-01-01",
manager = "OzzyTheGiant",
entity_type = "Individual",
client_status = "Active",
address = "123 Main St.",
city = "Anytown",
state = "XX",
zip = 78550,
email = "john.doe#example.com",
phone = "123-555-7890",
notes = "self-employed"
);
That's cause the row/record you are trying to insert into Clients table doesn't exists in Users table and thus the error. Since the record doesn't exists in parent table itself, how can you expect to insert in child table? that's not possible.
Also, your INSERT query syntax wrong. Your schema creation script should look like below. See a demo fiddle here: Demo Here
CREATE TABLE IF NOT EXISTS Users(
username VARCHAR(50) NOT NULL PRIMARY KEY,
pwd TEXT NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
user_type VARCHAR(25) NOT NULL,
user_status VARCHAR(10) NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE UNIQUE INDEX Users_Index
ON Users (username);
CREATE TABLE IF NOT EXISTS Clients(
client_id VARCHAR(10) PRIMARY KEY,
client_name VARCHAR(255) NOT NULL UNIQUE,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
middle_name VARCHAR(25),
related_to VARCHAR(10),
relation_type VARCHAR(25),
ssn_ein VARCHAR(11) NOT NULL UNIQUE,
date_of_birth DATE NOT NULL,
manager VARCHAR(50) NOT NULL,
entity_type VARCHAR(50) NOT NULL,
client_status VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
state VARCHAR(2) NOT NULL,
zip INT NOT NULL,
email VARCHAR(255),
phone VARCHAR(255) NOT NULL,
notes TEXT,
FOREIGN KEY(manager) REFERENCES Users(username)
);
CREATE UNIQUE INDEX username_index
ON Clients(manager);
INSERT INTO `Users` VALUES('OzzyTheGiant', 'scxcxx','test1','test2','test','test','test');
INSERT INTO Clients VALUES (
'123456',
'John Doe',
'John',
'Doe',
'Michael',
NULL,
NULL,
'123-456-78',
'1990-01-01',
'OzzyTheGiant',
'Individual',
'Active',
'123 Main St.',
'Anytown',
'XX',
78550,
'john.doe#example.com',
'123-555-7890',
'self-employed'
);

Duplicate data is inserted with unique index

I have a table called users with 4 unique columns and when I insert data in email it doesn't give me any error and inserts the data even when when the same value already exists in that column.
Here is my database structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL,
google_id BIGINT UNSIGNED NULL,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL,
username VARCHAR(20) NULL,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id),
UNIQUE(email,username,fb_id,google_id)
)";
But, when I create my table with following structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL UNIQUE,
google_id BIGINT UNSIGNED NULL UNIQUE,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL UNIQUE,
username VARCHAR(20) NULL UNIQUE,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id)
)";
It gives me an error when there is a duplicate entry.
Creating table with any of those methods doesn't give any error. After creating the tables I have verified with phpmyadmin that all those columns have unique index in both methods.
Akash, in the 1st create table, the composite (combination) is unique. If you want them individually to be unique, separate them into ... separate UNIQUE key statements, like in the 2nd.
Let's say the bottom of your first table read this
PRIMARY KEY(id),
UNIQUE KEY(email,username,fb_id,google_id)
Then there is nothing wrong with these two rows existing in the composite index:
'akash#gmail.com','Akash',101,102
and
'akash#gmail.com','Akash2',101,102

How to select column twice from the same mysql table?

I've been doing quite a bit of MySql lately for uni, and i cant seem to figure out how to get a field from a table twice in the same statement.
My database is this:
drop database if exists AIRLINE;
create database AIRLINE;
use AIRLINE;
CREATE TABLE AIRCRAFT
(
AircraftNo INT(20) NOT NULL,
AircraftType VARCHAR(100) NOT NULL,
FuelBurn VARCHAR(100) NOT NULL,
Airspeed VARCHAR(100) NULL,
LastInspection DATE NULL,
TotalFlyingTime INT(50) NOT NULL,
TotalTimeLeftEngine INT(50) NULL,
TotalTimeRightEngine INT(50) NULL,
PRIMARY KEY (AircraftNo)
);
CREATE TABLE PILOT
(
PilotCode INT(20) NOT NULL,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
MiddleInitial VARCHAR(50) NULL,
HiredDate DATE NULL,
BasePay VARCHAR(50) NULL,
Dependents VARCHAR(100) NULL,
License INT(50) NOT NULL,
TotalHours INT(50) NOT NULL,
PRIMARY KEY (PilotCode)
);
CREATE TABLE CUSTOMER
(
CustomerNo INT(20) NOT NULL,
Name VARCHAR(100) NOT NULL,
Contact INT(50) NOT NULL,
Phone INT(50) NOT NULL,
Street VARCHAR(100) NULL,
Suburb VARCHAR(100) NULL,
State VARCHAR(100) NULL,
Postcode INT(20) NULL,
Balance INT(50) NULL,
PRIMARY KEY (CustomerNo)
);
CREATE TABLE CHARTER
(
TripTicket INT(50) NOT NULL AUTO_INCREMENT,
CharterDate DATE NOT NULL,
PilotCode INT(20) NOT NULL,
CopilotCode INT(20) NULL,
AircraftNo INT(20) NOT NULL,
Destination VARCHAR(100) NOT NULL,
Distance INT(20) NULL,
HoursFlow INT(20) NULL,
HoursWating INT(20) NULL,
Fuel INT(20) NULL,
Oil INT(20) NULL,
CustomerNo INT(20) NOT NULL,
PRIMARY KEY (TripTicket),
FOREIGN KEY(PilotCode) REFERENCES PILOT(PilotCode),
FOREIGN KEY(CopilotCode) REFERENCES PILOT(PilotCode),
FOREIGN KEY(AircraftNo) REFERENCES AIRCRAFT(AircraftNo),
FOREIGN KEY(CustomerNo) REFERENCES CUSTOMER(CustomerNo)
);
My goal is to list the charterdate, destination, customer details (name, customerNo, address, phone), and pilot names (firstname, middleinitial, lastname) of all charters.
I have managed to get everything, but only with one pilot. I need to list both pilot names however.
I have googled my problem, but i cant seem to find anything.
If someone could please point me in the right direction, i would be hugely grateful.
Thanks
Cheers
Corey
You just need to JOIN the table twice with different aliases.
Something like:
SELECT p1.lastname, p2.lastname, /* other fields */
FROM CHARTER c
JOIN PILOT p1 ON p1.PilotCode = c.PilotCode
JOIN PILOT p2 on p2.PilotCode = c.CoPilotCode
Give alias name as
SELECT a.columname1 AS 1, a.columname1 AS 2
FROM tablename a
You have to use table aliases in your join:
SELECT MainPilot.LastName, CoPilot.LastName FROM CHARTER
LEFT JOIN PILOT MainPilot ON MainPilot.PilotCode=CHARTER.PilotCode
LEFT JOIN PILOT CoPilot ON CoPilot.PilotCode=CHARTER.CoPilotCode
You need to join the pilot table twice in your query. to do that you will have to use an alias for each Pilot table you join.
You can simply use the same column multiple times and add for each of them and as and use different name
SELECT column1 as c1, column1 as c2, column1 as c3 FROM TABLE1 WHERE ....

MySQL creating table and return a specific value if row is null

I am trying to create a simple Database:
Students
First_name (May not be empty)
Last_name (May not be empty)
Region (IF empty, return "unknown")
I used the following code:
CREATE TABLE Students
(
First_name VARCHAR(50) NOT NULL,
Last_name VARCHAR(50) NOT NULL,
//And my wild guess:
Region VARCHAR(50) ISNULL(Region, "unknown")
)
Doesn't work :( Any idea why?
drop database if exists studentsdb;
create database studentsdb;
use studentsdb;
CREATE TABLE Students(
id VARCHAR (10) NOT NULL,
First_name VARCHAR ( 50 ) NOT NULL,
Last_name VARCHAR( 50 ) NOT NULL,
Region VARCHAR( 20 ) NOT NULL default 'unknown',
PRIMARY KEY (id)
) ENGINE=InnoDB;
Also, don't forget to specify a primary key!
use default
CREATE TABLE Students
(
First_name VARCHAR(50) NOT NULL,
Last_name VARCHAR(50) NOT NULL,
Region VARCHAR(50) NOT NULL default 'unknown'
)