Joeyrodrigues92
2/2/2017 - 4:56 AM

sql table, students example 2/1/17

sql table, students example 2/1/17

//Example of creating a table//

CREATE TABLE students( 
id INTEGER(11) AUTO_INCREMENT NOT NULL,  //INTEGER = number
name VARCHAR(30) NOT NULL, //VARCHAR = letter or 'strings'
age INTEGER(10), 
location VARCHAR(30) NOT NULL, 
PRIMARY KEY(id) *****//Very IMportant, always want to assign id's to everything********
);
 
INSERT INTO <table name> (column names) VAlues(value for each column)
        

select * , selects all columns
select * from //to from certain row//
 select * from students where id in (2,3);

//to update //
UPDATE students SET name = 'JHON' WHERE id = 2;///specify where or everything deletes;///


//TO UPDATE TWO IDS//
UPDATE students SET age=22 where id in (3,4);

ACD --> Add Change Delete

//to delete//
DELETE from students
WHERE id=4;

select * from students left join pets on pets.student_id = students.id; //or//
select * from students s left join pets p on s.id = p.student_id;  //shows all the students first and joing pets to the table//

    "    "    "   s right join pets p on s.id = p.students_id //grabs everything from the right table and joins everything to the left table//

ALTER TABLE pets
    -> ADD FOREIGN KEY (student_id)
    -> REFERENCES students(id);

this is to add the foreign key