美文网首页
基本操作

基本操作

作者: 蘋果_283e | 来源:发表于2017-03-19 18:19 被阅读0次

DOS界面连接数据:

标记选中 右键 复制上

登录数据库:mysql -u用户名(一般是root) -p //mysql -uroot -p

输入密码:

退出数据库:exit;

select version();//打印数据库版本

select user();//打印当前用户

放弃正在输入的命令:\c
显示命令清单:\h
退出mysql程序:\q 或 exit
查看MySQL服务器状态信息:\s;status;

show warnings; // 显示最后一个执行的语句所产生的错误、警告和通知

show errors; // 只显示最后一个执行语句所产生的错误

notice

mysql数据类型

1、整数 :默认用int,再大用bigint ()中的数字和数值列属性zerofill有关

数值列属性
AUTO_INCREMENT
UNSIGNED
ZEROFILL
NULL和NOT NULL
DEFAULT


(1)整型

a)tinyint:很小的整数,范围0~255,有符号的范围-128~127,默认长度4,占1个字节。一般用于年龄

b)smallint:小的整数,范围0~65535,有符号范围-32768~32767,默认长度是6,占2个字节。

c)int:中等大小的整数,范围0~42.9亿,有符号访问-21亿~21亿,默认长度是11,占4个字节。一般用于主键字段id

13245678909  

d)bigint:很大的整数,范围0~18446744073709551615,有符号范围:-9223372036854775808~9223372036854775807用于微生物界或天文历法

2、小数:float double DECIMAL

(3)浮点型

a)float(m,d) 精度会有损失

b)double(m,d) 精度会有损失

c)decimal(m,d)m:小数的总位数,d:小数点后面的位数,常用于金融/银行账目,精度不会有损失,推荐使用

3、日期:date  时间 time 日期和时间:DATETIME

(4)时间/日期类型

a)date//日期类型 2017-01-01

b)time//时间类型 11:05:00

c)datetime//日期时间类型 2017-01-01 11:05:00

d)timestamp//时间戳

4、字符串类型

字符串列属性
BINARY
NOT NULL和NULL
DEFAULT

char(10)定长,varchar(10)变长,TEXT文

(2)字符串类型

a)char:定长字符串,浪费资源。 范围0~255

b)varchar:变长字符串,节省空间。范围0~65535,长度值可以是0~65535中的任何一个值。

本.

Value

CHAR(4)

Storage Required

VARCHAR(4)

Storage Required

''

' '

4 bytes

''

1 byte

'ab'

'ab '

4 bytes

'ab'

3 bytes

'abcd'

'abcd'

4 bytes

'abcd'

5 bytes

'abcdefgh'

'abcd'

4 bytes

'abcd'

5 bytes

当定义char时,不管你存入多少字符,都会占用到你定义的字符数,而用varchar时,则和你输入的字符数有关,会多一到两个字节来记录字节长度,当数据位占用的字节数小于255时,用1个字节来记录长度,数据位占用字节数大于255时,用2个字节来记录长度,还有一位来记录是否为null值

c)text//文本类型,范围0~65535。不能有默认长度值

d)enum//枚举类型,enum('man','woman'),一般用于性别

BLOB是一个二进制大对象,可以容纳可变数量的数据即图或视频、声音

完整性约束

1 not null非空约束

2primary key 主键约束

3unique唯一

4auto_increment自增长

5UNSIGNED 不能是负数,从0开始

6DEFAULT默认

7主键(primary key)和外键(foreign key)

主键:帮助MySQL以最快的速度把一条特点的数据记录的位置确定下来。

主键必须是唯一的、非空的

主键应该是紧凑的,因此整数类型比较适合

主键一般不允许有符号(unsigned)

联合主键:两个字段联合起来唯一标识一条记录

外键:引用另外一个数据表的某条记录。

外键列类型要与主键列类型保持一致

外键列应该加上NOT NULL

注:有主外键关系的表,插入数据时要先主表在从表,删除时要先从表在主表,修改表中记录要保证外键要在主表中主键存在

mysql>create table dept(id int auto_increment primary key,bname varchar(20) unique,bmoney int unsigned);

mysql> create table person(pid int primary key auto_increment,name varchar(10) not null,degree varchar(10) default '大专',bid  int not null,foreign key(bid) references dept(id));

数据库操作

1、mysql> show databases; 显示所有数据库

2、mysql> create database if not exists student1;  创建数据库

create database demo2 (default) character set utf8/gbk;//设置编码

3、mysql>use student; 打开数据库

4、mysql> rename database student1 to student2;//重命名,现在不能使用,因为不安全,要在资源管理器中打开才可以

5、mysql> drop database student2;删除数据库

6、select database();

表操作

1、mysql> show tables;显示所有表

