码迷,mamicode.com
首页 > 数据库 > 详细

mysql 操作sql语句 操作数据表

时间:2018-10-25 17:56:19      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:lin   0.00   数据表   warnings   alter   sele   links   odi   rev   

 

 

#2. 操作文件
    先切换到文件夹下:use db1

查看当前所在的数据库
mysql> select database();
+------------+
| database() |
+------------+
| db1        |
+------------+
1 row in set (0.00 sec)

 

每创建一个表每个字段都有相应类型
int 整数类型
char 字符类型 增:create table t1(id int,name char);

创建的数据库都在 在 /var/lib/mysql目录下


[root@mysql mysql]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@mysql mysql]# cd /var/lib/mysql
[root@mysql mysql]# ll
总用量 20492
drwx------. 2 mysql mysql     4096 10月  6 03:55 db1
-rw-rw----. 1 mysql mysql 10485760 10月  6 03:49 ibdata1
-rw-rw----. 1 mysql mysql  5242880 10月  6 03:49 ib_logfile0
-rw-rw----. 1 mysql mysql  5242880 10月  6 02:16 ib_logfile1
drwx------. 2 mysql mysql     4096 10月  6 02:16 mysql
srwxrwxrwx. 1 mysql mysql        0 10月  6 03:47 mysql.sock
drwx------. 2 mysql mysql     4096 10月  6 02:15 test

 

db1 数据库下面有 新创建的 t1 数据表

[root@mysql mysql]# cd db1/



[root@mysql db1]# ll
总用量 20
-rw-rw----. 1 mysql mysql   61 10月  6 03:50 db.opt
-rw-rw----. 1 mysql mysql 8586 10月  6 03:55 t1.frm
-rw-rw----. 1 mysql mysql    0 10月  6 03:55 t1.MYD
-rw-rw----. 1 mysql mysql 1024 10月  6 03:55 t1.MYI

 

 查看当前数据库所有表

   查:show tables

查表结构 根据数据表名字
show create table t1;
mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `name` char(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 


modify 修改意思 只能改字段类型  字段名不能改
改哪个字段

alter table 表名 modify 要改的字段 数据类型 和长度

        改:alter table t1 modify name char(3);

mysql> alter table t1 modify name char(3);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `name` char(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

另外一种方式查表 查看表结构

desc 表名;

desc t1;

 

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(3) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 

change 可以改字段名

alter table 表名 change 要改的字段 改后的字段 数据类型 和长度
alter table t1 change name name1 char(2); 删:drop table t1;
 
mysql> alter table t1 change name NAME char(2);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> 
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| NAME  | char(2) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)



mysql> show create table t1; +-------+---------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL, `NAME` char(2) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+---------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

 


删:drop table t1;


 

mysql 操作sql语句 操作数据表

标签:lin   0.00   数据表   warnings   alter   sele   links   odi   rev   

原文地址:https://www.cnblogs.com/mingerlcm/p/9745721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!