I tried it with SQL Server and it was working by changing auto_increment into identity, but when it comes to MySQL, I keep getting an error:
create database Students
create table midTBSurname (
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
insert into midTBSurname
(**name**,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
1 errors were found during analysis.
Unrecognized statement type. (near "name" at position 305)
Here is the version in SQL Server
create database Students
create table midTBSurname (
id int not null primary key IDENTITY,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null)
Insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) values
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1')
Select * from midTBSurname;
This the table in SQL Server:
- id name Sex birthdate address contactno Course Yearlevel
- 1 Ruskin M 2002-08-10 Mandaluyong 422-5267 BSMATH 4
- 2 Tristan M 2001-07-23 Quezon 913-6791 BSN 1
- 3 Therese F 1998-06-19 Quezon 422-5267 BSIT 2
In mysql EVERY statement must be terminated with a semi-colon
drop table if exists midTBSurname;
create table midTBSurname(
id int not null primary key AUTO_INCREMENT,
name varchar(15) not null,
Sex char(1) not null,
birthdate date not null,
address varchar(50) not null,
contactno varchar(15),
Course varchar(10) not null,
Yearlevel int not null);
insert into midTBSurname
(name,Sex,birthdate,address,contactno,Course,Yearlevel) VALUES
('Ruskin','M','2002-08-10','Mandaluyong City','422-5267','BSMATH','4'),
('Tristan','M','2001-07-23','Quezon','913-6791','BSN','1'),
('Therese','F','1998-06-19','Quezon','422-5267','BSIT','2'),
('Lejla','F','2001-03-07','Laguna',NULL,'BSN','1');
Select * from midTBSurname;
For example
Related
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);
I am stuck with Error Code 1604 trying to create a table. The code goes like this:
USE Rudinas;
CREATE TABLE clan (
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
How can I get this table to run?
Use comma(,) after birth_date DATE NOT NULL
CREATE TABLE clan (
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL,
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
you just missed the , before sex column.
CREATE TABLE clan(
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
zip MEDIUMINT UNSIGNED NOT NULL,
birth_date DATE NOT NULL ,
sex ENUM("M", "F") NOT NULL,
clan_belong ENUM('EU', 'ASIAPAC', 'USA', 'SLO') NOT NULL,
date_entered TIMESTAMP,
relative_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
);
There will be something wrong with your statement somewhere. Unfortunately with the smallest of errors the human brain will sometimes autocorrect and it takes some time/energy/luck to discover the problem.
One thing I would suggest is to start paring your statement down until it works, then build it back up again. For example, start by removing half the columns, then add the others back until it either breaks (the problem is here) or it works (you didn't make the same mistake this time). This is in fact a good approach for debugging most code, especially if you cannot step through.
Stepping away for a few minutes and coming back can also help, as can utilizing a second pair of eyes.
In this case you're simply missing a comma:
birth_date DATE NOT NULL
-- ^^^
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'
);
I want to make a table in phpmyadmin and I am using SQL command for that
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
I am getting this error:
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 varchar(255) NOT NULL,
address text,
phone varchar(13) NOT ' at line 2
It should be like this
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP);
You are missing KEY after PRIMARY:
CREATE TABLE userdetail (
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
Note that int(255) really doesn't make sense. Are you familiar with the integer data types and what the value in parentheses means? You can review the documentation here.
I am creating tables where I have foreign keys. This is part of the statements. For some reason I cannot get it to work.
What am i doing wrong?
CREATE TABLE Doctor (
NPI NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN NUMBER PRIMARY KEY NOT NULL,
INSURANCE_POLICY_ID NUMBER NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));
You have tagged your question with SQL Server and MySql both, in both of these RDBMS there is no data type called Number but there is a data type to store numbers it is called INT or INTEGER.
Therefore your table definitions should be as follow:
CREATE TABLE Doctor (
NPI INT NOT NULL PRIMARY KEY ,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE INT NOT NULL, --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN INT NOT NULL PRIMARY KEY,
INSURANCE_POLICY_ID INT NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE INT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID)
REFERENCES INSURANCE (INSURANCE_POLICY_ID));