[root@localhost ]#tar -xvf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar
[root@localhost ]#rm -rf mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm
[root@localhost ]#rpm -qa | grep -i mariadb
[root@localhost ]#rpm -e --nodeps mariadb-libs
[root@localhost ]#rm -rf /etc/my.cnf
[root@localhost ]#rm -rf /var/lib/mysql/*
[root@localhost ]#yum -y install perl-Data-Dumper perl-JSON
[root@localhost ]#rpm -Uvh mysql-community-*.rpm
[root@localhost ]#rpm -qa | grep -i mysql
二启动服务
[root@localhost ]#netstat -utnalp | grep :3306
[root@localhost ]#systemctl status mysqld
[root@localhost ]#systemctl start mysqld
[root@localhost ]#systemctl status mysqld
[root@localhost ]#netstat -utnalp | grep :3306
三设置管理员本机登录密码
[root@localhost ]#grep -i 'password' /var/log/mysqld.log
[root@localhost ]#mysql -uroot -p"e7kdRG_&wA>T"
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=6;
mysql> alter user root@"localhost" identified by "123456";
mysql> quit
# mysql -uroot -p123456
mysql> show databases;
mysql> quit
四把数据库存储到数据库服务器上过程?
1 连接数据库服务器
2 选择库 (数库目录下的文件夹)
3 建表(文件夹里的文件)
4 插入表记录
5 断开链接
建表
show databases;
create database bbsdb;
use bbsdb
select database();
create table stuinfo(
name char(10),
age int(2),
sex char(4),
class char(7)
);
查看表结构
mysql> desc stuinfo;
插入表记录
insert into stuinfo values("jim",21,"boy","nsd1702"),("tom",21,"boy","nsd1702");
查看表记录
select * from stuinfo;
断开链接
mysql>quit
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
create table stuinfo(
name char(10),
age int(2),
sex char(4),
class char(7)
);
查看表结构
mysql> desc stuinfo;
插入表记录
insert into stuinfo values
("jim",21,"boy","nsd1702"),
("tom",21,"boy","nsd1702");
查看表记录
select * from stuinfo;
断开链接
mysql>quit
sql命令使用规则?
管理库的常用SQL命令?
show databases;
use 库名;
create databse 库名;
select database();
drop databse 库名;
库名的命名规则?
管理表的常用SQL命令?
show tables;
desc 表名;
select * from 表名;
create table 表名(
字段名 类型(宽度),字段名 类型(宽度),字段名 类型
(宽度)
);
insert into 表名 values(字段值列表),(字段值列表);;
drop table 表名;
mysql数据类型?
数值型:体重、身高、成绩、工资
整型 tinyint smallint MEDIUMINT int bigint
create table t1(level tinyint);
create table t2(level tinyint unsigned);
crceate table t3(level int unsigned);
浮点型
float(m,n)
double(m,n)
create table t4 (chengj float(5,2));
字符型:姓名、工作单位、通信住址
char varchar blob text
create table t6(name char(10),level tinyint unsigned);
create table t7(name char(10),address varchar(50),
level tinyint unsigned);
create table t8(name blob,address text, level tinyint
unsigned);
create table t9(name char,address varchar);
日期时间型:生日、注册时间、出生年份 上课时间
year YYYY 2015
time HHMMSS 153529
date YYYYMMDD 20150522
datetime / timestamp YYYYMMDDHHMMSS
20170522153658
create table t10(
name char(10),
age tinyint unsigned ,
pay float(7,2),
up_class time,
birthday date,
meetting datetime,
syear year);
insert into t10 values
("bob",21,20000,083000,20171120,20170522183000,1
997);
使用时间函数获取时间给日期时间类型字段赋值
select now();
select year(20190909);
select month(20190909);
select day(20190909);
select date( now());
select time( now());
insert into t10 values("tom",21,20000,now(),now
(),now(),now());
insert into t10 values("alic",21,20000,time(now
()),now(),now(),year(20190328));
枚举类型:爱好、性别
字段值在列举的范围内选择
set("值1","值2","值1") 多选
enum("值1","值2","值1") 单选
create table t11(
name char(10),
age tinyint unsigned ,
sex enum("boy","girl","no"),
likes set("book","it","film","music")
);
insert into t11 values("bob",23,"boy","book,it");
insert into t11 values("alic",23,"man","book,it");
insert into t11 values("jerry",29,1,"book,it,music");
desc mysql.user;
字段约束条件: 作用限制如何给字段赋值
是否允许赋空(null)值 默认允许赋null值
not null 不允许赋null值
索引 key
默认值 default "值"
额外设置 Extra
create table t12(
name char(10) ,
age tinyint unsigned not null default 25,
sex enum("boy","girl","no") default "no",
likes set("book","it","film","music") default "it"
);
desc t12;
insert into t12(name)values("bob");
insert into t12 values("tom",29,"boy","it,book");
insert into t12 values(null,null,"girl","music,book");
insert into t12 values("null",31,"girl","music,book");
insert into t12 values("",31,"girl","music,book");
+++++++++++++++++
修改表结构
alter table 表名 执行动作;
添加新字段
add 字段名 类型(宽度) 约束条件;
add 字段名 类型(宽度) 约束条件 first;
add 字段名 类型(宽度) 约束条件 after 字段名;
alter table t12 add mail varchar(30) not null
default "stu@tedu.cn";
alter table t12
add stu_id char(7) first,
add tel char(11) after name;
删除已有字段
drop 字段名,drop 字段名;
mysql> alter table t12 drop sex;
修改已有字段类型
modify 字段名 类型(宽度) 约束条件;
alter table t12 modify tel varchar(15);
修改字段名
change 源字段名 新字段名 类型(宽度) 约束条件;
alter table t12 change stu_id stu_num char(7);
修改表名
alter table 源表名 rename 新表名;
++++++++++++++++++++++++
课后练习
创建学生信息表
存储系统账号信息表
原文地址:http://blog.51cto.com/13513556/2052940