Basic Operations database MySQL (a)

A. With regard to user management operations

1. Create a user

create user '用户名'@‘ip地址’ identified by '密码' ;

2. Delete user

drop user '用户名'@‘ip地址’;

3. Modify the user

rename user '用户名'@‘ip地址’; to '新用户名'@‘新ip地址’;;

4. Change Password

set password for '用户名'@'ip地址' = password('新密码');

Note: where% indicates any number ip address as '%', '192.168%.'.

II. User rights management operations

1. Check the user rights

show grant for '用户'@'ip地址'

2. Authorizes

grant 权限 on 数据库.表 to ‘用户名’@‘ip地址’;

3. Cancellation rights

revoke 权限 on 数据库.表 from '用户名'@'ip'地址

Note: 1 database table can be written * * indicates the relevant authority to grant permission to all databases.

权限的种类:
			all privileges  除grant外的所有权限
            select          仅查权限
            select,insert   查和插入权限
            ...更多权限可以查看手册

III. Display, create, use a database

1. Display Database

show databases;

2. Use the database (open folder)

use  数据库名称;

3. Create a database

create databases db default charset UTF-8;
创建一个数据库,并将其编码格式设置为UTF-8

IV. Operating Table

1. Create a table

create table 表名(
		列名	类型	是否可以为空,
		列名	类型	是否可以为空,
)engine=innodb default charset=UTF-8
举例:
create table t1(
		列名 类型 null,
		列明 类型 not null,
		列名 类型 not null default 1,
		列名 类型 not null(null) auto_increment primary key,
		id int,
		num decimal(10,5)	//前面是小数点前后的总位数,后面代表小数点后面的数
		name char[10]) engine=innodb default charset = UTF-8;

Note: null represents null, not null represents a non-empty. default 2 represents a set defaults

(1) primary key
definition: a special kind of unique index, allow nulls, if a single primary key column, its value must be unique, if a plurality of columns, it must be unique combination

例如:
定义语句: name char(10),id int
设置主键: id int primary key 一个属性作为主键
		  primary key(nid,num)两个属性组合起来作为主键

(2) the foreign key
is defined: If the public key is the primary key in a relationship, then the public key is called a foreign key relationship to another.

定义语句:
	constraint 名称 foreign key (color_id) references color(nid)
	color_id为外键,nid为color表中的主键,color为被参照表。

(3) increment
defined: If set to auto-increment to a column, this column without having to insert the data set, the default will increment (table can only have one additional)

id int not null auto_increment primary key,

Note: set to auto-incremented primary key columns must

2. Delete table

drop table 表名;

3. Empty Table

delete table 表名			//不清空自增,添加元组时,自增列的值接清空前
truncate table 表名			//同时清空自增列

Table 4. Modify

添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:alter table 表名 change 原列名 新列名 类型; 

V. contents (CRUD)

1. increase

insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表

2. deleted

delete from 表 where id=007 and name='嘿嘿嘿'

3. change

update 表 set name = 'lihua' where age<18

4. Charles

select nid,name,gender as gg from 表 where id > 1
select * form 表			//表示查询该表中的所有数据

VI. Data Types

1. Digital Type

整数类型:
1.tinyint	2.int	3.bigint	4.decimal
浮点数类型:
1.float		2.double
字符类型:
1.char		2.varchar	3.text
枚举类型:
1.enum
集合类型:
1.set
时间类型:
1.Date 年月日	2.time 时分秒	3.year 年

1.num decimal (10,5) // total number of digits in front after the decimal point, the number after the decimal point behind the representative
2.char (10) regardless of the length is not equal to the length 10 will account for 10
3.varchar (10) in accordance with actual allocated, up to 10, to save space, speed is not fast char
4.size ENUM ( 'x-small' , 'small', 'medium', 'large', 'x-large') can only be selected when data is inserted ENUM is a
5.col SET ( 'a', ' b', 'c', 'd'); time survey data may be any combination of SET

Replenishing operation:

Conditions Operation:

select * from 表 where id >1 and name !='alex' and num = 12;
select * from 表 where between 5 and 16;
select * from 表 where id in (22,12,1);
select * from 表 where id not in (11,2,1);
select * from 表 where id in (select nid from 表)

Tsuhaifu:

select * from 表 where name like '%'	其中%代表多个字符
select * from 表 where name llike '_'  其中_代表一个字符

Restrictions (mainly for paging):

select * from 表 limit 5;
select * from 表 limit 10,10	表示从第10行开始的后面10行;
select * from 表 limit 10 offset 10 效果同上

Sequence

select * from 表 order by 列 asc 根据列从小到大排序
select * from 表 order by 列1 asc,列2 asc 根据列1从小到大排序,如果有相同,再根据列2从小到大排序
asc:从小到大,desc:从大到小

Grouping:

select * from 表 group by xx;
select * from 表 group by xx having xx>xx;
分组常与聚集函数组合使用,并且group by 必须放在where之后,group by 之前

Even tables:
left and right even table: the Join (automatic de-emphasis)

方法一:
select A.name,B.name from A,B where A.num = B.num;
方法二:
1.inner join
select A.name,B.num from A inner join B on A.num = B.num;
当A.num与B.num无对应关系则不显示
2.left join
select A.name,B.num from A left join B on A.num = B.num;
A表全部显示,A表存在,但B表不存在的显示空
3.right join
select A.name,B.num from A right join B on A.num = B.num;
B表全部显示,B表存在,但A表不存在的显示为空	

Even down table: union (automatic de-emphasis) union all (not weight)

Guess you like

Origin blog.csdn.net/w819104246/article/details/91126654