2 、mysql> create table if not exists student1(xid int primary key auto_increment,name varchar(20),sex varchar(20),degree varchar(20),jobtime date,money decimal(7,2));

//创建表时,先删除表
drop table if exists demo;
create table demo(

);

3、crud:增 删 改 查,CRUD是指在做计算处理时的增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)

对表中的数据进行操作:增(insert)删(delete)改(update)查(select)

1)增(insert) insert into 表名(字段名)values(字段值);字符串、时间日期类型都需要加引号

加入所有字段可省略(字段名),(字段值)可以有多个

insert into worker(name,degree,money) select name,degree,money from worker; //插入多条记录

2)查(select)select *(或字段名) from 表名 [where条件];

3)改(update) update 表名 set 字段名1=新字段值1,字段名2=新字段值2,...()where条件;

4)删(delete)delete from 表名 where 条件;delete删除之后存入的数据,id会从被删除的记录ID之后继续递增

清空数据truncate table 表名;auto-increment不会递增

4、查看表结构:desc 表名;

5、删除表:drop table 表名;

6、修改表:

1)修改表名:alter table 旧表名 rename to 新表名;rename table 旧表名 to 新表名

2)添加一个字段(列)alter table 表名 add 字段名 字段类型 [是否为空];

3)删除一个字段alter table 表名 drop column 列名;

4)修改字段名alter table 表名 change 旧字段 新字段 数据类型;

5)修改数据类型(长度,是否为空)alter table 表名 modify 字段名 字段类型 [是否为空];

格式: alter table 表名 action;

Action取值如下:
add 列名 <建表语句> (first | after 列名)
add primary key (列名)
alter 列名 set default 默认值
change (modify) 列名 <建表语句>(first | after)
drop 列名
drop primary key
Drop index index_name;
rename as 新表名

Mysql数据库中的通配符
“%” (百分号)  代表任意长度(长度可以为0)的字符串
“_ “(下横线)  代表任意单个字符

select中的where条件

大于(等于)/小于(等于)
select stid,name,age from student where age >= 20 and age <=25;
select * from student where joinTime >= '2015-01-01' and joinTime <= '2015-12-31';

between... and...限定一个范围
select * from student where age between 20 and 25;//在20到25之间
select * from student where joinTime between '2015-01-01' and '2015-12-31';

and or not
select * from student where age = 20 and sex='女';//并且
select * from student where sex = '男' or sex='其他';//或者
select * from student where sex != '男';//不等于
select * from student where sex <> '男';//不等于

select * from student where not sex = '男';

in 在某个范围内  not in 不在某个范围内

select 字段名 from 表 [where条件][order by 字段名 asc/desc];不写asc或desc,默认是升序

select * from student order by stid desc,age asc;//多个字段排序

宏select 字段名 from 表 [where条件][order by 字段 asc/desc][limit 起始位置,记录数];select * from 表名 limit (当前页-1)*记录数,记录数; 不能和in一起用

模糊查询 like 效率低
select * from student where name like '%明%';//匹配含有'明'的记录
select * from student where name like '明%';//匹配以'明'开头的记录
select * from student where name like '%明';//匹配以'明'结尾的记录

as 别名(不能和关键词冲突)//字段名或表名太长//防止字段名冲突

mysql> select pid as 学号,name as 姓名,degree as 学历,dept.bname from person,dept where dept.id=person.bid;

相关文章

  • 栈和队列

    顺序栈的基本操作: 链栈的基本操作 顺序队的基本操作 链队的基本操作

  • 【数据类型】21、上机练习:容器类型操作

    目录一、列表、元组基本操作二、列表、元组高级操作三、集合基本操作四、字典基本操作 一、列表、元组基本操作 +,*,...

  • MongoDB 基本操作用法

    MongoDB文档 基本操作 update 基本操作 find 基本操作 aggregate 终端用法 导入导出

  • 基本操作

    Alt+P:运行 按住win+左/右箭头:改变窗口大小 ctrl+W:复制文件 点击`:调出控制台输入框 在不同的...

  • 基本操作

    通过对廖雪峰的git教程学习做出的摘要1.git init 创建版本库git add () 告诉Git,把文件添加...

  • 基本操作

    用户相关 查看所有用户 修改用户名/密码 删除用户 数据泵相关 表空间 新建表空间 空间 表相关 查看所有表

  • 基本操作

    动态浏览

  • 基本操作

    因为要在Linux操作系统下写HDL,所以需要对Linux系统下的一些命令比较熟悉,以下做一下对Linux系统做一...

  • 基本操作

    1)查看目录下所有文件 2)返回最上级目录 3)复制文件 4)supervisor一套 5)supervisor简...

  • 基本操作

    可视化设置外键:http://blog.csdn.net/justdb/article/details/69934...

网友评论

      本文标题:基本操作

      本文链接:https://www.haomeiwen.com/subject/schvnttx.html