Posts

Showing posts from October, 2022

SQL-Basics 6(Primery key,Foreign key,update cascade,delete Cascade)

Image
 ------Primary key and Foreign key ------ create database key_prim; use key_prim; Primary Key:- Table:1 create table ineuron( course_id int not null, course_name varchar(60), course_status varchar(40), number_of_enr int, primary key(course_id)); insert into ineuron values(01,'FSDA','active',100); select * from ineuron; insert into ineuron values(01,'FSDA','not active',100); Error: --------:- Error Code: 1062. Duplicate entry '1' for key 'ineuron.PRIMARY' Note:- ---->Bcz primary key sholud be unique(it is not allowed duplicate values). insert into ineuron values(02,'FSDA','not active',100); select * from ineuron; -------->Whenever we want to establish a relationship b/w one table to another table, then we can use  together primary key and foreign key.   Foreign key:- Table:2 create table students_ineuron( student_id int, course_name varchar(60), student_mail varchar(60), student_status varchar(60), course_id1 int, for...

SQL-Basics 5 (UDF,IF ELSE,WHILE LOOP)

select database(); use today; select * from sales; desc sales; ----------------------How to create the UDF(user-defined functions)-------------------      -------About Delimiter ---------- -------->Please refer to the below link about delimiter   https://www.mysqltutorial.org/mysql-stored-procedure/mysql-delimiter/ Ex:-1 ------- DELIMITER $$ create function add_to_col(a INT) Returns INT DETERMINISTIC  Begin      Declare b int;      set b=a+10;      return b; end $$     select add_to_col(12.2); -------This color code is just trial and error For understanding Deterministic-------------- Ex:-2(This is not recommended without deterministic) Delimiter $$ create function add_to_col_1(a int) returns int Begin      Declare c int;       set c=a+20;       return c; end $$       -------Above function am not mentioned Deterministic --------- show ...

SQL-Basics(3A) (How to load bulk data into table In MYSQL Database)

                                  ------------- Table Creation-------------------- create table if not exists dress( Dress_ID varchar(30), Style varchar(30), Price varchar(30), Rating varchar(30), Size varchar(30), Season varchar(30), NeckLine varchar(30), SleeveLength varchar(30), waiseline varchar(30), Material varchar(30), FabricType varchar(30), Decoration varchar(30), Pattern Type varchar(30), Recommendation varchar(30)) Error:- ------ ---Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your    MySQL server version for the right syntax to use near 'Type varchar(30),  Recommendation  varchar(30))' at line 14 Reason:- ---------- ----->Here (Type) is the default keyword because of that we are getting the above error to resolve this  we should use the backtick(`) symbol. create table if not exists dress( `Dress_ID...

SQL-Basics 4(Time&Date Functions)

 create database today; -----How to check which databases are used right now------ select database(); use today; CREATE TABLE sales ( order_id VARCHAR(15) NOT NULL,  order_date VARCHAR(15) NOT NULL,  ship_date VARCHAR(15) NOT NULL,  ship_mode VARCHAR(14) NOT NULL,  customer_name VARCHAR(22) NOT NULL,  segment VARCHAR(11) NOT NULL,  state VARCHAR(36) NOT NULL,  country VARCHAR(32) NOT NULL,  market VARCHAR(6) NOT NULL,  region VARCHAR(14) NOT NULL,  product_id VARCHAR(16) NOT NULL,  category VARCHAR(15) NOT NULL,  sub_category VARCHAR(11) NOT NULL,  product_name VARCHAR(127) NOT NULL,  sales DECIMAL(38, 0) NOT NULL,  quantity DECIMAL(38, 0) NOT NULL,  discount DECIMAL(38, 3) NOT NULL,  profit DECIMAL(38, 8) NOT NULL,  shipping_cost DECIMAL(38, 2) NOT NULL,  order_priority VARCHAR(8) NOT NULL,  `year` DECIMAL(38, 0) NOT NULL); select * from sal...

SQL-Basics (3B)(Constraints)

                             SQL Constraints                           ----------------------- SQL constraints are used to specify rules for the data in a table. Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table.  If there is any violation between the constraint and the data action, the action is aborted. Constraints can be used column level or table level.  Column-level constraints apply to a column, and table-level constraints apply to the whole table. ------>As per my understanding, we have 6 types of constraints                             1)Primary key                             2)Foreign Key   ...

SQL(Basics2)(Procedures &Views)

 show databases create database dev use dev select database() select count(*) from bank_details Stored Proceudre ----------------:- Note:- ----->In SQL indentation is important in the case of functions and procedures only. A stored procedure is a group of statements that can be executed. -------How to Create the Procedure----------------------- DELIMITER && create procedure select_rec() BEGIN  select * from bank_details; END &&  How to Call the procedure --------------------------------- syntax:- call <procedure name> call select_rec() DELIMITER && create procedure bal_min() BEGIN  select * from bank_details where balance in(select min(balance) from bank_details); END &&  call bal_min() select * from bank_details select distinct job from bank_details select count(distinct job) from bank_details ------How to create parameterized  procedure----------------------- DELIMITER && create procedure job_all(IN galla v...

SQL on MYSQL Database(Basics1)

                                              SQL on  MYSQL Database                                                                 How To check the existing databases  ------------------------------------------- show databases How To create the database --------------------------------- syntax :- create database <db name> Ex:- create database test How to check which database are using now ------------------------------------------------------ select database() How to use which database do we want to use ----------------------------------------------------- syntax :- use <db name> Ex:- use test How to create a table -------------------------- create table if not exists bank_details( age int, ...