创建数据库 mysql> create database testdb; mysql> create database if not exists testdb; mysql> create schema if not exists student character set 'gbk' collate 'gbk_chinese_ci'; 删除数据库 mysql> drop database testdb;
- CREATE TABLE [if not exists] tb_name(col_name,col_definstion,constraint)
- 创建表
- mysql> create table tb (id int unsigned not null auto_increment primary key,Name char(20)
- not null,Age tinyint not null);
- mysql> create table tb (id int unsigned not null auto_increment,Name char(20) not null,Age
- tinyint not null,primary key(id));
- mysql> create database mydb;
- mysql> use mydb;
- mysql> create table students(name char(20) not null,age tinyint unsigned,gender char(1)
- not null);
- mysql> create table courses(ID tinyint unsigned not null auto_increment primary key,Couse
- varchar(50) not null);
- mysql> create table courses(name char(20) not null,age tinyint unsigned,gender char(1)
- not null); ---从一张表中查出需要的数并创建为一个新表,但是很多字段的属性没有存在,需要自己在重新定义
- mysql> create table testcourses select * from courses where CID <=2;
- 以其它表为模板,创建一个新表,字段的属性还会存在
- mysql> create table test like courses;
- 删除表:DROP TABLE tb_name;
- mysql> drop table testcourses;
- ALTER TABLE tb_name;
- mysql>alter table students change course Course varchar(100) after name;
- mysql>alter table students add course varchar(100);
- 向表中插入数据
- insert into tb_name (col,col2,....) values (val1,val2,....);
- insert into tutors (Tname,Gender,Age) values ('jerry','M',24); -----批量插入方式
- insert into tutors set Tname='Tom',Genser='F',Age=30; -----只能实现单个字段插入
- insert into tutors (Tname,Gender,Age) select Name,Genser,Age from students where Age >=20
- select * from tutors order by TID desc limit 1; -----查看降序的第一行
- select last_insert_ID(); -----查询插入的最后一个序列号
- 更改数据库
- UPDATE tb_name SET column=value where
- mysql>update students set Course='wg' where Name='j'; -----更改j的课程为wg
- 删除表中的某一字段
- DELETE FROM students WHERE Course='';
- mysql>delete from students where Course='wg';
CREATE USER 'USERNAME'@'HOST' IDENTIFIED BY 'PASSWORD'; mysql> create user 'jerry'@'%' identified by 'jerry'; ------创建用户 修改用户密码的方法 1) mysql> SET PASSWORD FOR 'USERNAME'@'HOST'=PASSWORD('NEW_PASSWORD'); 2) mysqladmin -uUSERNAME -hHOST -p password 'password' ------不进入mysql修改密码 mysqladmin -uroot -h127.0.0.1 -p passwd'123456' 3) mysql> UPDATE user SET Password=PASSWORD('password') WHERE USER='root' AND Host='127.0.0.1'; UPDATE user SET Password=PASSWORD('123456') WHERE USER='root' AND Host='127.0.0.1'; 给用户授权 GRANT pri1,pri2,......ON DB_NAME.TB_NAME TO 'USERNAME'@'HOST' [IDENTIFIED BY 'password'] mysql> grant all privileges on test.* to 'jerry'@'%'; -----给用户所有权限 REVOKE pri1,pri2,.....ON DB_NAME.TB_NAME FROM 'USERNAME'@'HOST'
SHOW GRANTS FOR 'USERNAME'@'HOST' ----- 查看用户的授权 mysql> show databases; -----查看数据库 mysql> desc students; -----查看一张表的结够 mysql> show grants for 'jerry'@'%'; -----查看创建用户的信息 mysql> show character set; -----查看当前服务器所支持的字符集 mysql> show collation; -----查看排序规则 mysql> show engines; -----查看数据库中的所有引擎 mysql> show table status like 'user'; -----查看一张表的状态信息(横向显示的) mysql> show table status like 'user'\G: -----查看一张表的状态信息(纵向显示)
- 简单语句查询
- select * from students; ------显示表中的所有内容
- select Name,Age from students; ------显示students表中的Name和Age列
- select distict Gender from students; ------相同的内容只显示一次
- 选择students表中年龄大于20的同学(以下三种方式):
- select * from students where Age>=20;
- select Name,Age from students where Age>=20;
- select Name,Age from students where Age+1>20;
- 查找年龄大于20的同学并且按降序排列:
- select Name,Age from students where Age>20 order by Age desc;
- 年龄大于等于20并且是男性的同学:
- select Name from students where Age>20 and Gender='M';
- 年龄不大于20的同学:
- select Name,Age,Gender from students where not Age>20;
- 小于等于20的女同学:
- select Name,Age,Gender from students where not (Age>20 or Gender=‘M’);
- 年龄在(21-24)之间的同学(以下两种方式):
- select Name,Age from students where Age>20 and Age<25;
- select Name,Age from students where Age between 20 and 25;
- 显示以Y开头的名称(这里限定了姓名的长度)("_"表示任意单个字符):
- select Name from students where Name like 'Y___';
- 显示以Y开头的姓名:
- select Name from students where Name like 'Y%';
- 名称中含有ing的名称(“%”表示任意长度的任意字符):
- select Name from students where Name like '%ing%';
- 显示以M或N或Y开头的名字(支持正则表达式):
- select Name from students where Name rlike '^[MNY].*$';
- 显示年龄是18、20、25的同学:
- select Name from students where Age IN (18,20,25);
- 显示挑选课程号(CID1)为空的同学:
- select Name from students where CID1 is null;
- 把查询后的结果进行降序排序(ASC升序,desc降序)
- select Name,CID1 from students where CID1 is not null order by CID1 desc;
- select Name AS Student_Name from students;显示查询的Name表头名变为name
- 隔两行数据向后取三行数据:
- select Name from students limit 2,3;
- 所有同学的平均年龄:
- select AVG(age) from students;
- 显示年龄最大的同学:
- select MAX(age) from students;
- 显示年龄最小的同学:
- select MIN(age) from students;
- 显示所有同学的年龄总和:
- select SUM(age) from students;
- 显示所有同学的个数:
- select count(age) from students;
- 显示所有男同学的平均年龄:
- select AVG(age) from students where Gender=’M‘;
- 显示所有女同学的平均年龄:
- select AVG(age) from students where Gender=’F‘;
- 显示男女同学的平均年龄:
- select Gender,avg(age) from students group by Gender;
- 显示选修CID1的同学
- select count(CID1) AS Persons,CID1 from students group by CID1;
- 显示选修人数大于2的课程:
- select count(CID1) AS Persons,CID1 from students group by CID1 having Persons>=2;
- 多表查询
- 每位同学及其他所学习的课程名称(以下四种方式)
- select students.Name,courses.Cname from students,courses where students.CID1=courses.CID;
- select s.Name,c.Cname from students AS s,courses AS c where s.CID1=c.CID;
- select s.Name,c.Cname from students AS s left jion courses AS c on s.CID1=c.CID;(左连接)
- select s.Name,c.Cname from students AS s right jion courses AS c on s.CID1=c.CID;(右连接)
- 显示各个同学与他相对应的导师:
- select c.Name as student,s.Name as teacher from students as s,students as c where
- s.SID=c.TID;
- 显示每一位老师及其所教授的课程;没有教授的课程保持为NULL:
- select t.Tname,c.Cname from tutors as t left join courses as c on t.TID=c.TID;
- 显示每一个课程及其相关的老师,没有老师教授的课程将其老师显示为空:
- select t.Tname,c.Cname from tutors as t right jion courses as c on t.TID=c.TID;
- 显示每位同学CID1课程的课程名及其讲授了相关课程的老师的名称:
- select Name,Cname,Tname from students,courses,tutors where students.CID1=courses.CID
- and courses.TID=tutors.TID;
- 查看同学的成绩及姓名,并且按升序排列:
- select students.Name,scores.Score from students,scores where students.SID=scores.SID
- order by scores.Score desc;
- 子查询
- 挑选出courses表中没有被students中的CID2学习的课程的课程名称:
- select Cname from courses where CID not IN (select CID2 from students where
- CID2 is not null);
- 挑选出没有教授任何课程的老师,每个老师及其所教授课程的对应关系在courses表中:
- select Tname from tutors where TID not in (select distinct TID from courses);
- 找出students表中CID1有两个或两个以上同学学习了的同一个门课程的课程名称:
- select Cname from courses where CID in (select CID1 from students group by CID1
- having count(CID1) >=2);
- 年龄大于平均年龄的同学:(使用子查询时,子查询只能返回单个值):
- select Name,Age from students where Age > (select avg(age) from students);
- 查询学生和老师各自的年龄并写在一个表中:
- (select Name,Age from students) union (select Tname,Age from tutors);
上面的就是关于MySQL的一些基础性总结,如果其中不对的地方还请大家指出