MariaDB create database relational tables - php

I created 2 tables as follows:
$sql = "CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(25) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL,
first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,
email varchar(50) UNIQUE NOT NULL
date_created VARCHAR(25) NOT NULL,
date_modified VARCHAR(25) NOT NULL
)";
This table was created successfully.
I tried to create a 2nd table as follows:
$sql = "CREATE TABLE users_performance (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL,
performance text NOT NULL
)";
This failed with following error:
Error creating table: You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'NOT NULL, performance text NOT NULL )' at line 3
Now the error line is:
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL
I have scratched through Maria DB manual but can't quickly get a solution.
I know this syntax works fine on postgresql. Why is it not working on mysqli
I prefer to create 2 separate tables for future changes, etc.
Thanks

First, check that you are using MariaDB 10.5 or later. Inline foreign keys are not implemented in older versions of MariaDB.
Then, move the UNIQUE NOT NULL before the foreign key option. The order of column options is important.
Also, make sure the data type of the foreign key column is the same as the column it references. In this case, you need to make it INT UNSIGNED. The "length" option (11) doesn't matter, and you can omit it.
user_id INT UNSIGNED UNIQUE NOT NULL REFERENCES users(id),
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=54964388cd0d880169c0332934c13b3d

Related

Error #1064 while executing sql query in phpmyadmin (wamp server)

m trying to make simple login registration app in android having php mysql db at the back end.. when i try to execute following query, i get error in phpmyadmin
Code:
create database android_api /** Creating Database **/
use android_api /** Selecting Database **/
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
); /** Creating Users Table **/
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 'use android_api /** Selecting Database **/
create table users(
id int(11' at line 3
also i'd like to know.. the tutorial m following says i need two machines on same wifi network to run php n android.. cant they be run on same machine?? how about after deployment??
try the following. Creates a db to keep it out of yours for a test.
create schema abc123; -- create a db
use abc123; -- use it
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null,
name varchar(50) not null,
email varchar(100) not null,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime null,
updated_at datetime null,
unique key(unique_id),
unique key(email)
); /** Creating Users Table **/
drop schema abc123; -- clean up drop schema
Run this:
create database android_api;
use android_api;
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);

Creating Tables with MYsql in yii 1.1

Here is the scenario: I have been following the "yii" book by Larry Ullman in which he gave his MYsql for CMS but he didn't described any tool , how to create these tables of sql. The only way I know is through migrations but the sql written in the book is not working in migrations.
The sample sql is given for a table from the book :
CREATE TABLE IF NOT EXISTS yii_cms.user ( id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(45) NOT NULL,
email VARCHAR(60) NOT NULL,
pass CHAR(64) NOT NULL,
type ENUM('public','author','admin') NOT NULL,
date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE INDEX username_UNIQUE (username ASC),
UNIQUE INDEX email_UNIQUE (email ASC) )
ENGINE = InnoDB DEFAULT
CHARACTER SET = utf8
If you don't mind, you can use straight-forward way to execute sql so:
$sql = 'your sql here';
Yii::app()->db->createCommand($sql)->execute();
The other way is to use Yii QueryBuilder and the createTable() command. More information for QueryBuilder is here and for createTable() here.

Why won't my foreign key create in MySQL?

I've tried many different ways to create table with a foreign key and trying to insert into phpMyAdmin. However, it just not working as I was expected.
Here are what I've so far:
CREATE TABLE user (
user_id BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(50) NOT NULL,
user_password VARCHAR(50) NOT NULL);
This works perfectly fine. However, if I try to add a table with a foreign key thus, it refuses to create:
CREATE TABLE article (
article_id INT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
article_title VARCHAR(100) NOT NULL,
article_content VARCHAR(1000) NOT NULL,
user_id INT(10) NOT NULL,
FOREIGN KEY (user_id) REFERENCES user (user_id));
This would not work as expected and would not add the table to the MySQL database. I get this error:
Cannot add foreign key constraint
How can I fix it?
We discovered in the comments that if a primary key is defined thus:
user_id BIGINT(10) UNSIGNED
then a foreign key like this will not work, since it needs to match on signedness (and I think type too):
user_id INT(10) NOT NULL
This works fine:
user_id BIGINT(10) UNSIGNED NOT NULL
Here's a Fiddle to demonstrate it working.

How to create a table in a database

I am trying to create a table in my database, accountdb. My query to create the table is:
$query1="CREATE TABLE asset( id int(16) auto_increment primary key,TotBalance double(35),creditAmnt double(35),debitAmnt double(35))";
After that when I am executing the above query, the database is created, but there is an error in creating the table. The error is as follows:
error creating tableYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),creditAmnt double(35),debitAmnt double(35))' at line 1
How can I fix this error?
Here is the correct query:
CREATE TABLE asset(
id int(16) auto_increment primary key NOT NULL,
TotBalance double(35,3),
creditAmnt double(35,3),
debitAmnt double(35,3)
);
When the datatype is double, float, or decimal, you need to specify the decimal places.
Correct syntax to create a double datatype column:
double(D,M);
M is the total number of digits and D is the number of digits following the decimal point.
See also: http://code.rohitink.com/2013/06/12/mysql-integer-float-decimal-data-types-differences/
I hope this helps you.
CREATE TABLE IF NOT EXIST asset(
id int(16) auto_increment primary key
,TotBalance double(35)
,creditAmnt double(35)
,debitAmnt double(35)
);
CREATE TABLE assets (
id int(16) NOT NULL AUTO_INCREMENT,
Tot_balance bigint(20) NOT NULL,
Credit_Amt bigint(20) NOT NULL,
Debit_Amt bigint(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

mysql problems with full text and type

I'm using xampp software
In that I opened mysql my admin and paste that sql code. That shows an error like
#1214 - The used table type doesn't support FULLTEXT indexes
#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 'TYPE = MYISAM' at line 7
CREATE TABLE code (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
chapter TINYINT UNSIGNED NOT NULL,
code TEXT NOT NULL,
FULLTEXT (title,code)
) TYPE = MYISAM;
Try this instead:
CREATE TABLE code
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
chapter TINYINT UNSIGNED NOT NULL,
code TEXT NOT NULL,
FULLTEXT (title,code)
) ENGINE = MyISAM;

Categories