Question

I am very new to MySQL coding I assure you my question will make me look like a fool. However despite constantly looking for good example code I constantly come across varying syntax and organizational habits; I simply want to know once I create a table and fill it with data is it acceptable to just enter in my queries or should there be some break in the code, For example a language like java or C would use curly brackets,{ }. My Problem is the assignment requires us not to use foreign keys but instead, to just set up a general model; that while not functional looks right, This is part of my Code.

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Music`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Music` (
  `Title` VARCHAR(25) NOT NULL,
  `Artist` VARCHAR(30) NOT NULL,
  `Producer` VARCHAR(45) NOT NULL,
  `Genre` VARCHAR(20) NOT NULL,
  `Year` YEAR NOT NULL,
  `Price` DOUBLE NOT NULL,
  `CD Quantity` INT NOT NULL,
  `Cassette Quantity` INT NOT NULL,
  `Sheet Music Quantity` INT NOT NULL,
  `MID` INT NOT NULL,
  `VID` INT NOT NULL,
  PRIMARY KEY (`Title`, `Artist`, `Producer`, `Genre`, `Year`, `Price`, `CD Quantity`,     `Cassette Quantity`, `Sheet Music Quantity`, `MID`, `VID`))
  ENGINE = InnoDB
COMMENT = '         ';

Hear I create more tables, then I insert into this table;

#Inserts for Music


$sql = INSERT INTO Music.
       (Title, Artist, Producer, Genre, Year, Price, CD Quantity, Cassette Quantity,   Sheet Music Quantity, MID, VID) .
       VALUES .
('Jazz Hits ', 'Dizzy gillespi', 'Jazz co.', 'Jazz', '1990','12.00','2', '5', '8',      '1', '1');

$sql = INSERT INTO Music.
       (Title, Artist, Producer, Genre, Year, Price, CD Quantity, Cassette Quantity, Sheet Music Quantity, MID, VID) .
       VALUES .
('Funky Fresh', 'Snoop', 'HOV', 'Hip Hop', '1997', '6.00', '5', '3', , '2', '2', '3');

$sql = INSERT INTO Music.
       (Title, Artist, Producer, Genre, Year, Price, CD Quantity, Cassette Quantity,  Sheet Music Quantity, MID, VID) .
       VALUES .
('Classic Jazz', 'Cole Train', 'Jazz inc.', 'Jazz', '1992', '8.00', '5', '3 ',  '4', '3', '1');

$sql = INSERT INTO Music.
       (Title, Artist, Producer, Genre, Year, Price, CD Quantity, Cassette Quantity, Sheet Music Quantity, MID, VID) .
       VALUES .
('Rock Hits', 'ACDC', 'Rocafella', 'Rock', '1989', '11.00', '7', '5 ', '1', '4', '2');

$sql = INSERT INTO Music.
       (Title, Artist, Producer, Genre, Year, Price, CD Quantity, Cassette Quantity, Sheet Music Quantity, MID, VID) .
       VALUES .
('Jazz Masterpiece', 'Jazzy J', 'Jazz Bros', 'Jazz', '1990', '1', '5 ', '15.00', '2', '5', '1');

Lastly I add some action Statements;

DELETE FROM Music
WHERE Title='Classic Jazz' AND Artist='Cole Train' AND Producer='Jazz inc.' AND Genre = 'Jazz' AND Year='1992' AND  Price='8.00' AND CD Quantity='5'AND Cassette Quantity= '3' AND Sheet MusicQuantity='4' AND VID='1';

UPDATE Music
SET Title='Rockhits', Artist='The Beats', Producer='RockProductions', Genre='rock', Year='1977', Price='12.25', CD Quantity='2', Cassette Quantity='5', Sheet Music Quantity='2', VID='2'
WHERE MID='3'; 

SELECT SUM(CD Quantity)
    -> FROM Music;

SELECT SUM(Cassette Quantity)
    -> FROM Music;

SELECT SUM(Sheet Music Quantity)
    -> FROM Music;

My question is does the setup look right or should my action statements have some sort of separation, weather it be via bracketing/headers or another organizational manor?

Was it helpful?

Solution

In SQL the structure is in the Queries, you usually operate on SETs instead of single data.

Inserting Data is just one Insert followed by the next insert and so on. you could even speed things up with multiple-row-inserts,

insert into test (id, name) values (1,"john"), (2,"paul"), (3,"ringo");

But use a new insert every few hundred KB (Maximum is depending on some max length of statement-variable).

The other structures are just the JOINs and the WHEREs, like in your example.

You do have a structure already, you seem to create the SQL in some programming language, which is a good thing ($sql, .).

I do have some nice programs just as a list of SQL-Statements in a text file, but users nowadays tend to want something less compact than

name |   city   |  age
   john   | London|45
 Paul |  Berlin      | 107

so you need a program anyway.

Have you tried your Statements, do they work?

EDIT:

You lack of Normalization: You very probably will have more than one CD for a genre. If you just let insert Strings like "Rock", "Rock'n'Roll", "Rock and Roll", you will end up with many similar entries and would also get in trouble if you want some additional properties. So: for everything which is needed in different rows or different places, use "Normalization". That just and simply means: if "genre" is an entity and not clearly bound 1:1 to another entity, you should give it a table. So goes for artist. Price is just a property of a certain "Music".

So: create table genre (id int(11) not null auto_increment, shortname varchar(32), name varchar(255))

and mydb.music just has a genre_id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top