Basics SQL statement to explain the basis

Data definition language DDL tube in the data structure


1. Create a database
create datavase database name charset utf8

2. Display all databases
show databases;

3
jdbc calls in Java code inside the database.

4. Use the database
use the database name


5. Create a table, for storing data units. Each row is a record and each column is a field
create table table name (id int, username varchar, age int)


Table 6 shows all
show tables;


Field 7 View table
desc table name

Table 8 field applied to
alter table add table Field Name Field Type

9. The field type of modification
alter table modify table name field name field type.

10. Delete field
alter table drop table field name;

11. Modify the table name
rename table was originally called to a new name

Table 12. View details of
show create table table name

13. Modify the table character set
alter table table name charter set the character set name

15. Modify the column name

alter table table name change to the original list of new column name field type

16. Delete the table name
drop table table name

Data Management DML statements.
1. query all the data.
The SELECT * from table name \ g

2, insert data.
INSERT INTO table name (field 1, field 2, field 3) values (the value of the field 1 Field 2 Value Field ternary)

3. Update the data
update table set column name = value column where the column name column value =

4. Delete record
delete from table where column name = value; the data can be retrieved
truncate table name of the table to delete the entire table is re-established as a table data structure can not be retrieved


DQL query data
1. All statements query
select * from show
2. Query designated master data
select the specified field name from the table name  


3. Conditions query (query according to the condition)
SELECT * from table where age <= 17; more on the back where the conditions

= Equal
! = Not equal

betwen .............. and ................. queries between the ages which range
select * from stu where age beetewn 15 and 17


Age is equal to the query and select 17 and 15 * from stu where age = 17 and id = 4; two conditions
or select * from stu where age = 15 or age = 17; representing the selected one or two, as long as a conditions can
is null    

is   not null   ;

Fuzzy query
keywords like
     the SELECT * from STU the WHERE name like "Ma%"; query for all persons named Ma


Ordering; order by
ascending order of default ase is
descending is SESC
SELECT * from the STU desc Order by Age Age descending order

Aggregation function
count () statistics of the number of rows is not null
max ()
min () Minimum
AVG () average


select  max(age) from   stu;

Packet
group by field name
select * from stu group by age; age group.
HAVING more functions on a polymeric back
select * from (1) where ( 2) group by (3) having (4) order by (6) limit (7)

Page: limit int index from the beginning that, to that end.


Constraints
primary key primary key constraint is not empty and the only
unique UNIQUE
AUTO_INCREMENT increment
not null is not empty
to ensure the integrity of data


Continuous multi-table queries
things we learned before only query in a table inside, and now I want more than one table inside early inquiry 
found each student whose teacher's name, can not do the traditional single-table query of
1.99 query select student.name.name from student, teacher where student.id = teacher.sid;
links within 2
SELECT * from table 1, join in table 2 on condition;
SELECT * from the Join Teacher Student = ON student.id teacher.sid;


External link outside left and right link
is to watch the rest of the stuff left to check out

Index  
accelerate query efficiency   
if the data from the late 1000 Vision, we add to the id index
create index
create index index name on table name (field)
to delete the index
name drop index index on the table name
to view the index
show index from the table name


Analyzing the condition
in which the data indicating in what (m, n) represents the data from m to n is between
not in something that is no longer between. not in (m, n)

 

 

Here it says how to create a view. Use sql statement. Our view is like a big word, excel spreadsheet when the view of the same. Let us look at how specific code to create a view. The syntax

Create view view field names as Select queries 1, 2 field query, the query field of 3 from table where conditions;

 

Delete View

Name drop view view;

Let's look at a few simple sql statement

1. Select

select * from table1 where 范围

2. Insert

insert into table1(field1,field2) values(value1,value2)

3, delete

 delete from table1 where 范围

4. Update

update table1 set field1=value1 where 范围

5. Find

select * from table1 where field1 like '% value1%' --- like the syntax is very subtle, to find information! which is like fuzzy matching

6. Sort

select * from table1 order by field1, field2 [desc] sorted in descending order, the default is ascending

7. summation

select sum (field names) from table name a few other functions are the same, not how much.

 

II: Description of several senior operational queries.

A.unique operator.

UNION operator derives a result table by combining the results of the other two tables (e.g. TABLE1 and TABLE2) and eliminate any duplicate rows. When used in conjunction with ALL UNION (i.e. UNION ALL), not eliminate duplicate rows. In both cases, each row of the derived table is not from TABLE1 from TABLE2.

B.

EXCEPT operator

  EXCEPT operator by including all the rows in TABLE2 TABLE1 but not in the elimination of all duplicate rows and derive a result table. When used in conjunction with ALL EXCEPT (EXCEPT ALL), not eliminate duplicate rows.

C.

INTERSECT operator

  INTERSECT operator by including only TABLE1 and TABLE2 both rows and eliminate all duplicate rows derives a result table. When used in conjunction with ALL INTERSECT (INTERSECT ALL), not eliminate duplicate rows.

  Note: Use the operation of several query results word line must be the same.

 

Usually, when we should be more to write sql statement, this will sql statement used in the extreme.
 

Guess you like

Origin blog.csdn.net/hackerbaseing/article/details/90512496