Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.

Similar presentations


Presentation on theme: "SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case."— Presentation transcript:

1 SQL Basics

2 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case sensitive But user identifier names ARE case sensitive

3 5/27/2016Chapter 33 of 19 Database Check what databases you have Check what databases you have mysql> SHOW DATABASES; mysql> SHOW DATABASES; Create a database Create a database mysql> CREATE DATABASE name; mysql> CREATE DATABASE name; Delete a database Delete a database mysql> DROP DATABASE name; mysql> DROP DATABASE name; Open a database to use Open a database to use mysql> USE name; mysql> USE name;

4 5/27/2016Chapter 34 of 19 Tables Check what tables you have in the database Check what tables you have in the database mysql> SHOW TABLES; mysql> SHOW TABLES;

5 5/27/2016Chapter 35 of 19 Tables Create a table Create a table mysql> CREATE TABLE potluck ( mysql> CREATE TABLE potluck ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), food VARCHAR(30), confirmed CHAR(1), signup_date DATE);

6 5/27/2016Chapter 36 of 19 Tables Check the table structure Check the table structure mysql> DESCRIBE potluck; mysql> DESCRIBE potluck;

7 5/27/2016Chapter 37 of 19 Add a Column Add a new column at the end of the table Add a new column at the end of the table ALTER TABLE potluck ADD email VARCHAR(40); ALTER TABLE potluck ADD email VARCHAR(40);

8 5/27/2016Chapter 38 of 19 Add a Column Add a new column at a specific location in the table Add a new column at a specific location in the table ALTER TABLE potluck ADD email VARCHAR(40) AFTER name; ALTER TABLE potluck ADD email VARCHAR(40) AFTER name;

9 5/27/2016Chapter 39 of 19 Delete a Column Delete a column from the table Delete a column from the table ALTER TABLE potluck DROP email; ALTER TABLE potluck DROP email;

10 5/27/2016Chapter 310 of 19 Add Record Add a record into a table Add a record into a table INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "John", "Casserole","Y", '2012- 04-11'); INSERT INTO `potluck` (`id`,`name`,`food`,`confirmed`,`signup_date`) VALUES (NULL, "John", "Casserole","Y", '2012- 04-11');

11 5/27/2016Chapter 311 of 19 List Records List the records in a table List the records in a table SELECT * FROM potluck; SELECT * FROM potluck;

12 5/27/2016Chapter 312 of 19 Update Record Update a record in a table Update a record in a table UPDATE `potluck` SET `confirmed` = 'Y' WHERE `potluck`.`name` = 'Sandy'; UPDATE `potluck` SET `confirmed` = 'Y' WHERE `potluck`.`name` = 'Sandy';

13 5/27/2016Chapter 313 of 19 Delete Record Delete a record from a table Delete a record from a table DELETE from potluck where name='Sandy'; DELETE from potluck where name='Sandy';

14 5/27/2016Chapter 314 of 19 Create Foreign Key Create a foreign key Create a foreign key CONSTRAINT constraint_name FOREIGN KEY foreign_key_name (columns) REFERENCES parent_table(columns) ON DELETE action ON UPDATE action http://www.mysqltutorial.org/mysql-foreign-key/

15 5/27/2016Chapter 315 of 19 Create Foreign Key CREATE TABLE categories( cat_id int not null auto_increment primary key, cat_id int not null auto_increment primary key, cat_name varchar(255) not null, cat_name varchar(255) not null, cat_description text cat_description text ) ;

16 5/27/2016Chapter 316 of 19 Create Foreign Key CREATE TABLE products( prd_id int not null auto_increment primary key, prd_id int not null auto_increment primary key, prd_name varchar(355) not null, prd_name varchar(355) not null, prd_price decimal, prd_price decimal, cat_id int not null, cat_id int not null, FOREIGN KEY fk_cat(cat_id) FOREIGN KEY fk_cat(cat_id) REFERENCES categories(cat_id) REFERENCES categories(cat_id) ON UPDATE CASCADE ON UPDATE CASCADE ON DELETE RESTRICT ON DELETE RESTRICT);

17 5/27/2016Chapter 317 of 19 Show Foreign Key Show the foreign key created in table Show the foreign key created in table SHOW CREATE TABLE table_name http://www.mysqltutorial.org/mysql-foreign-key/

18 5/27/2016Chapter 318 of 19 Add Foreign Key Add a foreign key to an existing table Add a foreign key to an existing table ALTER table_name ADD CONSTRAINT constraint_name FOREIGN KEY foreign_key_name (columns) REFERENCES parent_table(columns) ON DELETE action ON UPDATE action http://www.mysqltutorial.org/mysql-foreign-key/

19 5/27/2016Chapter 319 of 19 Add Foreign Key ALTER TABLE products( ADD FOREIGN KEY fk_vendor(vdr_id) ADD FOREIGN KEY fk_vendor(vdr_id) REFERENCES vendors(vdr_id) REFERENCES vendors(vdr_id) ON UPDATE CASCADE ON UPDATE CASCADE ON DELETE RESTRICT ON DELETE RESTRICT);

20 5/27/2016Chapter 320 of 19 Drop Foreign Key Drop foreign key from table Drop foreign key from table ALTER table_name DROP FOREIGN KEY constraint_name http://www.mysqltutorial.org/mysql-foreign-key/


Download ppt "SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case."

Similar presentations


Ads by Google