mysql database DDL, DML
1 DDL syntax
Data definition language DDL is an abbreviation of operating table database
create drop alter
Create Delete Modify
1 Create a database
create datebase dbname;
① use database
use dbname
② View all databases
show datebases;
③ View Database Details
show create database dbname ;
2 Delete the database
drop datebase dbname;
3 Create a table
create table tablename (
clo_name clo_type,
...
);
4 Delete table
drop table tablename;
See Table 5 details
Methods ①
desc table names;
Method ②
show create tablename \G;
6 Modify table properties
① modify the table name
rename tabename to newtablename ;
② field type changes to the table
alter tabel tablename modify clo_name clo_type
③ modified field name field attributes table, while changes to the table, and
alter table tablename change clo_name clo_new_name clo_type
# Note change modify field types can be modified, but change the field name written twice, and modify field names can not be changed, only change field properties
# Change using the change when the field names must also bring field attributes such alter table tablename change clo_name new_clo_name clo_type
④ increase the table's field
alter table tablenam add column clo_name clo_type after|first
Field operating table
DML language
1 Add Field [field: Field]
insert into tablename (field
...) values();
2 Delete field (record)
delete form tanlename drop field where ....;
3 modify the field (recording)
update tablename set field
where fleid ;
4 query field (record)
① queries all field values
select * from tablename;
② query part of field values
select field
,.. from tablename
③ query field when the value of screening (criteria query)
select * from tablename where + [=,>, <,> =, <=,! =] Analyzing the like sentence
④ sorting and restrictions
Sort [order by: keywords sorted in ascending order by default] [desc: descending] [asc: Ascending]
* from TableName Order by SELECT
Field
[desc | ASC],
Field
2 [desc | ASC]; if the case occurs will be in accordance with the value of the second value to sort
Limit [limit: limit]
select * from tablename order by field1 [desc | asc] .. field n [desc | asc] limit n: offset limit to how many records show
⑤ polymerization operation of the recording function fun_name: count (), max (), min (), sum ()
select field fun_name from tablename ...
fun_name function:
count () counts, max () maximum value, min () minimum, sum () summation
grammar:
select [field1,field2...fieldn] fun_name from tablename [where + ] group by + [with rollup] + [having]
group by group
with rollup: Optional syntax is to summarize the results of the polymerization
For example: the number of statistical departments have to count the total number of
select deptno ,count(1) from emp group by deptno with rollup
having: the results of the polymerization screened again
⑥ table join
The connection: the connection between the two different tables, two tables to select records matching
Public Connected select a_table_field, b_table_field from a_table, b_table where ... a table with b
Outer join: Left connection Right connection
Connect the left and right connections
⑦ subquery
Two tables, the query results after a table, matching the query results in a table in another
Sub-queries and connections can be queried
E.g
Assume a_table and b_table have a common field id field has to be queried id of a table and the table recorded b
Written in connection
connection:
select a_tabel.* from a_tabel,b_table where a_table.id = b_table.id
##
a_table. * -----> shows the results are the results after matching all fields and a_table
Written subquery
Subquery:
select id from a_table where id in (select id from b_table)
⑧ set union, union all [union: collection]
After a set of two tables of data query [the] union all, or to re-set [union]
Suppose a, b has a table name field
grammar:
select name from a_table
union [union all]
select name from b_table;