Statements set names utf8; to set up a database coding, so that Chinese can be displayed properly.
Create a database:
CREATE DATABASE `database`
CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci ';
Create a table:
CREATE TABLE `database_user` (
`ID` varchar(40) NOT NULL default ' ',
`UserID` varchar(40) NOT NULL default ' ',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Create a table:
CREATE TABLE `csyzz` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age1` char(10) DEFAULT NULL,
`time` datetime DEFAULT NULL,
`author` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
Modify the table name
alter table test rename test1;
- Add table column
alter table test add column name varchar(10);
- Delete table column
alter table test drop column name;
或
alter table family drop age
Modify the table column names age is age1;
alter table ceshishuju change column age age1 varchar(30);
- Modify the table column type
alter table test modify address char(10)
或
alter table test change address address char(40)
Modify a table column type of syntax:
ALTER modify table Table name for the new type of the new column parameters;
alter table family modify age varchar
If you want to add one, and located at the head of the table, with First
ALTER add table column name Table Column Type First column parameters;
alter table m1 add pid int not null first;
adding data
insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
Adding data columns to a particular
insert into Info (code,name) values('p010','李四');
Since the processing of the growth column
insert into family values('','p001','数据','T001','数据',1);
insert into table values (value)
Deleting Data
Delete All Data
delete from family
Delete specific data
delete from Info where code='p001'
delete from table where conditions
Modify data
modify all data
update Info set name='徐业鹏'
Modify certain data
update Info set name='吕永乐' where code='p002'
Modify multiple columns
update Info set name='吕永乐',sex=1 where code='p003'
update table name set to modify the content where conditions
Read data
(1) simple to read, query all columns (*) all rows (no added conditions)
select * from Info
(2) reading a particular row
select code,name from Info
(3) Conditions inquiry
select * from Info where code='p003'
(4) multi-criteria query
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5) keyword queries (fuzzy query)
to check all cars include Audi
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
Check to 'crown' at the beginning of all cars
select * from car where name like '皇冠%';
Car name query in the second character is 'horse' in
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6) Sorting Query
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
In ascending brand press, and then in descending order according to the price
select * from car order by brand,price desc
Range query
select * from car where price> 40 and price <60 - Check rates between 40-60
select * from car where price between 40 and 60 --between...and...
Discrete query query discrete values, such as queries car prices is an integer of 50, 60, etc.
select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)
select * from car where price not in(30,40,50,60) --价格除30,40,50,60以外的数的
Aggregate functions (statistical information)
select count(*) from car --查询这张表里面有多少数据。count方法可以用来求条数
select count(code) from car #取所有的数据条数。code为主键,内容不为空,可以用code查询。
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值
Data Query principle: the database query data when each piece of data will be checked again, when queried if the condition is true, it returns the data true, if not true, returns false. If the return is true, we will select this data, if it was false, then pass out of this data. For example, if the select * from car where 1 = 1, the output will be all the data table, since the condition 1 = a constant.
Paging query each page has a lot of data, you can view data on the next page.
select * from car limit 0,10 #分页查询,跳过几条数据(0) 取几条(10)
A predetermined number of the displayed page: m
Pages: n
select * from car limit (n-1)*m,m 取第n页显示的m条数据
To re-query
select distinct brand from car --distinct表示去重,前面代码表示对brand列去重查询
To re-query for a check, check 2 is not suitable for use.
Grouped query
query the number of cars in the table, under each series of the car. Grouped by brand.
select brand,count(*) from car group by brand
After grouping, only query the column or aggregate functions
Take the average is greater than the price series code series 40
select brand from car group by brand having avg(price)>40
This series of fuel is greater than the maximum value taken 8 series code
select brand from car group by brand having max (oil)> 8