库
1.查看数据库命令
show databases;
2.创建数据库命令
create database longhuang; < longhuan是新建库的名字 >
3.使用数据库(进入数据库)的命令
use longhuang; < longhuan是库的名字 >
4.删除数据库的命令
drop database longhuang; < longhuang是库的名字 >
表
1.查看数据库中表的命令
show tables;
2.创建新的表命令
create table 生物录(名称 varchar(60),种类 varchar(60),年龄 varchar(60),食物 varchar(60))character set=utf8;
<创建中文的数据表,非中文可以不加‘character set=utf8’>
3.查看表结构
describe jj(表的名子);
4.删除数据表的命令
drop table 录; <录是要删除表的名字>
5.要往数据表里插入数据的命令
insert into 生物录(名称,种类,年龄,食物)values('猪','哺乳动物','2','杂食'); <所有符号都是英文下的>
insert into 生物录(名称,种类,年龄,食物)values('鸡','卵生动物','2','杂食');
6.查看表的内容的命令
select * from 生物录;
7.设置条件进行查询的命令
(1)只查询杂食动物的名称,种类,年龄的命令
select 名称,种类,年龄 from 生物录 where 食物='杂食';
(2)查询年龄大于等于5的动物的命令
select 名称,种类,食物 from 生物录 where 年龄>='5';
(3)添加表字段
alter table 虎 add 序列号 varchar(10) not Null;
(4)将表中的字段设置为主键
添加主键:Alter table 虎 add primary key(序列号);
删除主键:alter table 虎 drop primary key;
(5)添加表字段设置为主键并让其成为自曾(id)
alter table 表名 add id int unsigned not Null auto_increment primary key;
(6)修改指定字段为自曾id
alter table 虎 change 序列号 id int(20) primary key auto_increment;
alter table 表名 add id int unsigned not Null auto_increment primary key;
(7)修改某个表的字段类型及指定为空或非空
alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];
alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
(8)如果要删除某一字段
alter table 虎(表名) drop 序列号(要删除的字段);
8.修改某一条数据
update 虎(表名) set sex='男' where id=4;<id为主键>
9.删除一条数据
delete from 虎(表名) where id=5;<id为主键>
and(且),or(或),between
1. and 且
select * from 表名 where date>'1988-1-2' and date<'1988-12-1';
2. or 或
select * from 表名 where date<'1988-11-2' or date>'1988-12-1';
3.between
select * from 表名 where date between '1988-1-2' and '1988-12-1';
4 in 查询制定集合内的数据
select * from student where id in (1,3,5);
5.排序 asc 升序 desc 降序
select * from 虎(表名) order by id asc;
#分组查询 #聚合函数
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #统计表中总数
select count(sex) from student; #统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~
select sum(id) from student;
#查询第i条以后到第j条的数据(不包括第i条)
select * from student limit 2,5; #显示3-5条数据
分组查询
select name,max(age) from c group by sex; #按性别分组查年龄最大值
聚合函数
select min(age) from c;
select avg(age) as '平均年龄 ' from c;
select count(*) from c; #统计表中数据总数
select sum(age) from c;
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table c rename to a;
#修改表中某个字段的名字
alter table tablename change columnname newcolumnname type; #修改一个表的字段名
alter table test change name uname varchar(50);
#表position 增加列test
alter table position add(test char(10));
#表position 修改列test
alter table position modify test char(20) not null;
#表position 修改列test 默认值
alter table position alter test set default 'system';
#表position 去掉test 默认值
alter table position alter test drop default;
#表position 去掉列test
alter table position drop column test;
#表depart_pos 删除主键
alter table depart_pos drop primary key;
#表depart_pos 增加主键
alter table depart_pos add primary key PK_depart_pos
(department_id,position_id);
#用文本方式将数据装入数据库表中(例如E:/mysql.txt)
load data local infile "E:/mysql.txt" into table MYTABLE;