Database Create:
create database patientdb;
View:
show databases;
SHOW DATABASES lists the databases on the MySQL server host
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | patientdb | | performance_schema | | sakula | | sql_hr | | sql_inventory | | sql_invoicing | | sql_store | | studentdb | | sys | | world | +--------------------+ 12 rows in set (0.0017 sec)
show create database patientdb;
Select database:
The USE statement tells MySQL to use the named database as the default (current) database for subsequent statements.
use patientdb;
Drop:
drops all tables in the database and deletes the database
drop database patientdb;
Create Table:
create table student_table(
stud_id int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
age int NOT NULL,
PRIMARY KEY(stud_id)
);
view table:
Display all the tables present in the database.
show tables;
Insert record:
The INSERT INTO
statement is used to insert new records in a table.
It is possible to write the INSERT INTO
statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
values (1101,'tom',23);
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO
syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
insert into student_table
values
(1104,'sam',22),
(1106,'jack',21),
(1107,'john',21);
View records:
Display all the records.
select * from student_table;
Replace
123,xyz street,
update student_table
set address = REPLACE(address,'xyz','abc')
where stud_id = 1101;
Delete record --> limit clause:
delete from student_table where id=1102;
delete from student_table;
Delete all the records of the table
delete from student_table order by stud_id limit 2;
Delete first two records
Select column:
select name from student_table;
select name,age from student_table;
Having clause -->
select * from student_table group by name having age > 30;
+-----+---------+------------+-----+
| id | name | occupation | age |
+-----+---------+------------+-----+
| 101 | Peter | Engineer | 32 |
| 104 | Stephen | Scientist | 45 |
+-----+---------+------------+-----+
Add new column:
alter table student_table add column city varchar(30) NOT NULL;
alter table student_table
add column stud_class varchar(30) default null after age,
add column stud_marks int default null after age;
alter table student_table
change column name stud_name int;
Drop a column:
alter table student_table
drop column stud_marks,
drop column stud_class;
Index in sql:
The CREATE INDEX
statement is used to create indexes in tables.
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.
CREATE INDEX Syntax
Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
consider table --> 1 million records
student named michael?
select * from table where name = 'michael'
indexing --> clustered index and non-clustered index
create index stud_index ON student_table(stud_name);
GROUP BY:
The GROUP BY
statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY
statement is often used with aggregate functions (COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
) to group the result-set by one or more columns.
select name,count(age) as n_age from student_table GROUP By name;
Order by:
The ORDER BY
keyword is used to sort the result-set in ascending or descending order.
The ORDER BY
keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC
keyword.
select stud_id,name from student_table order by age asc;
select stud_id,name,age from student_table where stud_id>1103 order by age desc;
Insert on duplicate key update:
insert into student_table(stud_id,name,age) values (1108,'tom',23) on duplicate key update name = 'ramesh';
Comments
Post a Comment