找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 dcs_68-董瑞杰 于 2021-11-21 19:53 编辑

昨天内容:
进入MySQL:重启:service mysqld restart        进入:mysql -uroot -p123456
对数据库进行操作:show databasess;[查库]                create database 库名;[增库]               
drop database 库名;[删库]                        use 数据库名;[进入指定库]
对表进行操作:desc 表名;[进入表]                drop 表名;[删除表]                show tables;[查询所有表]
对表内容进行操作:create table 表名(字段);——创建表结构
desc 表名;——查询表结构
alter table 表名 add age int(3);——增加表字段(默认放末尾)——add
alter table 表名 add id int(4)first;——增加表字段(放在最前面)——first
alter table 表名 add id int(2)after age;——增加表字段(放在指定位置后)——after
alter table 表名 drop age;——删除表字段——drop
alter table 表名1 rename 表名2;——修改表名——rename
alter table 表名 change id s_id int(4);——修改表字段名——change
drop table 表名——删除表


今天内容:
1、同时增加多个字段
    alter table 表名称 add (字段1,字段2);
    alter table aa add(id int(3),name char(4));
2、删除指定的单个字段或者多个字段
   alter table 表名 drop 字段名称; #单个
    alter table aa drop id;
    alter table 表名称 drop 字段名称,drop 字段名称;#多个
    alter table aa drop id,drop name;
3、删除表
    drop table 表名称
    drop table aa;删除aa这个表——删除后当前aa表不存在

表结构操作语言:DDL
    create创建  alter增加修改的  drop删除  desc查询
4、创建表增加主键和自增长约束
    create table dcs68(id int(4)primary key auto_increment,name char(10),scroe int(4));
5、给没有主键的表增加主键和自增长
    alter table aa change id id int(5)primary key;
    alter table aa change id id int(5)auto_increment;
6、删除主键和自增长
如果你要删除主键必须要先删除自增长约束
    alter table aa change id id int(5);删除id中的自增长
    alter table aa drop primary key;删除主键

如果给表中插入数据的流程:
    1、在liunx交互界面先进入数据库:mysql -uroot -p123456
    2、查询一下所有数据库:show databases;
    3、进入到指定的数据库中:use +数据库名称如:use dcs68;进入dcs68数据库中
    4、查询一下有没有可以用表:show tables;
    5、如果没有你就自己创建一个:
    creat table 表名称(字段 约束);
    create table dcs(id int(3),name char(10),age int(4));#创建一个表名称dcs
6、表结构创建好后自己查询一下表结构:
    desc dcs;查询dcs表结构
7、然后插入数据删除查询操作
    insert into 插入数据
    select * from 表; 查询
    delete from 表 删除

表数据:(重点)
表数据操作语言:DML
增删改查:insert into ,delete from ,update set,select from
where条件表达式
1、表中增加数据:
结构:id ,name ,age ,scroe
    insert into stu(id,name,age,scroe)values(1,'zhang',20,89); #增加一行数据
    insert into stu values(2,'wang',19,100),(3,'lisi',20,99);#增加多行数据
    insert into aa(name,age)values('zng',22),('ang',21),('wang',31);#自增长和主键同时插入多行数据
2、查询数据
    select * from 表名称; 查询所有的数据
3、删除表的数据
    delete from stu where id=4;#删除表中id=4的这一行的数据
    delete from stu;删除整个表中的所有数据
备注:delete只能删除表数据,不能删除表结构,如需要删除表结构需要用drop table
4、删除整个表的数据:
    truncate aa;
备注:truncate只能删除表数据,不能删除表结构,如需要删除表结构需要用drop table
5、修改表数据:
    update 表名称 set 修改的值 where 条件指定;
    update aa set name='zhang' where id=1;#把id=1这一行的姓名改为zhang
    update aa set name='aa';#把所有的姓名都改为aa

and:与——全真才为真,有一假即为假
or:或——有一真为真即为真,全假才为假
where ,and ,or ,in,=,!=(讲解查询语句会讲)

查询(重点)
查询所有:
    select * from 表名称;
    select * from stu;查询stu表中的所有内容
通过条件查询某个条件的值:
    select * from 表名称 where 条件;
    select * from stu where name='aa';查询姓名是aa的所有内容
通过条件查询满足几个条件的内容:
    select * from 表名称 where 条件 and 条件;
    select * from stu where name='qian' and age=20;查询满足姓名是qian年纪是20的数据
大于>
    select * from stu where scroe>60; 大于60分所有信息
小于>
    select * from stu where scroe<60;小于60分所有信息
等于=
    select * from stu where scroe=60;等于60分所有信息
大于等于>=
    select * from stu where scroe>=60;大于等于60分所有信息
小于等于<=
    select * from stu where scroe<=60;小于等于60分所有信息
不等于!=
    select * from stu where scroe !=60;不包含60分的所有信息
in指定具体的值
    select * from stu where scroe in(60,88,99);指定的值有就显示,没有就不显示
查询张三的信息,告诉张三的年纪的多少?
    select * from stu where name='张三';
    select age from stu where name='张三';




































分享至 : QQ空间
收藏

0 个回复

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