找回密码
 立即注册

南京1期张炎棋

新手上路

  • 17

    积分

  • 1

    帖子

  • 0

    精华

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
数据库
关系型数据库:存储的格式跟我们excel 表格很像,存储数据非常友好
Oracle ==》收费的、大型的公司、百度Oracle, 全称是 “ 甲骨文'公司,oracle 是他的一个产品


安装数据库:
yum install ==》在线下载并安排
yum install mysql ==》下载客户端
yum install mysql-server  ==》下载服务端
rpm -qa|grep -i mysql   ==》查看是否安装了数据库
mysql 服务启动和关闭
service mysqld start  ==》 启动数据库,以后每次进入数据库都需要启动数据库
service mysqld stop ==》关闭数据库
service mysqld status  ==》 查询数据库状态
service mysqld restart ==》重启数据库
备注:mysql 后面这个 d 代表的是一个守护进程 daemon
netstat -nltp  ==》 查看进程

登录mysql数据库 ==》第一次登录是不需要密码
[root@192 /]# mysql -uroot -p
Enter password:  ==》第一次装数据库,不需要密码
-u 代表用户 user 的意思, root 是用户
-p 代表密码 password的意思
我们默认mysql 中有个用户是 root
show databases;  ==》 查看数据库中的所有库;里面有 3个系统库
| information_schema |
| mysql              |
| test   
这里要加 ";",是数据库的默认格式
mysql> exit  ==》退出mysql数据库, 按住 Ctrl + C 或者输入 exit
mysqladmin -uroot password "123456"  ==》设置数据库的密码==(不是在mysql数据库里面输入)
mysql -uroot -p123456 ==》修改完密码后登录mysql数据库1
[root@192 /]# mysql -uroot -p
Enter password: 123456 ==》修改完密码后登录mysql数据库2,这里输入的密码是不会展示的
create database dcs1;  ==》创建一个新的数据库名字叫做dcs1
drop database dcs1;  ==》删除一个dcs1库( 千万不要删除默认的系统库)
数据库密码:
1)跳过权限不输入密码进入mysql
编辑 vim  /etc/my.cnf, 在 socket=/var下面这行输入
skip-grant-tables
service mysqld restart  ==》重启数据库
use dcs1;  ==》进入dcs1数据库里面(切换和进入数据库)
select database();  ==》查看当前已经进入到哪个数据库里面了
创建一个test表格
create table test(id int(20)primary key auto_increment,score float(20,2)not null,
name varchar(20),phone bigint(20) default 18611112222,time date);

desc test;  ==》查看表结构
Field ==》字段
type ==》数据类型
null ==》是否可以为空
key ==》primary key 主键
default ==》默认值约束
Extra ==》额外的备注, auto_increment 自增长约束

数据中常用的数据类型:
数值型:
int ==》最大存储值 2147483647( 最大存储值为10位)
bigint==》 手机号码都只能用 bigint来存储
float ==》score|float(20,2),2是精度,代表的是几位数
文本型:【 varchar 和 char 存的数据需要加单引号或者双引号】
varchar ==》存字符串,字符位数多一些
char ==》存字符,单个字符
日期型:【date存的数据需要加单引号或者双引号】
date ==》"2021-11-30”

数据库中常用的约束有哪些?
not null ==》非空约束
primary key ==》主键约束,里面的值必须是唯一的,不能重复
default ==》默认值约束,若不输入值,字段永远是 18611112222
auto_increment ==》自增长约束
foreign key==》外键约束
【对于表结构的操作】
show tables;  ==》查看当前库中有多少个表
alter table test rename test1;  ==》把 test 表的名字修改为test1
alter table test change id sid int(20);  ==》把id字段改为sid,并且去掉了自增长
alter table test change sid id int(20) auto_increment; ==》把sid字段改为id,并且加上自增长约束
alter table test add class int(20) first;  ==》 添加一个字段在最前面
alter table test add sex int(20) after id;  ==》添加sex字段到id后面
alter table test add(age1 int(20),age2 int(20)); ==》添加两个字段,只能添加到最后面,不能添加到最前面
alter table test modify class int(20) after id; ==》调整把class字段放在id字段后面
alter table test drop sex; ==》删除sex字段
alter table test drop age1,drop age2;  ==》删除两个字段
alter table test change id id int(20);  ==》删除自增长且不改变字段名称
alter table test change id id int(20)auto_increment;  ==》 不改变字段名称增加自增长
alter table test drop primary key; ==》删除主键
备注: 删除主键,先删除自增长
自增长约束是结合主键去使用的

【对于表中数据的操作】
单个数据插入:
insert into test(id,score,name,phone,time)values(1,89.99,'xiaoliu',18611223311,'2021-11-30');
insert into test values(2,78.66,'xiaoliu1',18982475616,'2021-11-30');
多个数据的插入:
insert into test(id,score,name,phone,time)values(3,66.666,'xiaoliu2',13688889999,'2021-11-30'),
(4,78.89,'xiaoliu3',13666668888,'2021-11-30');  ==》插入两条数据
insert into test values(5,100,'xiaoxie',default,'2021-11-30'),(6,99.99,'xiaoshi',13688889999,'2021-11-30'),
(7,68.99,'xiaozhang',18966669999,'2021-11-30');  ==》插入三条数据
备注: 0 不等于null, null 指的是一个空的属性,0 代表的是一个值

指定字段插入数据
insert into test(id)values(8);  ==》帮id字段插入值
insert into test(score)values(88.88);  ==》把score插入值的时候id字段对应的值会自动加1,因为设置了自增长
int  bigint
alter table test change phone phone int(20) not null;  ==》把phone数据类型修改为int且加上not null约束
所有的手机号都改为:2147483647
insert into test(phone)values(2147483648);  ==》还是显示2147483647
insert into test(phone)values(2147483646);  ==》正常显示 2147483646




分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册