其他分享
首页 > 其他分享> > DDL(数据定义语言)

DDL(数据定义语言)

作者:互联网

 1.库的管理

CREATE DATABASE IF NOT EXISTS books;

RENAME DATABASE books TO new_books;

ALTER DATABASE books CHARACTER SET gbk;

DROP DATABASE IF EXISTS books;

2.表的管理

create table 【if not exists】 表名(

​ 列名 列的类型【(长度) 约束】,

​ 列名 列的类型【(长度) 约束】,

​ …

)

CREATE TABLE book (
id INT,
bname VARCHAR (20),
price DOUBLE,
authorid INT,
publishdate DATETIME
) ;

CREATE TABLE author (
id INT,
au_name VARCHAR (20),
nation VARCHAR (10)
) ;

DESC author;

ALTER TABLE book
CHANGE COLUMN publishdate pubdate DATETIME ;

ALTER TABLE book
MODIFY COLUMN pubdate TIMESTAMP ;

ALTER TABLE author
ADD COLUMN annual DOUBLE ;

ALTER TABLE author
DROP COLUMN annual ;

ALTER TABLE author
RENAME TO book_author ;

 

DROP DATABASE IF EXISTS 旧库名;
CREATE DATABASE 新库名;

DROP TABLE IF EXISTS 旧表名;
CREATE TABLE 表名();

CREATE TABLE copy LIKE book_author ;

CREATE TABLE copy2
SELECT
*
FROM
book_author ;

CREATE TABLE copy3
SELECT
id,
au_name
FROM
book_author
WHERE nation = '中国' ;

CREATE TABLE copy4
SELECT
id,
au_name
FROM
book_author
WHERE 0 ;

USE myemployees;
CREATE TABLE dept1 (id INT (7), NAME VARCHAR (25)) ;

CREATE TABLE dept2
SELECT
department_id,
department_name
FROM
departments ;

CREATE TABLE emp5 (
id INT (7),
first_name VARCHAR (25),
last_name VARCHAR (25),
dept_id INT (7)
) ;

ALTER TABLE emp5 MODIFY COLUMN last_name VARCHAR(50);

CREATE TABLE employee2 LIKE employees ;

DROP TABLE IF EXISTS emp5;

ALTER TABLE employee2
RENAME TO emp5 ;

ALTER TABLE emp5
ADD COLUMN test_column INT ;
DESC emp5;

ALTER TABLE emp5
DROP COLUMN dept_id ;

原文链接:https://blog.csdn.net/qq_21579045/article/details/98111827

标签:author,CREATE,表名,定义语言,emp5,DDL,TABLE,数据,ALTER
来源: https://www.cnblogs.com/hxl-learning-space/p/14594464.html