mysql mysql -u root -p
建立库:
> create database dt53;
> -> ; Query OK, 1 row affected (0.03 sec)
create database dt53 character set utf8; #不能写成utf-8
查询库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dt53 |
| dt54 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
删除库:
drop database mydb61;
Query OK, 0 rows affected (0.05 sec)
创建表
create table python_class( 姓名 varchar(40), 班级 varchar(40), java成绩 float );
Query OK, 0 rows affected (0.22 sec)
mysql> show tables;
+----------------+
| Tables_in_dt54 |
+----------------+
| python_class |
+----------------+
1 row in set (0.00 sec)
create table book( B_NAME varchar(20) comment '图书名称');
表的字段后面加注释
删除表:
drop table python_class
查看表:
select * from python_class2;
表中增删改查:
增: 不是add, append,put 而是 insert:
方法1:
insert into python_class2(姓名,班级,python成绩) values('张三','python2班',60 );
insert into python_class2(姓名) values('李四');
insert into python_class2 values('赵六','python2班',30)
注意1 最后一个不能加,
2字符串要写成’ ',而不能 " ",
3 不写()的话,后面的values值写满哦,如果缺少值的话会报错
方法2:
insert into python_class2 set 姓名='王五';
方法3:
同时插入多条语句:
insert into python_class2 (姓名,班级,python成绩)
values('周七','python2班',90) ,('吴八','python2班' ,80),('郑九','python3班',90);
insert into students values (1,'郑',20,100) ,(2,'陈',18,120),(3,'李',22,80);
删除:
=:set后面是赋值; where后面是条件!!!
delete from python_class2 where 姓名='李四';
删除表中所有信息:
delete from python_class2 where 1=1;
1=1永真式;
关系运算符:> ,>=,=,<,<=,!=
关系运算法需要放在where之后;
select a from tables [ where 条件 ]
select 姓名,班级,python成绩 from python_class2
select * from python_class2
查询python成绩表中成绩大于60的姓名:
select 姓名 from python_class2 where python成绩>60;
select 姓名 AS name from python_class2 where python成绩>60;
select 姓名 name from python_class2 where python成绩>=60;
与 (and)或(or) 非(not)
查询python2班级里面 成绩>60的;
select * from python_class2 where 班级='python3班' and python成绩>=75;
select * from python_class2 where 班级='python3班' or python成绩>=75;
改(update):update 表名 set 字段1=“ ” 字段2= “ 值2”,where 条件
update students set age=25,stuName='jerry' where id=1;
update students set age=30 where id=2;
create table students (
id int,
stuName varchar(30) ,
age int ,
weight float);
insert into students values (1,'郑',20,100) ,
(2,'陈',18,120),
(3,'李',22,80);
select
id as 主键 ,
stuName as 姓名 ,
age as 年龄,
weight as 体重 from students;