189 8069 5689

Linux_MySQL(mariadb)数据库上(3)

创新互联公司主要从事做网站、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务嵊泗,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

 

服务器端命令:

       DDL:数据定义语言,主要用于管理数据库组件,例如表、索引、视图、用户、存储过程

              CREATE、ALTER、DROP

       DML:数据操纵语言,主要用管理表中的数据,实现数据的增、删、改、查;

              INSERT, DELETE, UPDATE, SELECT

获取命令帮助:

       MySQL> help  KEYWORD

                           

数据库管理:

       创建:CREATE  {DATABASE | SCHEMA}  [IF NOT EXISTS]  db_name;

                     [DEFAULT]  CHARACTER SET [=] charset_name 设置字符集

                     [DEFAULT]  COLLATE [=] collation_name  排序规则

例:CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Linux_MySQL(mariadb)数据库上(3)

                                         

              查看支持的所有字符集:SHOW CHARACTER SET

              查看支持的所有排序规则:SHOW  COLLATION

修改:

       ALTER {DATABASE | SCHEMA}  [db_name]

       [DEFAULT]  CHARACTER SET [=] charset_name

       [DEFAULT]  COLLATE [=] collation_name

                                         

删除:

       DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

表管理:

       创建:

              CREATE TABLE  [IF NOT EXISTS]  tbl_name  (create_defination表定义)  [table_options]

       create_defination:

              字段:col_name  data_type

                     键:

                            PRIMARY KEY (col1, col2, ...) 主键约束

                            UNIQUE KEY  (col1, col2,...)       唯一键约束

                            FOREIGN KEY (column) 外键约束

                     索引:

                            KEY|INDEX  [index_name]  (col1, col2,...)

例: CREATE TABLE students(id INT UNSIGNED NOT NULL,name CHAR(30) NOT NULL,age TINYINT UNSIGNED,gender ENUM(‘f’,’m’) );

创建  表 学生 (id 整数型 无符号 不为空,姓名 字符型(30) 不为空,年龄 小整型 无符号,性别 枚举(‘f’,’m’));

UNSIGNED 必须跟在字符型之后 其他次序无所谓

Linux_MySQL(mariadb)数据库上(3)

              查看数据库支持的所有存储引擎类型:

                                          mysql> SHOW  ENGINES;

查看某表的存储引擎类型:

                                          mysql> SHOW  TABLES  STATUS  [LIKE  'tbl_name']

              table_options:

                     ENGINE [=] engine_name 存储引擎类型

修改:

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name  [alter_specification [, alter_specification] ...]                            alter_specification: 字段或属性

              字段:

                     添加:ADD  [COLUMN]字段名  col_name  data_type  [FIRST | AFTER col_name ]放置位置

            Linux_MySQL(mariadb)数据库上(3)

                     删除:DROP  [COLUMN] col_name

例:ALTER TABLE student DROP class;

                     修改:

                            CHANGE [COLUMN] old_col_name new_col_name column_definition  [FIRST|AFTER col_name]      

改名字和字段名

                            MODIFY [COLUMN] col_name column_definition  [FIRST | AFTER col_name]

改字段定义(只要不改名字都可用)

例: ALTER TABLE student MODIFY calss VARCHAR(100) AFTER age;

              键:

                     添加:ADD  {PRIMARY|UNIQUE|FOREIGN}  KEY (col1, col2,...)

例:ALTER TABLE student ADD UNIQUE KEY (name); 添加唯一键 (NULL不为空时自动转换成主键)

                     删除:

                            主键:DROP PRIMARY KEY

                            外键:DROP FOREIGN KEY fk_symbol

              索引:

                     添加:ADD {INDEX|KEY} [index_name]  (col1, col2,...)

例:ALTER TABLE student ADD INDEX index_calss (class);  index_name为自己起的名字

                     删除:DROP {INDEX|KEY}  index_name

例:ALTER TABLE  student DROP INDEX name;

              表选项:

                     ENGINE [=] engine_name

                                         

       查看表上的索引的信息:

              mysql> SHOW INDEXES FROM tbl_name;

删除表:

                                   DROP  TABLE  [IF EXISTS]   tbl_name [, tbl_name] ...

表的引用方式:

                                   tbl_name 相当于相对路径 必须指定当前表(USE db_name;)

                                   db_name.tbl_name 相当于绝对路径


分享文章:Linux_MySQL(mariadb)数据库上(3)
转载来源:http://cdxtjz.cn/article/giphse.html

其他资讯