A quick review of basic grammar MySQL
1. Install
Slightly References
2. database operations
-
View all databases
show databases; # mysql 用分号结束语句
-
Create a database
# create database [name] create database test
-
Delete Database
drop database test
-
Use Database
# use [name] use test;
3. Operating Table
-
Create a table
create table test( id int, name varchar(10), age int, gender char(2), birthday date, tel char(11), email varchar(30) );
-
Delete table
# drop table [name] drop table test;
-
Column constraints
- Primary Key
primary key
: used to uniquely identify a record field - Non-empty
not null
: The value of the modified field can not be empty - Foreign Key
foreign key
: foreign key values can only be identified from the current field of the foreign key values specified field, or null - Since growth
auto_increment
: grow automatically when you insert data fields
# create table department create table department( id int primary key auto_increment, name varchar(30) not null, tel char(11) );
CREATE TABLE class ( id int NOT NULL AUTO_INCREMENT, name varchar(10) DEFAULT NULL, depId int DEFAULT NULL, PRIMARY KEY (id), FOREIGN KEY (id) REFERENCES department(id) ON DELETE CASCADE ON UPDATE RESTRICT );
- Primary Key
-
View current database of all the tables
show tables;
-
View table structure
# desc [table_name] desc class;
-
Modify table
alter table
Content Key words grammar Add Column add alter table [table] the Add [Column Name] [type] [bound] Modify column modify alter table [table] modofy [Column Name] [type] [bound] Column Rename change alter table [table] Change [old column name] [new column name] [type] [bound] Remove Columns drop alter table [table name] drop [Column Name] Rename Table rename rename table [the old table name] to [new table name] Modify the table character set character set alter table [table name] character set [Character Set Name] rename table test to student; alter table student add grade int; alter table student modify id int primary key auto_increment; alter table student change birthday birth date; alter table student drop age;
4. Data Insert
insert into department(id,name,tel) value(1,'dep1','123456'); insert into department(name,tel) value('dep2','123456'); insert into department(name) value('dep2');# 这条记录id为3 insert into department value(4,'dep3','123456');
5. Data Delete
delete from department where id =3;
6. Data Update
update department set name='dep5' where id=4; # 使用某一列的值给其他列赋值 update department set name=tel;
7. Data Query
#===============单表查询================= # 查询student表中所有字段; select * from student; # 查询student表中特定字段; select id name from student; select name, grade from student; # 查询结果起别名 select name 姓名 ,grade 成绩 from student; #查询不重复的姓名 select distinct name from student # 查询成绩>60的学生学号,姓名和成绩 select id,name,grade from student where grade>60; # 查询总人数 select count(*) 总人数 from student; # 查询总人数,总成绩,平均分 select count(*) 总人数,sum(grade) 总成绩,sum(grade)/count(*) 平均分 from student; #查询结果排序 select * from student order by grade; select * from student order by grade desc;#降序排列 # 分组查询 ##查询男女生人数 select gender,count(*) from student group by gender; ## 查询各个分数有多少人 select grade,count(*) from student group by grade; #===============多表查询================= # 查询班级,系名,电话 select class.name, department.name, tel from class ,department where class.depId=department.id; select c.name, d.name, tel from class c ,department d where c.depId=d.id;# 使用别名 # 左连接 select c.name, d.name, tel from class c left join department d on c.depId=d.id ; # 右连接 select c.name, d.name, tel from class c right join department d on c.depId=d.id ;
4. User Management
In the default mysql database of user information mysql.user
stored in
#查看用户
select user ,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
# user :用户名
# host:主机地址
#添加用户
create user 'lisi'@'localhost' identified by '123456';
#删除用户
drop user 'lisi@localhost';
5. Rights Management
Mysql user rights in the default database information mysql.user
stored in
#查看用户权限
show grants for 'root'@'localhost';
# 授予用户权限
#grant 权限列表 on 数据库.表名 to 'user'@'host'
• grant select,update,delete on test.* to 'lisi'@'localhost'
grant all on *.* to 'lisi'@'localhost'
# 撤销权限授予
# revoke 权限列表 on 数据库名.表名 from 'username'@'host'