I can't create table in mysql [duplicate] - mysql

Can anybody tell me how to create columns with space like "FULL NAMES"?
I've tried like the following but it doesn't work.
CREATE TABLE info
(
Full Names varchar(20),
Physical Address varchar(20),
Moviesrented varchar(100),
Salutation varchar(20),
Category varchar(20),
PRIMARY KEY (address)
)

Just put the name in backticks:
CREATE TABLE info (`Full Names` varchar(20), ...)
But that's no good handle for naming your columns.

I wonder why you want that. But to answer your question, wrap it with backticks.
`Full Names` VARCHAR(20)

Related

How to make the primary key as varchar

Hi guys so i dont know how to make it as varchar because is want to input BSIT as my primary key and i cant figure it out how to make it as characters please help me this is my code btw
create table CourseTBL
(
CourseID int primary key,
Course varchar(50),
Description varchar(50),
)
It's better to create a different CODE column to use WHERE condition on it, because key is something meaningless and is intended for referential integrity only. For PK add a constraint as an another element of CREATE TABLE statement. Like this:
create table CourseTBL
(
CourseID varchar(50),
Course varchar(50),
Description varchar(50),
primary key( CourseID )
)
My preference is to add course code column
Create table CourseTBL
(
CourseID int primary key,
CourseName varchar(50),
CourseCode varchar(10),
CourseDescription varchar(50),
)

need help creating a simple many-to-many relationship using mysql

So I got used to using doctrine so much that I forgot the standard mysql syntax, which I need for an exam.
I want to create a test database called 'youtube'
with an app_users table and an app_videos table.
In "real life" this would be a oneToMany relationship, but I need to
study to create a manyToMany relationship.
so its
"many app_users" -> have "many app_videos"
and the other way around.
so first I created a database;
CREATE DATABASE youtube;
use youtube;
then I created the users and videos table
CREATE TABLE app_users(
id INT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(40),
first_name VARCHAR(20),
last_name VARCHAR(20),
video_id INT,
PRIMARY KEY(id)
);
CREATE TABLE app_videos(
id INT AUTO_INCREMENT,
video_path VARCHAR(20),
description VARCHAR(20),
likes INT(20),
dislikes INT(20),
user_id INT,
PRIMARY KEY(id)
);
then I created a relationship table
CREATE TABLE vid_uid
(
id INT AUTO_INCREMENT,
video_id INT,
user_id INT,
PRIMARY KEY(id)
);
now what I need to do is somehow connect the columns to eachother but my syntax never works
I know I need to change this
ALTER TABLE app_users
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(video_id);
to something that works :/
You need to change app_videos(video_id) to app_videos(id). Also you want to alter the table vid_uid (not app_users) since thats the one that as references to external keys:
ALTER TABLE vid_uid
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(id);
You'll also need to do:
ALTER TABLE vid_uid
ADD FOREIGN KEY(user_id)
REFERENCES app_users(id);

MySQL create table with set attribute from another table?

I am a beginner at this but is it possible (in MySQL workbench) to create an attribute that is a set depended on an attribute from another table?
CREATE TABLE danes (
id INT PRIMARY KEY,
name VARCHAR(50),
nationality VARCHAR(20),
gender CHAR(1),
degree SET ?????????????????? from degree(level)
);
CREATE TABLE degree (
level VARCHAR(10),
subject VARCHAR(20),
institutionawarding VARCHAR(20),
yearawarded DATE,
PRIMARY KEY (level, subject)
);
never mind I got it
I am guessing that you want another table, a junction table:
CREATE TABLE DaneDegrees (
DanesId INT REFERENCES danes(id),
Level VARCHAR(10),
Subject VARCHAR(20),
FOREIGN KEY fk_level_subject(level, subject) REFERENCES Degree(level, Subject)
);
I would, however, have an INT AUTO_INCREMENT PRIMARY KEY in both Danes and Degrees.
apparently you cant just say
<attribute_name> set(get set from another tabe)
the set thing doesn't work that way
eventhough you can apparently update it so it actually can
update table_name
attribute = (select * from another table_name)

How to create column name with space?

Can anybody tell me how to create columns with space like "FULL NAMES"?
I've tried like the following but it doesn't work.
CREATE TABLE info
(
Full Names varchar(20),
Physical Address varchar(20),
Moviesrented varchar(100),
Salutation varchar(20),
Category varchar(20),
PRIMARY KEY (address)
)
Just put the name in backticks:
CREATE TABLE info (`Full Names` varchar(20), ...)
But that's no good handle for naming your columns.
I wonder why you want that. But to answer your question, wrap it with backticks.
`Full Names` VARCHAR(20)

Setting Auto_Increment to start from 1000 when creating table

I want my table's primary key to start from the number 1000 instead of the default. I read around here and the answer is to declare the increment value when creating the table as follows:
AUTO_INCREMENT = 1000;
I tried it when I CREATE my table but it returns a syntax error. Of course I could make the changes on PhpMyAdmin but I want to do it on CREATE instead. Please advice what is wrong. Thanks.
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT=1000, -- the equals sign returns the syntax error.
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
);
The starting point must be specified as a table option:
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT,
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
) AUTO_INCREMENT=1000;
You can see the full syntax for creating a table here