6.1数据库MySQL基础

1.显示当前所有数据库

show databases;

2创建数据库

create database ceshi;

3进入(使用)数据库

use ceshi;

4显示数据库中的表

show tables;

备注: 数据类型

           数字型:

                   整型: tinyint 1字节 2的8次方

                               int      4字节 2的64次方

                         decimal(m,d) 定点型 m数字的总长度

                                                                  d小数的位数

                                decimal(5,2)1.2896-1.29

                                                           999.123-999.12

                      字符型:

                            char(255)  定长字符

                            varchar(255) 变长字符

                          日期时间

                                  datetime 8字节 2018-06-01 09:05:01

                           主键:p'rimary key

                            自增长列:auto_increment

                             非空:not null

                            默认值:default值

5,建表

          需要指明 数据类型 非空

          每个表都要设置一个主键

create table Myclass(

           id int(4)not null primary key auto_increment,

           name char(20) not null,

           sex int(4) not null default"0",

           degree  decimal(16,2)

            );

6, 查看表结构

  desc  myclass;

===================

show= 显示

create= 创造

database=数据库

7插入数据

insert into myclass values(’ ’,’张三’,’ ’,98.5);

8查看表中的数据

select*from myclass;

9修改信息

 update myclass set name = '张三' where id = 1;

10删除信息

   delete from myclass where id = 1;

=============================================

alter table myclass + 语句

1 修改表名

rename as newcl;

2添加字段

 add age int(4)not null;

add pid int(4) not null first;

add selfsm char(255) not null after age;

3修改字段的名字和类型

change age newage char(20);

4修改某一字段的类型

modify column newage int(4);

5添加默认值-如果有默认值,先删除再添加

alter column newage drop default;

alter column newage set default  18;

6删除字段

drop  id;

7添加主键

add primary key(字段名);

8删除主键

drop primary key;

9删除表

drop table newcl;

show tables;

10删除数据库

drop database ceshi;

show databases;

posted @ 2018-06-01 16:21  sun1987  阅读(246)  评论(0编辑  收藏  举报