码迷,mamicode.com
首页 > 数据库 > 详细

mysql基础

时间:2018-10-21 01:02:18      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:.sql   utf8   inner   specified   根据   行操作   进入   names   作文件   

1.mysql安装
windows
-可执行文件
-压缩包
-放置任意目录
-初始化
1.1 管理员命令进入mysql bin目录
-初始化
mysqld --initialize
-启动服务端
D:\mysql\mysql-5.6.41-winx64\bin\mysqld
-启动客户端
D:\mysql\mysql-5.6.41-winx64\bin\mysql -u root -p 密码为空

C:\Users\lenovo>D:\mysql\mysql-5.6.41-winx64\bin\mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

2 添加环境变量
D:\mysql\mysql-5.6.41-winx64\bin
关闭终端关闭进程
2.1 启动服务端
mysqld
2.2 启动客户端
mysql -u root -p

3 windows服务
以管理员身份运行cmd命令
D:\mysql\mysql-5.6.41-winx64\bin\mysqld --install
D:\mysql\mysql-5.6.41-winx64\bin\mysqld --remove
"D:\mysql\mysql-5.6.41-winx64\bin\mysqld" --install

以管理员身份运行cmd命令启动mysql服务
启动:net start mysql
停止:net stop mysql
以用户运行cmd命令进入客户端
mysql -u root -p
密码修改:set password=password(‘123‘)

4 关于连接
文件夹-数据库
文件-表
数据行-行
用户:root
查看用户信息:
show databases;
use mysql;
show tables;
select user from user;
创建用户:
create user ‘test‘@‘ip‘ identified by ‘123‘;
create user ‘test‘@‘192.168.%‘ identified by ‘123‘;
create user ‘test‘@‘%‘ identified by ‘test‘;
授权:
grant 权限 to wzh;
grant select,insert,update on db1.* to ‘test‘@‘%‘;
grant all privileges on db1.* to ‘test‘@‘%‘; --所有权限

sql语句
操作文件夹
create database db2 default charset=utf8;
show databases;
drop database db2;

操作文件
show tables;
create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10)) engine=innodb default charset=utf8; --加入引擎
#innodb支持回滚操作 支持事务操作
crate table t(
列名 类型 null,
列名 类型 not null,
列名 类型 not null default 1, --默认值
列名 类型 not null auto_increment primary key --自增 是自增必须是一个key 一个表只能一个自增列
)engine=innodb default charset=utf8

数据类型:
数值
tigyint
int
bigint
float
double 不太精准的小数
decimal 精准的 底层是按字符串存储
字符串
char(10) 速度快 定长的数据类型放前面
varchar(10) 节省空间 速度没有char快
-后面都需要加长度
-最高255字符
text 65535
longtext
上传文件:
存储在硬盘,数据库中存路径
时间类型
date
time
year
datetime
timestamp
枚举类型 enum
集合类型 set

操作文件中内容
select * from t1;
插入数据,插入中文时会乱码
insert into t1(id,name) values(1,‘A‘);
insert into t1(id,name) values(1,‘测试‘);

解决中文乱码问题:
在创建时设置字符集 default charset utf8

清空表内容:
delete from t1;
truncate table t1;
删除表:
drop table t1;

5 增删改查
insert
delete
update
select

6 外键
emp表 dept表
create table emp(
id int auto_increment primary key,
name varchar(50),
dept_id int,
constraint fk_emp_dept foreign key(dept_id) references dept(id)
)engine=innodb default charset=utf8;

create table dept(
id int auto_increment primary key,
dname varchar(50)
)engine=innodb default charset=utf8;


mysql> create table dept(
    -> id int auto_increment primary key,
    -> dname varchar(50)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.23 sec)

mysql> create table emp(
    -> id int auto_increment primary key,
    -> name varchar(50),
    -> dept_id int,
    -> constraint fk_emp_dept foreign key(dept_id) references dept(id)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.27 sec)

mysql> insert into dept(dname) values(sales);
Query OK, 1 row affected (2.05 sec)

mysql> insert into dept(dname) values(analysis);
Query OK, 1 row affected (0.41 sec)

mysql> select * from dept;
+----+----------+
| id | dname    |
+----+----------+
|  1 | sales    |
|  2 | analysis |
+----+----------+
2 rows in set (0.00 sec)

mysql> insert into emp(name,dept_id) values(A,1);
Query OK, 1 row affected (2.06 sec)

mysql> select * from emp;
+----+------+---------+
| id | name | dept_id |
+----+------+---------+
|  1 | A    |       1 |
+----+------+---------+
1 row in set (0.00 sec)

mysql> insert into emp(name,dept_id) values(A,3);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db2`.`emp`, CONSTRAINT `fk_emp_dept` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`))

查看表创建

show create table emp \G;

自增设置

