您的位置:首页 > 数据库 > MySQL

MySQL 相关库操作、表操作、数据操作

2017-04-03 21:50 246 查看

1、库操作

@ sunRainAmazing

必须首先登录到mysql 中,
有关操作都是在mysql 的提示符下进行,
而且每个命令以分号结束。

5.1 创建数据库
命令:create database <数据库名>;
例1:创建一个名为test 的数据库
mysql> create database test;

5.2 显示所有的数据库
命令:show databases;(注意:最后有个s)
mysql> show databases;

5.3 删除数据库
命令:drop database <数据库名>;
例2:删除名为test 的数据库
mysql> drop database test;

5.4 连接数据库
命令: use <数据库名>;
例3:连接david 数据库
mysql> use david;

5.5 查看当前使用的数据库
命令:select database();
mysql> select database();

5.6 当前数据库包含的表信息
命令:show tables;(注意:最后有个s)
mysql> show tables;


2、表操作–DDL

操作之前应连接某个数据库。

2.1 建立表
create table <表名>
(<字段名1> <类型1> [,..<字段名n> <类型n>]);

create table tablename
(
col1 type1 [not null] [primary key],
col2 type2 [not null],..
);

create table myclass (
id int(4) not null primary key auto_increment,
name varchar(20) not null,
sex int(4) not null default '0',
degree double(16,2)
);

补充:根据已有的表创建新表。
create table tab_new like tab_old; (只有表结构)

create table tab_new as select * from tab_old;
(既包含表结构,又包含表数据)

mysql> create table myclass2 like myclass;
Query OK, 0 rows affected (0.00 sec)

insert  into  myclass
values(1, 'david', 1, 20130417.16);

create  table  myclass3
as
select  *  from  myclass;

Query OK, 1 row affected (0.07 sec)

select  *  from  myclass3;

2.2 获取表结构
desc 表名; /
show columns from 表名;

获取myclass & myclass2 表结构
desc  myclass;
show  columns  from  myclass2;

2.3 删除表
命令:drop table <表名>;

例:删除表名为 myclass3 的表
drop  table  myclass3;

2.4 更改表名
命令:rename  table 原表名 to 新表名;

例:将表 myclass2 名字更改为 myclass4
rename  table  myclass2  to  myclass4;

6.5 在表中增加字段
命令:alter table 表名 add 字段 类型 其他;

例:在表 myclass 中
添加了一个字段passtest,类型为int(4),默认值为0。
alter  table  myclass
add  passtest  int(4)  default  '0';

扩展:
mysql在表的某一位置增加一列的命令
1.如果想在一个已经建好的表中添加一列,可以用诸如:
alter  table  t1
add  column  addr  varchar(20)  not  null;

这条语句会向已有的表t1中加入一列addr,
这一列在表的最后一列位置。

2.如果我们希望添加在指定的一列,可以用:
alter  table  t1
add  column  addr  varchar(20)  not null
after  user1;
注意,上面这个命令的意思是说添加addr列到user1这一列后面。

3.如果想添加到第一列的话,可以用:
alter  table  t1
add  column  addr  varchar(20)  not  null
first;

4.将表test,列名def改为unit,关键字是change
alter  test  change  def  unit  varchar(10);

5.将表test中的列名def的列删除,关键字是drop  column
alter  table  test  drop  column  def ;

其他解决方案
没有必要在建表时一定要按
Name Varchar2(11), Age Number(3),Sex Char(1),

当你把数据表建好时,select 的时候就可以改变查询的结果了

在SQL表中,怎样把原先的字段改变位置?
name,号码,住址,关系,ID

1)把这个表rename ;
rename  table1  to  table2;

2)重新创建一个表
create table table1
as
select ID,name,号码,住址,关系 from table2;

3)增加其他索引等对象。


3、数据操作

3.1 插入数据

命令:insert into <表名> [( <字段名1>[,..<字段名n > ])]
values ( 值1 )[, ( 值n )];

例:向 myclass 表中插入以下记录,留空的表示使用默认值。
mysql> insert into myclass
(id, name, sex, degree, passtest)
values(1, 'david', 1, 80.56, 78);

Query OK, 1 row affected (0.00 sec)

mysql> insert into myclass
values(2, 'sandy', 0, 100, 90);

Query OK, 1 row affected (0.00 sec)

mysql> insert into myclass (id, name, sex, degree)
values(3, 'renee', 0, 90.34);

Query OK, 1 row affected (0.00 sec)

mysql> insert into myclass (id, name) values(4, 'china');
Query OK, 1 row affected (0.00 sec)

3.2 查询表中的数据

a. 查询所有行
命令:select <字段1,字段2,...>
fro
beea
m < 表名 > where < 表达式 >;

例1:查看表 myclass 中所有数据
mysql> select * from myclass;

例2:查询表 david 相关信息
mysql> select * from myclass where name='Kevin';

b. 查询前几行数据
例如:查看表 myclass 中前2行数据
mysql> select * from myclass limit 2;
或者:
select * from myclass order by id limit 2;

扩展:
select * from table limit m,n;
其中m是指记录开始的index,从0开始,表示第一条记录
n是指从第m+1条开始,取n条。
select * from tablename limit 2,4
即取出第3条至第6条,4条记录

3.3 删除表中的数据
命令:delete from 表名 where 表达式;

例如:删除表 myclass 中编号为4的记录
mysql> delete from myclass where id=4;

3.4 修改表中的数据
update 表名 set 字段 = 新值,… where 条件;

例:修改 myclass 表中编号为1的记录,将degree 值改成89.99
mysql> update myclass set degree=89.99 where id=1;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 数据库