数据库—mysql的基本语法

alter user 'root'@'localhost' identified by 'root';//修改密码
quit  //退出数据库


1. =======选择或创建一个数据库=======

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

新建语法: create database 数据库名字;

create database test2;

使用(切换)库: use 数据库名;

use test2;

2. =======显示表=======
show tables;

3. =======创建表=======
create table 表名(
    列名1 类型 约束,
    列名2 类型 约束,
    ...
    列名n 类型 约束,
    约束
);

create table Student(
   id int,
   name varchar(10)
);

4. =======查看表结构=======
desc 表名;

5. =======插入数据=======
insert into 表名(列1, 列2...列n) values (值1,值2... 值n);

insert into student(id,name) values(1, '张三');
insert into student(id,name) values(2, '李四');
6. =======查询数据=======
select1,列2....列n from 表名;

select id,name from student;
select * from student;

7. =======数据类型=======
整数类型
    int -2^31 ~ 2^31-1   四个字节
    tinyint 一个字节
    smallint 两个字节
    bigint 八个字节
小数类型
    浮点类型
        FLOAT 
        DOUBLE
    定点类型 (小数部分不会丢失精度,占用空间高)
        DECIMAL(p, s)
        p 有效数字长度
        s 小数位数

        decimal(10, 2)
        12345678.77  ok
        123456789.44  存不下
        12345678.456  小数部分变为46

字符串类型
    char(最大长度)    定长的字符类型
    varchar(最大长度)  变长的字符类型

    name char(5)  'abc'  存储后  'abc  '  用空格补齐不足的部分, 再查询时会去掉, 效率高,但占用空间多, 最大值255
    name varchar(5) 'abc'  存储后 'abc'    不会用空格补齐                , 不会占用额外空间, 效率较char低, 最大值65535

日期类型
    datetime 包括年月日,时分秒 时间范围短(1000-1-1 ~ 9999-12-31)
    date    只包括年月日
    time    只包括时分秒
    timestamp  也可以包括年月日时分秒, 时间范围短(1970-1-1 ~ 2038-1-19)

create table test(
    a datetime,
    b date,
    c time,
    d timestamp
);
now() 获取当前时间

insert into test(a,b,c,d) values(now(),now(),now(),now());
insert into test(a) values('1999-5-8 10:30:00');

8. =======约束=======
学生表  student
编号 int  sid
姓名 varchar sname
生日 date   birthday
性别 char   sex

-- 把约束加在类型之后
create table student(
  sid int unique,
  sname varchar(20),
  birthday date,
  sex char(1)
);
-- 把约束单独一行
create table student(
  sid int not null,
  sname varchar(20) not null,
  birthday date,
  sex char(1),
  unique(sid)
);
删除表可以使用 drop table 表名;

可以使用 unique 唯一约束,一个表中可以有多个唯一约束
       not null 非空约束, 让列的取值不能为空
       primary key 主键约束, 效果上综合的唯一和非空, 一个表只能有一个主键
       foreign key 外键约束, 检查两表的关系列是否合法, 被引用的列必须是唯一的
       check (mysql不支持)
create table student(
  sid int primary key,
  sname varchar(20) not null,
  birthday date,
  sex char(1),
  cid int,
  foreign key(cid) references class(cid) /* student中的cid取值必须引用class中的cid的值 */
);
create table student(
  sid int,
  sname varchar(20) not null,
  birthday date,
  sex char(1),
  cid int,
  primary key(sid),
  foreign key(cid) references class(cid) /* student中的cid取值必须引用class中的cid的值 */
);
insert into student(sid,sname,birthday,sex,cid)values(1,'张三',now(),'男',2);
insert into student(sid,sname,birthday,sex,cid)values(2,'李四',now(),'男',3);

create table class(
   cid int primary key,
   cname varchar(20)
);

9. 自增列 (主要用在给主键产生一个唯一值)
create table course(
    cid int primary key auto_increment, /*产生一个自动增长的值*/
    cname varchar(10)
);
insert into course(cid, cname) values(null, 'java se'); -- 给null
insert into course(cname) values('web'); -- 直接把名字和值从insert中去掉
注意用了自增长,就不要手动赋值了,否则会使用手动赋的值

不从1开始的例子
create table course(
    cid int primary key auto_increment, /*产生一个自动增长的值*/
    cname varchar(10)
) auto_increment=1000;

10. 默认值 (default)
create table person(
    id int primary key auto_increment,
    cname varchar(10),
    sex char(1) default '男',
    lastmodify timestamp default current_timestamp on update current_timestamp
);
-- default current_timestamp 插入数据时,采用当前时间作为默认值
-- on update current_timestamp 修改数据时,采用修改那个时刻的时间来更新这一列
insert into person(cname) values('张三');
insert into person(cname,sex) values('李四','女');

11. 修改
update 表 set 列名=新值, 列2=值2 ...;  -- 修改表中所有的行
update 表 set 列名=新值, 列2=值2 ... where 列名=条件;   -- 仅更新符合条件的记录 

update course set cname='java se'; -- 修改表中所有课程名称
update course set cname='spring' where cid=1000; -- 仅修改了cid=1000 这条记录的课程名称

12. 删除
delete from 表名; -- 删除这个表中所有记录,但表的定义不动
delete from 表名 where 列名=条件; -- 仅删除符合条件的记录

drop table 表名; -- 删除这个表,连同里面的数据


13. 导入数据
1) 可以用
mysql > source 路径\sql文件的名称;
源 
例如:source E:\6.23实训班共享\预习\数据库(详)\scott.sql

2) 在cmd窗口运行
mysql -uroot -p test3 < E:\6.23实训班共享\预习\数据库(详)\scott.sql
其中test3是数据库的名字 

14. 导出数据 
在cmd窗口运行
mysqldump -uroot -p test3 > a.sql











猜你喜欢

转载自blog.csdn.net/woshijinfeixi/article/details/81837473