DDL(Data Definition Language):数据库定义语句;
DML(Data Manipulation Language):用于增删改数据库中数据,不包括查询;
DCL(Data Control Language)用来向用户赋予/取消对数据对象的控制权限;
TCL(Transaction Control Language):用来对事务进行管理
数据库登录:
win+R运行cmd,输入 双引号里面的内容:"mysql -uroot -proot"回车,root为用户名和密码。
注意:执行win命令,最后不需要加“;”,但执行mysql语句则需要加“;”
1.创建名为"mydb1"数据库:
create database mydb1;
2.显示所有数据库:
show databases;
3.创建名为"mydb1"并带字符集的数据库:
create database mydb2 CHARACTER SET=utf8;
4.显示数据库mydb2创建语句:
show create database mydb2;
5.如果存在数据库mydb2,则删除mydb2(如果不加上if exits判断,一旦要删除的数据库不存在,就会报错):
drop database if exits mydb2;
6.修改数据库的库字符编码:
alter database mydb2 character set gb2312;
7.数据库备份(root是用户名,这是一个win命令(即打开cmd,直接输入),将数据库备份到c盘下):
mysqldump -u root -p mydb2>c://test.sql;
8.数据库恢复(两种方式):
(1)这是一个win命令:mysql -u root -p mydb2<c://test.sql
(2)source c://test.sql;
1.创建学生表:
create table student( /* 直接指定主键,也可单独指定*/ sno varchar(4) primary key, sname varchar(10) not null, sage int, ssex char(2), email varchar(20) unique, constraint ck_student_ssex_sage check(ssex in('男','女')and sage between 10 and 50) )
2.增加列(字段):
alter table tableName add columnName varchar(30);
3.修改列,改变长度:
alter table tableName modify cloumnName varchar(60);
4.删除列:
alter table tableName drop column cloumnName;
5.修改表名:
rename table tableName_old to tableName_new;
6.修改表的字符集:
alter table user character set gbk;
7.修改列名:
alter table tableName change column cloumnName columnName_new varchar(60);
8.删除表:
drop table tableName;
准备表
create table employee ( id int, name varchar(40), sex varchar(4), birthday date, entry_date date, salary decimal(8,2), resume text );
1.插入数据:
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer'); /*可以省略表字段,但是必须插入全部字段*/ insert into employee values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
2.指定某些列插入数据:
insert into employee(id) values(6);
3.查看汉字时不乱码:
insert into employee(id,name) values(6,'张三'); /*告诉mysql客户采用gb2312编码*/ show variables like 'chara%'; set character_set_client=gb2312; insert into employee(id,username) values('3','张三'); /*查看汉字时不乱码*/ show variables like 'chara%'; set character_set_results=gb2312; select * from employee;
4.删除表数据:
/*删除表中名称为’zs’的记录*/ delete from employee where name='zs'; /*删除表中所有记录*/ delete from employee; /*使用truncate删除表中记录(快速删除表中所有数据,保留表的数据结构)*/ truncate table employee;
5.修改表中数据:
/*将所有员工薪水修改为5000元*/ update employee set salary=5000; /*将姓名为’zs’的员工薪水修改为3000元*/ update employee set salary = 3000 where name='zs'; /*将姓名为’aaa’的员工薪水修改为4000元,job改为ccc*/ update employee set salary = 4000,job='ccc' where name='aaa'; /*将lisi的薪水在原有基础上增加1000元*/ update employee set salary = salary+1000 where name='lisi';
6.查询表中数据:
/*查询表中所有学生的信息*/ select id,name,chinese,english,math from student; /*查询表中所有学生的姓名和对应的英语成绩*/ select name,english from student; /*过滤表中重复数据*/ select distinct english from student; 在所有学生分数上加10分特长分*/ select name,(chinese+english+math)+10 from student; /*统计每个学生的总分*/ select name,(chinese+english+math) from student; /*使用别名表示学生分数*/ select name,(chinese+english+math) as 总分 from student; /*使用别名表示学生分数,可以不用as*/ select name,(chinese+english+math) 总分 from student; /*查询姓名为张三的学生成绩*/ select * from student where name='张三'; /*查询英语成绩大于90分的同学*/ select * from student where english>'90'; /*查询总分大于200分的所有同学*/ select name,(chinese+english+math) 总分 from student where chinese+english+math>200; /*查询英语分数在 80-90之间的同学*/ select * from student where english>=80 and english=<90; select * from student where english between 80 and 90; /*查询数学分数为89,90,91的同学*/ select * from student where math=89 or math=90 or math=91; select * from student where math in(89,90,91); /*查询所有姓李的学生成绩*/ select * from student where name like '李%'; select * from student where name like '李_'; /*查询数学分>80,语文分>80的同学*/ select * from student where math>80 and chinese>80; select * from student where chinese is null; /*对数学成绩排序(降序:从高到低,desc)后输出。*/ select name,math from student order by math desc; /*对数学成绩排序(升序:从低到高,asc)后输出。*/ select name,math from student order by math asc; /*对总分排序后输出,然后再按从高到低的顺序输出*/ select name,math+english+chinese from student order by math+english+chinese desc; /*对姓李的学生成绩排序输出*/ select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc; /*统计一个班级共有多少学生*/ select count(*) from student; select count(id) from student; /*统计数学成绩大于80的学生人数*/ select count(*) from student where math>80; /*统计总分大于250的人数*/ select count(*) from student where math+english+chinese>250; /*细节 null不能被count*/ select count(chinese) from student; /*统计一个班级数学总成绩*/ select sum(math) from student; /*统计一个班级语文、英语、数学各科的总成绩s*/ elect sum(math),sum(english),sum(chinese) from student; /*统计一个班级语文、英语、数学的成绩总和*/ select sum(math+english+chinese) from student; /*统计一个班级语文成绩平均分*/ select sum(chinese)/count(*) from student; /*求一个班级数学平均分*/ select avg(math) from student; /*求一个班级总分平均分*/ select avg(math+english+chinese) from student; /*求班级最高分和最低分(数值范围在统计中特别有用)*/ select max(math+english+chinese) from student; select min(math+english+chinese) from student; /*对订单表中商品归类后,显示每一类商品的总价*/ select product,sum(price) from orders group by product; /*查询购买了几类商品,并且每类总价大于100的商品*/ select product,sum(price) from orders group by product having sum(price)>100;
1.定义主键约束:
create table demo1( id int primary key, name varchar(40) );
2.插入空id:
insert into demo1(name) values('aaa');/*报错,主键不能为空*/
3.插入重复的id:
insert into demo1(id,name) values(1,'aaa'); /*报错,主键不能重复*/ insert into demo1(id,name) values(1,'aaa');
4.显定义主键自动增长(auto_increment):
create table demo2( id int auto_increment primary key, name varchar(40) );
5.定义唯一约束(unique):
create table demo3( id int auto_increment primary key, name varchar(40) unique /*unique:唯一约束*/ );
6.定义非空(not null):
create table demo4( id int auto_increment primary key, name varchar(40) not null );
7.定义外键约束():
create table department( id int auto_increment primary key, name varchar(30) not null ); create table employee2( id int auto_increment primary key, name varchar(30) not null, salary double, department_id int, /*外键约束*/ constraint department_id_FK foreign key(department_id) references department(id) );
6.定义非空(not null):
create table demo4( id int auto_increment primary key, name varchar(40) not null );
7.删除主键:
alter table tablename drop primary key;
//INDEX(普通索引) ALTER TABLE table_name ADD INDEX index_name (column_name); //添加多列索引 ALTER TABLE table_name ADD INDEX index_name (column_name1,column_name2,column_name3 ); //添加FULLTEXT(全文索引) ALTER TABLE table_name ADD FULLTEXT (column_name); //添加 UNIQUE ALTER TABLE table_name ADD UNIQUE (column_name); ALTER TABLE table_name ADD PRIMARY KEY (column_name);
CREATE INDEX index_name ON table_name (column_name); CREATE UNIQUE INDEX index_name ON table_name (column_name);table_name、index_name和column_list具有与ALTER TABLE语句中相同的含义,索引名不可选。另外,不能用CREATE INDEX语句创建PRIMARY KEY索引。
ALTER TABLE students ADD PRIMARY KEY (sid);
DROP INDEX index_name ON talbe_name; ALTER TABLE table_name DROP INDEX index_name; ALTER TABLE table_name DROP PRIMARY KEY;其中,前两条语句是等价的,删除掉table_name中的索引index_name。