时间:2021-07-01 10:21:17 帮助过:14人阅读
示例:
#创建数据库,指定字符集utf8 mysql> create database nulige character set utf8; Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+ | information_schema | | mysql | | nulige | | performance_schema | | s3 | | s4 | | test | +--------------------+ 7 rows in set (0.00 sec) #进入nulige库 mysql> use nulige Database changed #查看表 mysql> show tables; Empty set (0.00 sec) #创建employee表 CREATE TABLE employee( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), gender boolean, age INT DEFAULT 19, department VARCHAR(20), salary DOUBLE(7,2) ); #查看表 mysql> show tables; +------------------+ | Tables_in_nulige | +------------------+ | employee | +------------------+ 1 row in set (0.00 sec) #查看表结构 mysql> desc employee; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id | tinyint(4) | NO | PRI | NULL | auto_increment | | name | varchar(25) | YES | | NULL | | | gender | tinyint(1) | YES | | NULL | | | age | int(11) | YES | | 19 | | | department | varchar(20) | YES | | NULL | | | salary | double(7,2) | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec) #增加一个字段 mysql> alter table employee add is_married tinyint(1); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #在表中增加1个字段 mysql> alter table employee add entry_date date not null; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 #增加二个字段 mysql> alter table employee add a INT, -> ADD B VARCHAR(20); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #创建表 mysql> create table A(id int); Query OK, 0 rows affected (0.01 sec) #查看表 mysql> show tables; +------------------+ | Tables_in_nulige | +------------------+ | A | | emp | +------------------+ 2 rows in set (0.00 sec) #删除表 mysql> alter table employee DROP A; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #删除employee表中b字段 mysql> alter table employee DROP b, -> DROP entry_date; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #修改表中age字段的 null default 属于,并且在ID后面(after) mysql> alter table employee modify age smallint not null default 18 after id; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #查看表结构 mysql> desc employee; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id | tinyint(4) | NO | PRI | NULL | auto_increment | | age | smallint(6) | NO | | 18 | | | name | varchar(25) | YES | | NULL | | | gender | tinyint(1) | YES | | NULL | | | department | varchar(20) | YES | | NULL | | | salary | double(7,2) | YES | | NULL | | | is_married | tinyint(1) | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+ 7 rows in set (0.01 sec) #修表employee表中字段名称:把department改成depart字段,并且把位置调到salary后面 mysql> alter table employee change department depart varchar(20) after salary; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 #查看表结构 mysql> desc employee; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id | tinyint(4) | NO | PRI | NULL | auto_increment | | age | smallint(6) | NO | | 18 | | | name | varchar(25) | YES | | NULL | | | gender | tinyint(1) | YES | | NULL | | | salary | double(7,2) | YES | | NULL | | | depart | varchar(20) | YES | | NULL | | | is_married | tinyint(1) | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec) #修改表名 mysql> rename table employee to emp; Query OK, 0 rows affected (0.00 sec) #查看表 mysql> show tables; +------------------+ | Tables_in_nulige | +------------------+ | emp | +------------------+ 1 row in set (0.00 sec) #创建表 mysql> create table A(id int); Query OK, 0 rows affected (0.01 sec) #查看表 mysql> show tables; +------------------+ | Tables_in_nulige | +------------------+ | A | | emp | +------------------+ 2 rows in set (0.00 sec) #删除A表 mysql> drop table A; Query OK, 0 rows affected (0.01 sec) #查看表 mysql> show tables; +------------------+ | Tables_in_nulige | +------------------+ | emp | +------------------+ 1 row in set (0.00 sec)
mysql 表操作
标签:des records not rename var 逗号 外键 条件 ddr