sql_关于表的操作

1.1、 关于表的操作
1.1.1、 创建
1.1.1.1、 创建表create:
语法:create table 数据库名.表名 (字段名 类型, 字段名 类型, 字段名 类型);
mysql> use HA;
Database changed
mysql> create table student(id int(20),name char(40),age int);
Query OK, 0 rows affected (0.02 sec)
1.1.1.2、 指定默认存储引擎和字符集
新建一个表,指定默认的存储引擎为InnoDB,编码为utf8
mysql> create table student2(id int(20),name char(40),age int) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> show create table student2 \G

1.1.2、 查看
1.1.2.3、 查看有哪些表
mysql> use HA;
Database changed
mysql> show tables;

1.1.2.4、 查看表结构(字段)
使用desc 命令来查看表的结构
mysql> desc student;

还可以用以下命令查看表结构,会一种就可以。
mysql> explain mysql.user;
mysql> show columns from mysql.user;
mysql> show fields from mysql.user;
mysql> show columns from mysql.user like ‘%user’;
1.1.2.5、 查看创建表执行了哪些命令:
mysql> show create table student \G

1.1.3、 禁止预读表信息
没有禁止前转换数据库会有提示信息
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with –A
解决这个问题可以在登陆mysql的时候添加参数-A
[root@cong11 ~]# mysql -uroot -p123456 -A
mysql> use mysql;
Database changed #发现没有提示信息了
1.1.4、 修改
1.1.4.6、 修改表名称 alter
语法:alter table 表名 rename 新表名;
修改HA库中student表名为students
mysql> alter table student rename students;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;

1.1.4.7、 修改表中的字段类型
语法:alter table 表名 modify 要修改的字段名要修改的类型;
查看students表结构
mysql> desc students;

修改字段id 的int(20)字段类型为int(10)
mysql> alter table students modify id int(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看修改完的students表结构
mysql> desc students;

1.1.4.8、 修改表中的字段类型和字段名称
语法:alter table 表名 change 原字段名 新字段名 新字段类型;
mysql> alter table students change name stname char(20);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc students;

注:CHANGE 和MODIFY的区别:
CHANGE 对列进行重命名和更改列的类型,需给定旧的列名称和新的列名称、当前的类型。 MODIFY 可以改变列的类型,此时不需要重命名(不需给定新的列名称)
1.1.5、 增加字段
1.1.5.9、 在表中添加字段:
语法:alter table 表名 add字段名 字段类型;
enum #枚举类型,比如性别,只能在男女选择,是男非女,是女非难
mysql> alter table students add sex enum(‘M’,‘W’);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc students;

1.1.5.10、 在表中指定位置添加字段
在第一列添加一个字段
mysql> alter table students add uid int(10) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc students;

在age后面添加一个address字段
mysql> alter table students add address char(40) after age;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

1.1.6、 删除
1.1.6.11、 删除表中字段
语法:alter table 表名 drop 字段名 ;
mysql> alter table students drop address;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc students;
发现表中address字段不见了

1.1.6.12、 删除表
mysql> drop table student2;
Query OK, 0 rows affected (0.01 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值