目录
进入MySQL命令符:mysql -u root -p
客服端Client和服务器Server模式(C/S模式 或 C/S架构)
数据库的服务器:mysql服务
数据库的客服端:cmd【命令行方式】、navicat【界面】
host:主机
cmd: mysql -h ip地址 -u root -p
ip地址:那一台电脑?
同一台电脑:127.0.0.1(本机回路IP地址) 域名:localhost
端口:哪个房间 3306
账号:root超级管理
密码:root
show databases;
create database 数据库名 character set 'utf8' collate 'utf8 bin';
存储编码 排序规则
drop database if exists 数据库名;//如果该数据库存在时,删除该数据库
use 数据库;// 使用该数据库
show tables;#显示所有的表
desc 表名; #查看该表的数据类型
创建表:
create table 表名
(
列名1 数据类型,
列名2 数据类型,
......
);
学生表:学号、姓名、性别、年龄、出生日期
Java 整型 int
小数 float double
字符串 varchar
日期 date datetime
举个例子:
- CREATE TABLE train_user(
- userid VARCHAR (100) PRIMARY KEY,
- username VARCHAR(100) NOT NULL,
- psw INT,
- sex VARCHAR(20),
- money DOUBLE,
- id VARCHAR(100),
- phone VARCHAR(100),
- photo VARCHAR(200),
- createtime datetime
- );
-
- INSERT into train_user(userid,username,psw,sex,money,id,phone,photo,createtime)VALUES
- ('user1','肖战',123,'男',100.01,'12345678901','12222222222','c1.jpg','2001-01-18 10:20:12'),
- ('user2','白鹿',123,'女',100.10,'12345678901','12222222222','c1.jpg','2001-01-18 10:20:12'),
- ('user3','杨紫',123,'女',100.83,'098765432345','12222222222','c1.jpg','2001-01-18 10:20:12'),
- ('user4','胡歌',123,'男',100.23,'098765432345','12222222222','c1.jpg','2001-01-18 10:20:12'),
- ('user5','鹿晗',123,'男',100.33,'098765432345','12222222222','c1.jpg','2001-01-18 10:20:12');
主键:唯一标识、 数据不能重复
primary key
一个表,只能有一个主键
一个主键:可以关联一个或多个字段
数据:规范或约束
int 类型: 值需要约束
主键约束:值不能重复
外键约束:子表中外建值,在父表中一定要存在
非空约束:not null
唯一约束:unique
自定义约束:年龄不能小于0,性别只能在男和女
drop table if exists student;//如果student表存在 就进行删除
alter table student add address varchar(20);
将address varchar(20)添加到student表格属性中去
create table classmate
(
classid varchar(20) primary key comment '注释',
classname varchar (20) comment'班级名称',
);comment '班级表';
insert into 表名(列1,列2,。。。)
values(值1,值2,.....),
(值1,值2,......);
值 数字不需要' '单引号
字符串 需要 单引号' 不能用双引号" "
日期 yyyy-mm-dd '2001-02-18'
hh:mi:ss '01:10:10'
主键约束:唯一
非空约束:不能为空
update 表名 set 列1 = 值1,列2 = 值2,。。。
where 列=值;
- UPDATE train_user SET psw = 123456 WHERE userid = 'user3';
-
- UPDATE train_number SET num = 200,money = 300 WHERE number = 'D6208';
-
- UPDATE train_number SET endstationid = '厦门北' WHERE number = 'D6237';
-
- UPDATE train_station SET stationinfo = '厦门北' WHERE stationid = '厦门北';
delete from 表名 where 列 = 值;
-
- delete from tb_user where tb_user.age < 20
select 列1,列2,。。。 from 表名 where 列=值1;
- SELECT * FROM train_station;
-
- select userid,username,userage from usertable where userid = 01;
注意点:模糊查询like
-
- SELECT * FROM train_station WHERE stationid LIKE '南%';
- SELECT * FROM train_station WHERE stationid LIKE '%南';
- SELECT * FROM train_station WHERE stationid LIKE '%南%';
- 1、
- SELECT userid,username,password,age,address
- FROM tb_user
-
- 2、
- SELECT userid,password
- FROM tb_user
- where tb_user.username='阿猫'
-
- 3、
- SELECT username
- FROM tb_user
- where tb_user.userid='u002'
-
- 4、
- SELECT username
- FROM tb_user
- where tb_user.userid='u004' and tb_user.password
-
- 5、
- SELECT username
- FROM tb_user
- where tb_user.age <22
-
- 6、
- select userid,username,password,age,address
- from tb_user
- where tb_user.age between 25 and 30
-
- 7、
- select *
- from tb_user
- where tb_user.username like '%猫%'
-
- 8、
- select a.goodsid,a.goodsname,b.typename,a.price,a.num,a.goodsinfo
- from tb_goods a
- inner join tb_type b on a.typeid = b.typeid
- where a.goodsname = '梨'
-
- 9、
- select a.goodsid,a.goodsname,b.typename,a.price,a.num,a.goodsinfo
- from tb_goods a
- inner join tb_type b on a.typeid = b.typeid
- where a.goodsname like '%奥%'
-
- 10、
- select *
- from tb_goods
- order by price
-
- 11、
- select *
- from tb_goods
- where price between 5 and 10
- order by price
-
- 12、
- select username,max(tb_user.age)'年龄'
- from tb_user
-
- 13、
- select username,age'年龄'
- from tb_user
- order by tb_user.age
- limit 0,5
-
- 14、
- delete from tb_user where tb_user.age < 20
-
- 15、
- select a.goodsid,a.goodsname,a.typeid,b.typename,a.price,a.num,a.goodsinfo
- from tb_goods a
- inner join tb_type b on a.typeid = b.typeid
- where b.typename = '水果'
-
- 16、
- select count(goodsid)
- from tb_goods a
- inner join tb_type b on a.typeid = b.typeid
- where b.typename = '水果'
-
- 17、
- select a.orderid,a.userid,a.goodsid,a.money,a.ordertime,a.state
- from tb_order a
- inner join tb_user b on a.userid = b.userid
- where b.username = '阿猫'
-
-
- 18、
- select a.orderid,a.userid,b.username,a.goodsid,c.goodsname,c.num,c.price,c.num*c.price 总价,a.ordertime,a.state
- from tb_order a
- inner join tb_user b on a.userid = b.userid
- inner join tb_goods c on a.goodsid = c.goodsid
- where a.state= '未付款'
-
- 19、
- select sum(money)'总金额'
- from tb_order
- where state= '已付款'
如有错误请指出哦,可能我不大细心没看到。练习就放这里,可以看看。