1. 程式人生 > >MySQL數據管理1

MySQL數據管理1

裝包 配置

一 裝包

[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 "[email protected]";


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 新表名;

++++++++++++++++++++++++

課後練習

創建學生信息表

存儲系統賬號信息表


MySQL數據管理1