How to make the primary key as varchar - mysql

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),
)

Related

Not getting column of table

CREATE TABLE login(
name varchar(200),
email varchar(200) primary key,
count int,
depart varchar(200),
type varchar(1),
password varchar(40));
CREATE TABLE qsstudent(
department varchar(200),
qs int,
email varchar(200),
type varchar(1),
qscorrect int,
qsdone int,
qswrong int ,
FOREIGN KEY (email) REFERENCES login(email));
CREATE TABLE qsteacher(
email varchar(200),
department varchar(200),
qsentered int,
type varchar(1),
FOREIGN KEY (email) REFERENCES login(email));
When I run the query below, it cannot find the login.depart column. I have tried doing this several times but it is not running. I don't think that there is an error in the foreign key
SELECT qsteacher.qsentered
FROM login,qsteacher
WHERE login.depart=qsteacher.department
AND qsstudent.email=login.email;
but still the error persists.
cannot find the login.depart column
This is the exact thing your query can't find:
SELECT qsteacher.qsentered
FROM login,qsteacher
WHERE login.depart=qsteacher.department
AND qsstudent.email=login.email; //<-- you didn't select qsstudent
If you want to use qsstudent on this query you need add it to the FROM
FROM login,qsteacher,qsstudent
I recommend to do something like this with INNER JOIN:
SELECT T3.qsentered
FROM login as T1
INNER JOIN qsstudent as T2 on T2.email=T1.email
INNER JOIN qsteacher as T3 on T1.depart=T3.department
and If you want to select by specific user just add this WHERE to the end:
WHERE T1.email = 'user_email'
Firstly your primary key should be an id, not an email, for multiple reasons, like uniqueness and index. So I will add an id to all of your tables which should auto increment with every record. And Your foreign key should be an id as well so I will change that.
CREATE TABLE login(
id int NOT NULL AUTO_INCREMENT primary key,
name varchar(200),
email varchar(200),
count int,
depart varchar(200),
type varchar(1),
password varchar(40));
CREATE TABLE qsstudent(
id int NOT NULL AUTO_INCREMENT primary key,
department varchar(200),
qs int,
email varchar(200),
type varchar(1),
qscorrect int,
qsdone int,
qswrong int ,
FOREIGN KEY (login_id) REFERENCES login(id));
CREATE TABLE qsteacher(
id int NOT NULL AUTO_INCREMENT primary key,
email varchar(200),
department varchar(200),
qsentered int,
type varchar(1),
FOREIGN KEY (login_id) REFERENCES login(id));
Now for the query that you are trying to run from what I can tell:
SELECT q.qsentered FROM qsteacher q
JOIN login l ON l.id = q.login_id
JOIN qsstudent s ON s.login_id = l.id
WHERE s.department = q.department
Please comment exactly what it is you are trying to query so I can better answer your question. It is not clear what you are looking for.

new to sql/mysql cant figure out foriegn keys

CREATE DATABASE FINAL_PROJECT;
USE FINAL_PROJECT;
CREATE TABLE MOVIE_RATING
(RATING_ID VARCHAR (1) PRIMARY KEY,
RATING VARCHAR(4));
CREATE TABLE MOVIE
(MOVIE_ID VARCHAR (2) PRIMARY KEY,
TITLE VARCHAR(20),
YEAR_RELEASED VARCHAR(4),
RATING_ID VARCHAR(1),
BW VARCHAR(4),
SUBTITLES VARCHAR(5));
CREATE TABLE CAST
(CAST_ID VARCHAR (2) PRIMARY KEY,
LAST_NAME VARCHAR(20),
FIRST_NAME VARCHAR(4),
DOB VARCHAR(20),
DOD VARCHAR(20));
CREATE TABLE MOVIE_CAST
(MOVIE_ID VARCHAR (2),
CAST_ID VARCHAR(2),
PERSONA VARCHAR(4));
I need to code a foreign keys for the tables here currently have no clue as to what to do for the following:
In the movie table I need to reference the rating id that is
present in the rating table.
In the cast table i need to reference movie id and cast id
from the movie table and cast table
This command will crate a foreign key:
alter table movie
add foreign key (rating_id)
references rating(id);
I recommend renaming the cast table to something else, because cast is a built-in function. Maybe movie_cast.

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 tables on putty if they all depend on each other by using mysql syntax?

I have to create a number of tables for my project and each one of them has a foreign key besides their primary key. I know how to create a table on putty without the foreign key but if they are all depends on each other, then how to create them individually ?
For example, if I want to create table A, B and C
CREATE TABLE PROFESSOR
(
SSN numeric(9) primary key,
PNAME varchar(20),
CITY varchar(20),
STREETADDRESS varchar(50),
STATE char(2),
ZIP numeric(5),
AREACODE numeric(3),
PHONENUMBER numeric(7),
SEX char(1),
TITLE char(4),
SALARY float(9),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
CREATE TABLE DEPARTMENT
(
DNUM numeric(1) primary key,
DNAME varchar(20),
DPHONE numeric(10),
OFFICELOCATION varchar(20),
foreign key (CWID) references STUDENT(CWID)
);
CREATE TABLE STUDENT
(
CWID numeric(9) primary key,
FNAME varchar(20),
LNAME varchar(20),
SADDRESS varchar(50),
SPHONE numeric(10),
foreign key (DNUM) references DEPARTMENT(DNUM)
);
I'm using Putty to create table and I have to run them separately. The thing is if I run the scrip separately for each table, it's going to give me an error, because the table that has the foreign key does not create yet. How am I going to fix it ? and is there a work around way on solving this problem ? Thank you.
If I run the script for creating table separately, it will give me an error

Creating a table, SQL. Basic stuff

So I'm having a small problem with creating a table. The problem is in my attempt to create a foreign key to another table. I'm currently using MYSQL2008 Management R2 express, so no designer. Heres my two tables
use teckDB;
CREATE TABLE inventory
(
primId int NOT NULL PRIMARY KEY,
prodName VarChar(255),
quantity int,
prodCost MONEY,
prodDesc VARCHAR(255)
);
CREATE TABLE orderTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT NOT NULL,
created date,
prodId INT,
);
Those two tables ran without a problem. When create the thrid one however causes this error message.
Msg 1769, Level 16, State 1, Line 3 Foreign key 'orderTB' references
invalid column 'orderTB' in referencing table 'CustomerTB'. Msg 1750,
Level 16, State 0, Line 3 Could not create constraint. See previous
errors.
On the thrid table of....
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT, FOREIGN KEY (orderTB) REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
try this
FOREIGN KEY (iparent_id) REFERENCES innodb_parent (iparent_id)
I think this should help to solve your query
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
You have an extra comma after "orderId INT", and you have got the foreign key syntax wrong.
This should work:
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
tested on SQLFiddle here