Is my SQL code okay?

Asked

Viewed 77 times

0

I’m learning SQL, and my teacher gave me an activity, I did it, but he gave me 1 grade. I want to know if the code is right. Essa é a lista de atividades

CREATE DATABASE escola;
USE escola;

CREATE TABLE aluno (
   codigo INT PRIMARY KEY AUTO_INCREMENT,
   nome VARCHAR (50) ,
   idade INT,
   cpf VARCHAR (15)
);

CREATE TABLE professor(
   codigo INT PRIMARY KEY AUTO_INCREMENT,
   nome VARCHAR (50),
   disciplina VARCHAR (50),
   salario DOUBLE
);

INSERT INTO aluno
VALUES (0000000, 'Joao', 13, 01010101011),
(1111111, 'Maria', 18, 02020202020),
(2222222, 'Gabriel', 20, 03030303030),
(3333333, 'Marcos', 30, 04040404040),
(4444444, 'Vitoria', 16, 05050505050);

INSERT INTO professor
VALUES (00000, 'Victor', 'Matemática', 1500),
(01010, 'Joana', 'Português', 2500),
(02020, 'Mario', 'Geografia', 1800),
(03030, 'Daniel', ''Física, 2500),
(04040, 'Juliana', 'Arte', 2300);


UPDATE aluno SET idade = 19 WHERE nome='Maria'

UPDATE professor SET salario = 2300 WHERE nome='Joana'

DELETE FROM aluno WHERE nome='Marcos'

DELETE FROM professor WHERE nome='Juliana'


SELECT * FROM aluno

SELECT * FROM professor

SELECT * FROM aluno WHERE idade>16

SELECT * FROM professor WHERE salario>2000
  • You tested the code?

  • 1

    I don’t know if the instructor took it as a mistake, but the names of the bank and the tables he asked for in capital letters. The INSERT is with several errors, to insert values you need to point the field that will receive such value, example: INSERT INTO Student (code, name, age, Cpf) VALUES (0000000, 'Joao', 13, '01010101011'), vc entered Cpf values as integer, but, it is a field of the type SWEEP, finally, when inserting the value Física you didn’t put in quotes.

1 answer

1


I believe that a valuable tip is, avoid using string when it is an operation of "update" or "delete". Because if there is more than one record in the bank with the same value example "Maria" or "Joana", will end up performing the operation for all. Always use ID as reference.

Example: UPDATE aluno SET idade = 19 WHERE id = 2

And in the Insert you made was missing simple quotes in value Physique.

INSERT INTO professor
VALUES (00000, 'Victor', 'Matemática', 1500),
(01010, 'Joana', 'Português', 2500),
(02020, 'Mario', 'Geografia', 1800),
(03030, 'Daniel', 'Física', 2500),
(04040, 'Juliana', 'Arte', 2300);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.