mysql> create table t2(id int auto_increment primary key,num int)engine=innodb default charset=utf8;
Query OK, 0 rows affected (2.30 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> insert into t2(num) values(1);
Query OK, 1 row affected (0.08 sec)

mysql> insert into t2(num) values(2);
Query OK, 1 row affected (0.07 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> insert into t2(num) values(3);
Query OK, 1 row affected (2.06 sec)

mysql> insert into t2(num) values(4);
Query OK, 1 row affected (0.06 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> delete from t2 where id=3;
Query OK, 1 row affected (0.70 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> insert into t2(num) values(5);
Query OK, 1 row affected (0.12 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table t2 AUTO_INCREMENT=10;
Query OK, 0 rows affected (2.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> insert into t2(num) values(6);
Query OK, 1 row affected (2.05 sec)

mysql> show create table t2 \G;
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `num` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

自增步长  基于会话级别步长

--查看会话步长
mysql> show session variables like auto_inc%;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
--设置会话步长
mysql> set session auto_increment_increment=2;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from emp;
+----+------+---------+
| id | name | dept_id |
+----+------+---------+
|  1 | A    |       1 |
+----+------+---------+
1 row in set (0.00 sec)

mysql> insert into emp(name,dept_id) values(B,1);
Query OK, 1 row affected (2.09 sec)

mysql> select * from emp;
+----+------+---------+
| id | name | dept_id |
+----+------+---------+
|  1 | A    |       1 |
|  3 | B    |       1 |
+----+------+---------+
2 rows in set (0.00 sec)

mysql> show session variables like auto_inc%;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+--------------------------+-------+

全局步长设置

set global auto_increment_increment=20;

会话和全局步长

--会话
show session variables like auto_inc%; --查看
set session auto_increment_increment=2;  --步长
set session auto_increment_offset=10;  --起始值

--全局
show global variables like auto_inc%;
set global auto_increment_increment=2;
set global auto_increment_offset=10;

===========================分割============================

1.唯一约束

  create table t1(

  id int,

  num int,

  d_id int,

  unique uq1(num)

  unique uq2(num,d_id)  --联合唯一

)

ps:唯一约束不能重复(可以为空)

    加速查找

2 外键的变种

  外键+唯一 ===> 一对一 

                create table userinfo1(
                    id int auto_increment primary key,
                    name char(10),
                    gender char(10),
                    email varchar(64)
                )engine=innodb default charset=utf8;
 
                create table admin(
                    id int not null auto_increment primary key,
                    username varchar(64) not null,
                    password VARCHAR(64) not null,
                    user_id int not null,
                    unique uq_u1 (user_id),
                    CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
                )engine=innodb default charset=utf8;

  多对多 -- 多张表 双向一对多 在第三张表中有多个外键,根据需求可以将这多个外键联合唯一 

create table userinfo2(
                    id int auto_increment primary key,
                    name char(10),
                    gender char(10),
                    email varchar(64)
                )engine=innodb default charset=utf8;
 
                create table host(
                    id int auto_increment primary key,
                    hostname char(64)
                )engine=innodb default charset=utf8;
 
 
                create table user2host(
                    id int auto_increment primary key,
                    userid int not null,
                    hostid int not null,
                    unique uq_user_host (userid,hostid),
                    CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
                    CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
                )engine=innodb default charset=utf8;

sql语句数据行操作

--
create table tb12(
                id int auto_increment primary key,
                name varchar(32),
                age int
            )engine=innodb default charset=utf8;
insert into tb12(name,age) values(A,22);  --插入单条
insert into tb12(name,age) values(A,22),(B,33);  --插入多条

create table tb13(
                id int auto_increment primary key,
                name varchar(32),
                age int
            )engine=innodb default charset=utf8;
insert into tb13(name,age) select name,age from tb12;  -- 从另一张表插入新的表 

--
delete from tb12 where id = 1;  --  可以用 < 、>、 !=、 not 、not in 

--
update tb12 set name=C,age=33 where id = 1 and name = A;

--
select id,name,age from tb12 where id = 1;
select name,age from tb12 where age like 3%;  -- 通配符
select name,age from tb12 where age like 3_;
select name,age from tb12 limit 1,2;  -- 分页
select name,age from tb12 limit 3 offset 1;  -- 从第1行读(不含1),读取3行
select * from tb12 order by id desc -- 排序 asc升序
select * from tb12 order by id desc limit 10;  -- 取后10条数据
select age,count(id) from tb12 group by age;  -- 分组
select age,count(id) from tb12 group by age having count(id) > 1;  -- 分组后的数据进行筛选
    --连表操作
select t12.name,t13.age from t12 left join t13 on t12.id = t13.id -- 左外连接 左表为基表匹配右表,匹配不上的空值补充
--right join 右表全部显示
--inner join 匹配上的数据
--outer full join 显示全部

 转储sql文件

-- 在当前目录下产生db1.sql文件 数据表结构+数据
mysqldump -u root db1 > db1.sql -p

-- 加参数 -d 数据库表结构
mysqldump -u root -d db1 > db1.sql -p

-- 导入现有数据库 < 
mysqldump -u root -d db1 < db1.sql -p

 

mysql基础

标签:.sql   utf8   inner   specified   根据   行操作   进入   names   作文件   

原文地址:https://www.cnblogs.com/wangzihong/p/9822518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!